From 7570495b0a25a88032b676092ce334573d92a662 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 6 Apr 2016 21:13:53 -0700 Subject: [PATCH] SnackBarActions shouldn't be tappable twice They should automagically disable after the first tap. --- .../flutter/lib/src/material/snack_bar.dart | 27 ++++++++++-- .../{widget => material}/snack_bar_test.dart | 44 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) rename packages/flutter/test/{widget => material}/snack_bar_test.dart (89%) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 1e55569af2..478dc2e248 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -36,11 +36,13 @@ const Curve _snackBarFadeCurve = const Interval(0.72, 1.0, curve: Curves.fastOut /// Snack bar actions are always enabled. If you want to disable a snack bar /// action, simply don't include it in the snack bar. /// +/// Snack bar actions can only be pressed once. Subsequent presses are ignored. +/// /// See also: /// /// * [SnackBar] /// * -class SnackBarAction extends StatelessWidget { +class SnackBarAction extends StatefulWidget { SnackBarAction({Key key, this.label, this.onPressed }) : super(key: key) { assert(label != null); assert(onPressed != null); @@ -50,16 +52,35 @@ class SnackBarAction extends StatelessWidget { final String label; /// The callback to be invoked when the button is pressed. Must be non-null. + /// + /// This callback will be invoked at most once each time this action is + /// displayed in a [SnackBar]. final VoidCallback onPressed; + @override + _SnackBarActionState createState() => new _SnackBarActionState(); +} + +class _SnackBarActionState extends State { + bool _haveTriggeredAction = false; + + void _handlePressed() { + if (_haveTriggeredAction) + return; + setState(() { + _haveTriggeredAction = true; + }); + config.onPressed(); + } + @override Widget build(BuildContext context) { return new Container( margin: const EdgeInsets.only(left: _kSideMargins), child: new FlatButton( - onPressed: onPressed, + onPressed: _haveTriggeredAction ? null : _handlePressed, textTheme: ButtonColor.accent, - child: new Text(label) + child: new Text(config.label) ) ); } diff --git a/packages/flutter/test/widget/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart similarity index 89% rename from packages/flutter/test/widget/snack_bar_test.dart rename to packages/flutter/test/material/snack_bar_test.dart index 800929c6a6..adc5abca3b 100644 --- a/packages/flutter/test/widget/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -282,4 +282,48 @@ void main() { }); }); + test('SnackBar cannot be tapped twice', () { + testWidgets((WidgetTester tester) { + int tapCount = 0; + tester.pumpWidget(new MaterialApp( + routes: { + '/': (BuildContext context) { + return new Scaffold( + body: new Builder( + builder: (BuildContext context) { + return new GestureDetector( + onTap: () { + Scaffold.of(context).showSnackBar(new SnackBar( + content: new Text('I am a snack bar.'), + duration: new Duration(seconds: 2), + action: new SnackBarAction( + label: 'ACTION', + onPressed: () { + ++tapCount; + } + ) + )); + }, + child: new Text('X') + ); + } + ) + ); + } + } + )); + tester.tap(tester.findText('X')); + tester.pump(); // start animation + tester.pump(const Duration(milliseconds: 750)); + + expect(tapCount, equals(0)); + tester.tap(tester.findText('ACTION')); + expect(tapCount, equals(1)); + tester.tap(tester.findText('ACTION')); + expect(tapCount, equals(1)); + tester.pump(); + tester.tap(tester.findText('ACTION')); + expect(tapCount, equals(1)); + }); + }); }