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
This commit is contained in:
@@ -55,7 +55,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
|
||||
TextDirection textDirection = TextDirection.ltr;
|
||||
ThemeMode themeMode = ThemeMode.light;
|
||||
late final ScrollController scrollController;
|
||||
bool isRunning = false;
|
||||
Future<void>? currentAction;
|
||||
|
||||
static const Widget verticalSpacer = SizedBox(height: 16);
|
||||
static const Widget horizontalSpacer = SizedBox(width: 32);
|
||||
@@ -345,17 +345,22 @@ class _TextButtonExampleState extends State<TextButtonExample> {
|
||||
// to provide one.
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
setState(() { isRunning = true; });
|
||||
Future<void>.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<void> thisAction;
|
||||
thisAction = Future<void>.delayed(const Duration(seconds: 1), () {
|
||||
if (currentAction == thisAction) {
|
||||
setState(() { currentAction = null; });
|
||||
}
|
||||
});
|
||||
setState(() { currentAction = thisAction; });
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
overlayColor: Colors.transparent,
|
||||
foregroundBuilder: (BuildContext context, Set<MaterialState> 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<TextButtonExample> {
|
||||
),
|
||||
horizontalSpacer,
|
||||
|
||||
// All of the button examples appear below. They're arranged in two columns.
|
||||
|
||||
Expanded(
|
||||
child: Scrollbar(
|
||||
controller: scrollController,
|
||||
|
||||
@@ -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<AnimatedContainer>(
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user