[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CupertinoPicker example #118248

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions examples/api/lib/cupertino/picker/cupertino_picker.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CupertinoPickerApp extends StatelessWidget {
);
}
}

class CupertinoPickerExample extends StatefulWidget {
const CupertinoPickerExample({super.key});

Expand Down Expand Up @@ -57,7 +58,7 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
top: false,
child: child,
),
)
),
);
}

Expand Down Expand Up @@ -86,23 +87,24 @@ class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
squeeze: 1.2,
useMagnifier: true,
itemExtent: _kItemExtent,
// This sets the initial item.
scrollController: FixedExtentScrollController(
initialItem: _selectedFruit,
),
// This is called when selected item is changed.
onSelectedItemChanged: (int selectedItem) {
setState(() {
_selectedFruit = selectedItem;
});
},
children: List<Widget>.generate(_fruitNames.length, (int index) {
return Center(
child: Text(
_fruitNames[index],
),
);
return Center(child: Text(_fruitNames[index]));
}),
),
),
// This displays the selected fruit name.
child: Text(_fruitNames[_selectedFruit],
child: Text(
_fruitNames[_selectedFruit],
style: const TextStyle(
fontSize: 22.0,
),
Expand Down
17 changes: 14 additions & 3 deletions examples/api/test/cupertino/picker/cupertino_picker.0_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// 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_test/flutter_test.dart';

Expand All @@ -14,19 +15,29 @@ void main() {
);

// Open the Cupertino picker.
await tester.tap(find.text('Apple'));
await tester.tap(find.widgetWithText(CupertinoButton, 'Apple'));
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.
await tester.drag(find.text('Mango'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file

await tester.pump();
await tester.pump(const Duration(milliseconds: 500));

// Close the Cupertino picker.
await tester.tapAt(const Offset(1.0, 1.0));
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);
});
}