From fb56442de74166a308f84d4aee943acee4205be0 Mon Sep 17 00:00:00 2001 From: Craig Labenz Date: Thu, 2 Sep 2021 14:36:16 -0700 Subject: [PATCH] fix: Preserve state in horizontal stepper (#84993) --- .../flutter/lib/src/material/stepper.dart | 13 ++- .../flutter/test/material/stepper_test.dart | 109 ++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/stepper.dart b/packages/flutter/lib/src/material/stepper.dart index 4033c7e944..1b84ae04a1 100755 --- a/packages/flutter/lib/src/material/stepper.dart +++ b/packages/flutter/lib/src/material/stepper.dart @@ -728,6 +728,17 @@ class _StepperState extends State with TickerProviderStateMixin { ], ]; + final List stepPanels = []; + for (int i = 0; i < widget.steps.length; i += 1) { + stepPanels.add( + Visibility( + maintainState: true, + visible: i == widget.currentStep, + child: widget.steps[i].content, + ), + ); + } + return Column( children: [ Material( @@ -747,7 +758,7 @@ class _StepperState extends State with TickerProviderStateMixin { AnimatedSize( curve: Curves.fastOutSlowIn, duration: kThemeAnimationDuration, - child: widget.steps[widget.currentStep].content, + child: Column(children: stepPanels, crossAxisAlignment: CrossAxisAlignment.stretch), ), _buildVerticalControls(widget.currentStep), ], diff --git a/packages/flutter/test/material/stepper_test.dart b/packages/flutter/test/material/stepper_test.dart index da104107bc..89b580ee33 100644 --- a/packages/flutter/test/material/stepper_test.dart +++ b/packages/flutter/test/material/stepper_test.dart @@ -1048,6 +1048,77 @@ testWidgets('Stepper custom indexed controls test', (WidgetTester tester) async expect(material.elevation, 2.0); }); + testWidgets('Stepper horizontal preserves state', (WidgetTester tester) async { + const Color untappedColor = Colors.blue; + const Color tappedColor = Colors.red; + int index = 0; + + Widget buildFrame() { + return MaterialApp( + home: Scaffold( + body: Center( + // Must break this out into its own widget purely to be able to call `setState()` + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Stepper( + onStepTapped: (int i) => setState(() => index = i), + currentStep: index, + type: StepperType.horizontal, + steps: const [ + Step( + title: Text('Step 1'), + content: _TappableColorWidget( + key: Key('tappable-color'), + tappedColor: tappedColor, + untappedColor: untappedColor, + ), + ), + Step( + title: Text('Step 2'), + content: Text('Step 2 Content'), + ), + ], + ); + }, + ), + ), + ), + ); + } + + final Widget widget = buildFrame(); + await tester.pumpWidget(widget); + + // Set up a getter to examine the MacGuffin's color + Color getColor() => tester.widget( + find.descendant(of: find.byKey(const Key('tappable-color')), matching: find.byType(ColoredBox)), + ).color; + + // We are on step 1 + expect(find.text('Step 2 Content'), findsNothing); + expect(getColor(), untappedColor); + + await tester.tap(find.byKey(const Key('tap-me'))); + await tester.pumpAndSettle(); + expect(getColor(), tappedColor); + + // Now flip to step 2 + await tester.tap(find.text('Step 2')); + await tester.pumpAndSettle(); + + // Confirm that we did in fact flip to step 2 + expect(find.text('Step 2 Content'), findsOneWidget); + + // Now go back to step 1 + await tester.tap(find.text('Step 1')); + await tester.pumpAndSettle(); + + // Confirm that we flipped back to step 1 + expect(find.text('Step 2 Content'), findsNothing); + + // The color should still be `tappedColor` + expect(getColor(), tappedColor); + }); testWidgets('Stepper custom margin', (WidgetTester tester) async { const EdgeInsetsGeometry margin = EdgeInsetsDirectional.only( @@ -1086,3 +1157,41 @@ testWidgets('Stepper custom indexed controls test', (WidgetTester tester) async expect(material.margin, equals(margin)); }); } + +class _TappableColorWidget extends StatefulWidget { + const _TappableColorWidget({required this.tappedColor, required this.untappedColor, Key? key,}) : super(key: key); + + final Color tappedColor; + final Color untappedColor; + + @override + State createState() => _TappableColorWidgetState(); +} + +class _TappableColorWidgetState extends State<_TappableColorWidget> { + + Color? color; + + @override + void initState() { + super.initState(); + color = widget.untappedColor; + } + + @override + Widget build(BuildContext context) { + return GestureDetector( + onTap: () { + setState((){ + color = widget.tappedColor; + }); + }, + child: Container( + key: const Key('tap-me'), + height: 50, + width: 50, + color: color, + ), + ); + } +}