Skip to content

Commit 3d3f8e8

Browse files
authored
Update CupertinoPicker example (#118248)
* Update `CupertinoPicker` example * format lines * Revert making variable public * revert variable change
1 parent 1d2e62b commit 3d3f8e8

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

examples/api/lib/cupertino/picker/cupertino_picker.0.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CupertinoPickerApp extends StatelessWidget {
2929
);
3030
}
3131
}
32+
3233
class CupertinoPickerExample extends StatefulWidget {
3334
const CupertinoPickerExample({super.key});
3435

@@ -57,7 +58,7 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
5758
top: false,
5859
child: child,
5960
),
60-
)
61+
),
6162
);
6263
}
6364

@@ -86,23 +87,24 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
8687
squeeze: 1.2,
8788
useMagnifier: true,
8889
itemExtent: _kItemExtent,
90+
// This sets the initial item.
91+
scrollController: FixedExtentScrollController(
92+
initialItem: _selectedFruit,
93+
),
8994
// This is called when selected item is changed.
9095
onSelectedItemChanged: (int selectedItem) {
9196
setState(() {
9297
_selectedFruit = selectedItem;
9398
});
9499
},
95100
children: List<Widget>.generate(_fruitNames.length, (int index) {
96-
return Center(
97-
child: Text(
98-
_fruitNames[index],
99-
),
100-
);
101+
return Center(child: Text(_fruitNames[index]));
101102
}),
102103
),
103104
),
104105
// This displays the selected fruit name.
105-
child: Text(_fruitNames[_selectedFruit],
106+
child: Text(
107+
_fruitNames[_selectedFruit],
106108
style: const TextStyle(
107109
fontSize: 22.0,
108110
),

examples/api/test/cupertino/picker/cupertino_picker.0_test.dart

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/cupertino.dart';
56
import 'package:flutter_api_samples/cupertino/picker/cupertino_picker.0.dart' as example;
67
import 'package:flutter_test/flutter_test.dart';
78

@@ -14,19 +15,29 @@ void main() {
1415
);
1516

1617
// Open the Cupertino picker.
17-
await tester.tap(find.text('Apple'));
18+
await tester.tap(find.widgetWithText(CupertinoButton, 'Apple'));
1819
await tester.pumpAndSettle();
1920

21+
// Test the initial item.
22+
CupertinoPicker picker = tester.widget<CupertinoPicker>(find.byType(CupertinoPicker));
23+
expect(picker.scrollController!.initialItem, 0);
24+
2025
// Drag the wheel to change fruit selection.
2126
await tester.drag(find.text('Mango'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file
22-
2327
await tester.pump();
2428
await tester.pump(const Duration(milliseconds: 500));
2529

2630
// Close the Cupertino picker.
2731
await tester.tapAt(const Offset(1.0, 1.0));
2832
await tester.pumpAndSettle();
2933

30-
expect(find.text('Banana'), findsOneWidget);
34+
expect(find.widgetWithText(CupertinoButton, 'Banana'), findsOneWidget);
35+
36+
// Test if the initial item has updated.
37+
await tester.tap(find.widgetWithText(CupertinoButton, 'Banana'));
38+
await tester.pumpAndSettle();
39+
40+
picker = tester.widget<CupertinoPicker>(find.byType(CupertinoPicker));
41+
expect(picker.scrollController!.initialItem, 2);
3142
});
3243
}

0 commit comments

Comments
 (0)