From 52961f761f80f602375dd8b7f3f08335d4166c66 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 5 Mar 2024 12:57:02 -0800 Subject: [PATCH] Updated the smiley TextButton example again (#144630) Improved the smiley image TextButton example a little. Handling the case where the `Future.delayed` object that represents the button's one second long action is superseded by a second button press that triggers a new one-second action. This does complicate the example - just a little - but it's a little more robust. In case someone copy-and-pastes the code. The TextButton example was recently updated: https://github.com/flutter/flutter/pull/144583 --- .../material/text_button/text_button.0.dart | 19 +++++++++++-------- .../text_button/text_button.0_test.dart | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/examples/api/lib/material/text_button/text_button.0.dart b/examples/api/lib/material/text_button/text_button.0.dart index 18ad6ecd68..805fce835e 100644 --- a/examples/api/lib/material/text_button/text_button.0.dart +++ b/examples/api/lib/material/text_button/text_button.0.dart @@ -55,7 +55,7 @@ class _TextButtonExampleState extends State { TextDirection textDirection = TextDirection.ltr; ThemeMode themeMode = ThemeMode.light; late final ScrollController scrollController; - bool isRunning = false; + Future? currentAction; static const Widget verticalSpacer = SizedBox(height: 16); static const Widget horizontalSpacer = SizedBox(width: 32); @@ -345,17 +345,22 @@ class _TextButtonExampleState extends State { // to provide one. TextButton( onPressed: () async { - setState(() { isRunning = true; }); - Future.delayed(const Duration(seconds: 1), () { - // Simulate a time consuming action. - setState(() { isRunning = false; }); + // This is slightly complicated so that if the user presses the button + // while the current Future.delayed action is running, the currentAction + // flag is only reset to null after the _new_ action completes. + late final Future thisAction; + thisAction = Future.delayed(const Duration(seconds: 1), () { + if (currentAction == thisAction) { + setState(() { currentAction = null; }); + } }); + setState(() { currentAction = thisAction; }); }, style: TextButton.styleFrom( overlayColor: Colors.transparent, foregroundBuilder: (BuildContext context, Set states, Widget? child) { late final ImageProvider image; - if (isRunning) { + if (currentAction != null) { image = runningImage; } else if (states.contains(MaterialState.pressed)) { image = pressedImage; @@ -401,8 +406,6 @@ class _TextButtonExampleState extends State { ), horizontalSpacer, - // All of the button examples appear below. They're arranged in two columns. - Expanded( child: Scrollbar( controller: scrollController, diff --git a/examples/api/test/material/text_button/text_button.0_test.dart b/examples/api/test/material/text_button/text_button.0_test.dart index f444f332cf..9b9ec76271 100644 --- a/examples/api/test/material/text_button/text_button.0_test.dart +++ b/examples/api/test/material/text_button/text_button.0_test.dart @@ -55,8 +55,8 @@ void main() { await tester.pumpAndSettle(); final Finder smileyButton = find.byType(TextButton).last; - await tester.tap(smileyButton); // Smiley image button - await tester.pumpAndSettle(); + await tester.tap(smileyButton); + await tester.pump(); String smileyButtonImageUrl() { final AnimatedContainer container = tester.widget( @@ -66,6 +66,7 @@ void main() { final NetworkImage image = decoration.image!.image as NetworkImage; return image.url; } + // The smiley button's onPressed method changes the button image // for one second to simulate a long action running. The button's // image changes while the action is running. @@ -73,6 +74,18 @@ void main() { await tester.pump(const Duration(seconds: 1)); expect(smileyButtonImageUrl().endsWith('text_button_nhu_default.png'), isTrue); + // Pressing the smiley button while the one second action is + // underway starts a new one section action. The button's image + // doesn't change until the second action has finished. + await tester.tap(smileyButton); + await tester.pump(const Duration(milliseconds: 500)); + expect(smileyButtonImageUrl().endsWith('text_button_nhu_end.png'), isTrue); + await tester.tap(smileyButton); // Second button press. + await tester.pump(const Duration(milliseconds: 500)); + expect(smileyButtonImageUrl().endsWith('text_button_nhu_end.png'), isTrue); + await tester.pump(const Duration(milliseconds: 500)); + expect(smileyButtonImageUrl().endsWith('text_button_nhu_default.png'), isTrue); + await tester.tap(find.byType(Switch).at(0)); // Dark Mode Switch await tester.pumpAndSettle();