-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.2.0 ready
- Loading branch information
Showing
15 changed files
with
228 additions
and
172 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
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
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 |
---|---|---|
|
@@ -33,7 +33,7 @@ dependencies: | |
sdk: flutter | ||
|
||
|
||
viewmodel: | ||
view_model_x: | ||
path: ../ | ||
cupertino_icons: ^1.0.2 | ||
|
||
|
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,61 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'flow.dart'; | ||
|
||
/// [SharedFlowListener] is used to catch the emitted value from [sharedFlow]. | ||
/// This requires [sharedFlow], [listener] and [child]. | ||
/// Whenever [sharedFlow] emits a value, [listener] will called. | ||
class SharedFlowListener<T> extends StatefulWidget { | ||
final SharedFlow<T> sharedFlow; | ||
final void Function(BuildContext context, T value) listener; | ||
final Widget child; | ||
|
||
const SharedFlowListener( | ||
{super.key, | ||
required this.sharedFlow, | ||
required this.listener, | ||
required this.child}); | ||
|
||
@override | ||
State<SharedFlowListener<T>> createState() => _SharedFlowListenerState<T>(); | ||
} | ||
|
||
class _SharedFlowListenerState<T> extends State<SharedFlowListener<T>> { | ||
@override | ||
void initState() { | ||
super.initState(); | ||
widget.sharedFlow.addListener(_stateListener); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
widget.sharedFlow.removeListener(_stateListener); | ||
super.dispose(); | ||
} | ||
|
||
void _stateListener() { | ||
if (mounted) { | ||
final value = widget.sharedFlow.lastEmitValue; | ||
if (value != null) { | ||
widget.listener(context, value); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant SharedFlowListener<T> oldWidget) { | ||
if (widget.sharedFlow != oldWidget.sharedFlow) { | ||
_migrate(widget.sharedFlow, oldWidget.sharedFlow, _stateListener); | ||
} | ||
super.didUpdateWidget(oldWidget); | ||
} | ||
|
||
void _migrate(Listenable a, Listenable b, void Function() listener) { | ||
b.removeListener(listener); | ||
a.addListener(listener); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return widget.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
Oops, something went wrong.