Skip to content

Commit 80935ad

Browse files
Adding ScrollController support for Stepper widget (#128814)
This PR is to add **controller** property to the **Stepper** widget, so that user has the flexibility to control the scroll offset for various purposes(especially in case of scroll animations)! Fixes #61207 Co-authored-by: Taha Tesser <tessertaha@gmail.com>
1 parent ca6f08a commit 80935ad

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/flutter/lib/src/material/stepper.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Stepper extends StatefulWidget {
202202
const Stepper({
203203
super.key,
204204
required this.steps,
205+
this.controller,
205206
this.physics,
206207
this.type = StepperType.vertical,
207208
this.currentStep = 0,
@@ -230,6 +231,13 @@ class Stepper extends StatefulWidget {
230231
/// can be helpful to set this property to [ClampingScrollPhysics].
231232
final ScrollPhysics? physics;
232233

234+
/// An object that can be used to control the position to which this scroll
235+
/// view is scrolled.
236+
///
237+
/// To control the initial scroll offset of the scroll view, provide a
238+
/// [controller] with its [ScrollController.initialScrollOffset] property set.
239+
final ScrollController? controller;
240+
233241
/// The type of stepper that determines the layout. In the case of
234242
/// [StepperType.horizontal], the content of the current step is displayed
235243
/// underneath as opposed to the [StepperType.vertical] case where it is
@@ -765,6 +773,7 @@ class _StepperState extends State<Stepper> with TickerProviderStateMixin {
765773

766774
Widget _buildVertical() {
767775
return ListView(
776+
controller: widget.controller,
768777
shrinkWrap: true,
769778
physics: widget.physics,
770779
children: <Widget>[
@@ -858,6 +867,7 @@ class _StepperState extends State<Stepper> with TickerProviderStateMixin {
858867
),
859868
Expanded(
860869
child: ListView(
870+
controller: widget.controller,
861871
physics: widget.physics,
862872
padding: const EdgeInsets.all(24.0),
863873
children: <Widget>[

packages/flutter/test/material/stepper_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,37 @@ testWidgets('Stepper custom indexed controls test', (WidgetTester tester) async
972972
}
973973
});
974974

975+
testWidgets('ScrollController is passed to the stepper listview', (WidgetTester tester) async {
976+
final ScrollController controller = ScrollController();
977+
for (final StepperType type in StepperType.values) {
978+
await tester.pumpWidget(
979+
MaterialApp(
980+
home: Material(
981+
child: Stepper(
982+
controller: controller,
983+
type: type,
984+
steps: const <Step>[
985+
Step(
986+
title: Text('Step 1'),
987+
content: SizedBox(
988+
width: 100.0,
989+
height: 100.0,
990+
),
991+
),
992+
],
993+
),
994+
),
995+
),
996+
);
997+
998+
final ListView listView = tester.widget<ListView>(
999+
find.descendant(of: find.byType(Stepper),
1000+
matching: find.byType(ListView),
1001+
));
1002+
expect(listView.controller, controller);
1003+
}
1004+
});
1005+
9751006
testWidgets('Stepper horizontal size test', (WidgetTester tester) async {
9761007
// Regression test for https://github.com/flutter/flutter/pull/77732
9771008
Widget buildFrame({ bool isActive = true, Brightness? brightness }) {

0 commit comments

Comments
 (0)