Unverified Commit 3d3f8e85 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Update `CupertinoPicker` example (#118248)

* Update `CupertinoPicker` example

* format lines

* Revert making variable public

* revert variable change
parent 1d2e62b7
...@@ -29,6 +29,7 @@ class CupertinoPickerApp extends StatelessWidget { ...@@ -29,6 +29,7 @@ class CupertinoPickerApp extends StatelessWidget {
); );
} }
} }
class CupertinoPickerExample extends StatefulWidget { class CupertinoPickerExample extends StatefulWidget {
const CupertinoPickerExample({super.key}); const CupertinoPickerExample({super.key});
...@@ -57,7 +58,7 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> { ...@@ -57,7 +58,7 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
top: false, top: false,
child: child, child: child,
), ),
) ),
); );
} }
...@@ -86,6 +87,10 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> { ...@@ -86,6 +87,10 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
squeeze: 1.2, squeeze: 1.2,
useMagnifier: true, useMagnifier: true,
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
// This sets the initial item.
scrollController: FixedExtentScrollController(
initialItem: _selectedFruit,
),
// This is called when selected item is changed. // This is called when selected item is changed.
onSelectedItemChanged: (int selectedItem) { onSelectedItemChanged: (int selectedItem) {
setState(() { setState(() {
...@@ -93,16 +98,13 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> { ...@@ -93,16 +98,13 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
}); });
}, },
children: List<Widget>.generate(_fruitNames.length, (int index) { children: List<Widget>.generate(_fruitNames.length, (int index) {
return Center( return Center(child: Text(_fruitNames[index]));
child: Text(
_fruitNames[index],
),
);
}), }),
), ),
), ),
// This displays the selected fruit name. // This displays the selected fruit name.
child: Text(_fruitNames[_selectedFruit], child: Text(
_fruitNames[_selectedFruit],
style: const TextStyle( style: const TextStyle(
fontSize: 22.0, fontSize: 22.0,
), ),
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:flutter_api_samples/cupertino/picker/cupertino_picker.0.dart' as example; import 'package:flutter_api_samples/cupertino/picker/cupertino_picker.0.dart' as example;
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
...@@ -14,12 +15,15 @@ void main() { ...@@ -14,12 +15,15 @@ void main() {
); );
// Open the Cupertino picker. // Open the Cupertino picker.
await tester.tap(find.text('Apple')); await tester.tap(find.widgetWithText(CupertinoButton, 'Apple'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// Test the initial item.
CupertinoPicker picker = tester.widget<CupertinoPicker>(find.byType(CupertinoPicker));
expect(picker.scrollController!.initialItem, 0);
// Drag the wheel to change fruit selection. // Drag the wheel to change fruit selection.
await tester.drag(find.text('Mango'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file await tester.drag(find.text('Mango'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file
await tester.pump(); await tester.pump();
await tester.pump(const Duration(milliseconds: 500)); await tester.pump(const Duration(milliseconds: 500));
...@@ -27,6 +31,13 @@ void main() { ...@@ -27,6 +31,13 @@ void main() {
await tester.tapAt(const Offset(1.0, 1.0)); await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.text('Banana'), findsOneWidget); expect(find.widgetWithText(CupertinoButton, 'Banana'), findsOneWidget);
// Test if the initial item has updated.
await tester.tap(find.widgetWithText(CupertinoButton, 'Banana'));
await tester.pumpAndSettle();
picker = tester.widget<CupertinoPicker>(find.byType(CupertinoPicker));
expect(picker.scrollController!.initialItem, 2);
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment