-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
MultiReactionBuilder
widget (#917)
* feat: add `MultiReactionBuilder` widget
- Loading branch information
Showing
7 changed files
with
158 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_mobx/src/reaction_builder.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
/// {@template multi_reaction_builder} | ||
/// Merges multiple [ReactionBuilder] widgets into one widget tree. | ||
/// | ||
/// [MultiReactionBuilder] improves the readability and eliminates the need | ||
/// to nest multiple [ReactionBuilder]s. | ||
/// | ||
/// By using [MultiReactionBuilder] we can go from: | ||
/// | ||
/// ```dart | ||
/// ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// child: ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// child: ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// child: ChildA(), | ||
/// ), | ||
/// ), | ||
/// ) | ||
/// ``` | ||
/// | ||
/// to: | ||
/// | ||
/// ```dart | ||
/// MultiReactionBuilder( | ||
/// builders: [ | ||
/// ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// ), | ||
/// ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// ), | ||
/// ReactionBuilder( | ||
/// builder: (context) {}, | ||
/// ), | ||
/// ], | ||
/// child: ChildA(), | ||
/// ) | ||
/// ``` | ||
/// | ||
/// [MultiReactionBuilder] converts the [ReactionBuilder] list into a tree of nested | ||
/// [ReactionBuilder] widgets. | ||
/// As a result, the only advantage of using [MultiReactionBuilder] is improved | ||
/// readability due to the reduction in nesting and boilerplate. | ||
/// {@endtemplate} | ||
class MultiReactionBuilder extends MultiProvider { | ||
/// {@macro multi_reaction_builder} | ||
MultiReactionBuilder({ | ||
Key? key, | ||
required List<ReactionBuilder> builders, | ||
required Widget child, | ||
}) : super(key: key, providers: builders, child: child); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import 'flutter_mobx_test.dart' as flutter_mobx_test; | ||
import 'reaction_builder_test.dart' as reaction_builder_test; | ||
import 'multi_reaction_builder_test.dart' as multi_reaction_builder_test; | ||
|
||
void main() { | ||
flutter_mobx_test.main(); | ||
reaction_builder_test.main(); | ||
multi_reaction_builder_test.main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_mobx/src/multi_reaction_builder.dart'; | ||
import 'package:flutter_mobx/src/reaction_builder.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mobx/mobx.dart'; | ||
|
||
class Counter { | ||
Counter(); | ||
|
||
Observable<int> state = Observable(0); | ||
|
||
void increment() => runInAction(() => state.value = state.value + 1); | ||
} | ||
|
||
void main() { | ||
group('MultiReactionBuilder', () { | ||
testWidgets('calls reactions on state changes', (tester) async { | ||
final statesA = <int>[]; | ||
const expectedStatesA = [1, 2]; | ||
final counterA = Counter(); | ||
|
||
final statesB = <int>[]; | ||
final expectedStatesB = [1]; | ||
final counterB = Counter(); | ||
|
||
await tester.pumpWidget( | ||
MultiReactionBuilder( | ||
key: const Key('MultiReactionBuilder'), | ||
builders: [ | ||
ReactionBuilder( | ||
builder: (context) => reaction( | ||
(_) => counterA.state.value, | ||
(int state) => statesA.add(state), | ||
), | ||
), | ||
ReactionBuilder( | ||
builder: (context) => reaction( | ||
(_) => counterB.state.value, | ||
(int state) => statesB.add(state), | ||
), | ||
), | ||
], | ||
child: const SizedBox(key: Key('multiListener_child')), | ||
), | ||
); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.byKey(const Key('multiListener_child')), findsOneWidget); | ||
|
||
counterA.increment(); | ||
await tester.pump(); | ||
counterA.increment(); | ||
await tester.pump(); | ||
counterB.increment(); | ||
await tester.pump(); | ||
|
||
expect(statesA, expectedStatesA); | ||
expect(statesB, expectedStatesB); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters