-
-
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
- Loading branch information
Showing
5 changed files
with
141 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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( | ||
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); | ||
}); | ||
}); | ||
} |