This repository was archived by the owner on Feb 25, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +35
-8
lines changed Expand file tree Collapse file tree 2 files changed +35
-8
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,8 @@ class StockHome extends StatefulComponent {
5151 bool _isSearching = false ;
5252 String _searchQuery;
5353
54- bool _isShowingSnackBar = false ;
54+ bool _isSnackBarLive = false ; // Is it on screen at all (maybe dismissing)?
55+ bool _isSnackBarShowing = false ; // Should it be showing?
5556
5657 void _handleSearchBegin () {
5758 navigator.pushState (this , (_) {
@@ -261,22 +262,25 @@ class StockHome extends StatefulComponent {
261262
262263 void _handleUndo () {
263264 setState (() {
264- _isShowingSnackBar = false ;
265+ _isSnackBarShowing = false ;
265266 });
266267 }
267268
268269 Widget buildSnackBar () {
269- if (! _isShowingSnackBar )
270+ if (! _isSnackBarLive )
270271 return null ;
271272 return new SnackBar (
273+ showing: _isSnackBarShowing,
272274 content: new Text ("Stock purchased!" ),
273- actions: [new SnackBarAction (label: "UNDO" , onPressed: _handleUndo)]
275+ actions: [new SnackBarAction (label: "UNDO" , onPressed: _handleUndo)],
276+ onHidden: () { setState (() { _isSnackBarLive = false ; }); }
274277 );
275278 }
276279
277280 void _handleStockPurchased () {
278281 setState (() {
279- _isShowingSnackBar = true ;
282+ _isSnackBarShowing = true ;
283+ _isSnackBarLive = true ;
280284 });
281285 }
282286
Original file line number Diff line number Diff line change @@ -40,30 +40,53 @@ class SnackBar extends AnimatedComponent {
4040 SnackBar ({
4141 String key,
4242 this .content,
43- this .actions
43+ this .actions,
44+ this .showing,
45+ this .onHidden
4446 }) : super (key: key) {
4547 assert (content != null );
4648 }
4749
4850 Widget content;
4951 List <SnackBarAction > actions;
52+ bool showing;
53+ Function onHidden;
5054
5155 void syncFields (SnackBar source) {
5256 content = source.content;
5357 actions = source.actions;
58+ onHidden = source.onHidden;
59+ if (showing != source.showing) {
60+ showing = source.showing;
61+ showing ? _show () : _hide ();
62+ }
5463 }
5564
5665 AnimatedType <Point > _position;
5766 AnimationPerformance _performance;
5867
5968 void initState () {
60- _position = new AnimatedType <Point >(new Point (0.0 , 48 .0 ), end: Point .origin);
69+ _position = new AnimatedType <Point >(new Point (0.0 , 50 .0 ), end: Point .origin);
6170 _performance = new AnimationPerformance ()
6271 ..duration = _kSlideInDuration
63- ..variable = _position;
72+ ..variable = _position
73+ ..addListener (_checkCompleted);
6474 watch (_performance);
75+ if (showing)
76+ _show ();
77+ }
78+
79+ void _show () {
6580 _performance.play ();
6681 }
82+ void _hide () {
83+ _performance.reverse ();
84+ }
85+ void _checkCompleted () {
86+ if (! _performance.isAnimating && _performance.isDismissed && onHidden != null ) {
87+ onHidden ();
88+ }
89+ }
6790
6891 Widget build () {
6992 List <Widget > children = [
You can’t perform that action at this time.
0 commit comments