CupertinoPicker class
An iOS-styled picker.
Displays its children widgets on a wheel for selection and calls back when the currently selected item changes.
By default, the first child in children will be the initially selected child.
The index of a different child can be specified in scrollController, to make
that child the initially selected child.
Can be used with showCupertinoModalPopup to display the picker modally at the
bottom of the screen. When calling showCupertinoModalPopup, be sure to set
semanticsDismissible to true to enable dismissing the modal via semantics.
Sizes itself to its parent. All children are sized to the same size based on itemExtent.
By default, descendent texts are shown with CupertinoTextThemeData.pickerTextStyle.
This example shows a CupertinoPicker that displays a list of fruits on a wheel for selection.
To see it in action, copy and run this code snippet on DartPad.
import 'package:cupertino_ui/cupertino_ui.dart';
/// Flutter code sample for [CupertinoPicker].
const double _kItemExtent = 32.0;
const List<String> _fruitNames = <String>[
'Apple',
'Mango',
'Banana',
'Orange',
'Pineapple',
'Strawberry',
];
void main() => runApp(const CupertinoPickerApp());
class CupertinoPickerApp extends StatelessWidget {
const CupertinoPickerApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
theme: CupertinoThemeData(brightness: .light),
home: CupertinoPickerExample(),
);
}
}
class CupertinoPickerExample extends StatefulWidget {
const CupertinoPickerExample({super.key});
@override
State<CupertinoPickerExample> createState() => _CupertinoPickerExampleState();
}
class _CupertinoPickerExampleState extends State<CupertinoPickerExample> {
int _selectedFruit = 0;
// This shows a CupertinoModalPopup with a reasonable fixed height which hosts CupertinoPicker.
void _showDialog(Widget child) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => Container(
height: 216,
padding: const .only(top: 6.0),
// The Bottom margin is provided to align the popup above the system navigation bar.
margin: .only(bottom: MediaQuery.of(context).viewInsets.bottom),
// Provide a background color for the popup.
color: CupertinoColors.systemBackground.resolveFrom(context),
// Use a SafeArea widget to avoid system overlaps.
child: SafeArea(top: false, child: child),
),
);
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('CupertinoPicker Sample'),
),
child: DefaultTextStyle(
style: TextStyle(
color: CupertinoColors.label.resolveFrom(context),
fontSize: 22.0,
),
child: Center(
child: Row(
mainAxisAlignment: .center,
children: <Widget>[
const Text('Selected fruit: '),
CupertinoButton(
padding: .zero,
// Display a CupertinoPicker with list of fruits.
onPressed: () => _showDialog(
CupertinoPicker(
magnification: 1.22,
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: [
for (final String fruitName in _fruitNames)
Center(child: Text(fruitName)),
],
),
),
// This displays the selected fruit name.
child: Text(
_fruitNames[_selectedFruit],
style: const TextStyle(fontSize: 22.0),
),
),
],
),
),
),
);
}
}
See also:
- ListWheelScrollView, the generic widget backing this picker without the iOS design specific chrome.
- developer.apple.com/design/human-interface-guidelines/pickers/
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- CupertinoPicker
Constructors
-
CupertinoPicker({Key? key, double diameterRatio = _kDefaultDiameterRatio, Color? backgroundColor, double offAxisFraction = 0.0, bool useMagnifier = false, double magnification = 1.0, FixedExtentScrollController? scrollController, double squeeze = _kSqueeze, ChangeReportingBehavior changeReportingBehavior = ChangeReportingBehavior.onScrollUpdate, required double itemExtent, required ValueChanged<
int> ? onSelectedItemChanged, required List<Widget> children, Widget? selectionOverlay = const CupertinoPickerDefaultSelectionOverlay(), bool looping = false}) - Creates a picker from a concrete list of children.
-
CupertinoPicker.builder({Key? key, double diameterRatio = _kDefaultDiameterRatio, Color? backgroundColor, double offAxisFraction = 0.0, bool useMagnifier = false, double magnification = 1.0, FixedExtentScrollController? scrollController, double squeeze = _kSqueeze, ChangeReportingBehavior changeReportingBehavior = ChangeReportingBehavior.onScrollUpdate, required double itemExtent, required ValueChanged<
int> ? onSelectedItemChanged, required NullableIndexedWidgetBuilder itemBuilder, int? childCount, Widget? selectionOverlay = const CupertinoPickerDefaultSelectionOverlay()}) - Creates a picker from an IndexedWidgetBuilder callback where the builder is dynamically invoked during layout.
Properties
- backgroundColor → Color?
-
Background color behind the children.
final
- changeReportingBehavior → ChangeReportingBehavior
-
The behavior of reporting the selected item index.
final
- childDelegate → ListWheelChildDelegate
-
A delegate that lazily instantiates children.
final
- diameterRatio → double
-
Relative ratio between this picker's height and the simulated cylinder's diameter.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- itemExtent → double
-
The uniform height of all children.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- magnification → double
-
The zoomed-in rate of the magnifier, if it is used.
final
- offAxisFraction → double
-
How much the wheel is horizontally off-center, as a fraction of its width.
This property creates the visual effect of looking at a vertical wheel from
its side where its vanishing points at the edge curves to one side instead
of looking at the wheel head-on.
final
-
onSelectedItemChanged
→ ValueChanged<
int> ? -
Called when the selected item changes.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scrollController → FixedExtentScrollController?
-
A FixedExtentScrollController to read and control the current item, and
to set the initial item.
final
- selectionOverlay → Widget?
-
A widget overlaid on the picker to highlight the currently selected entry.
final
- squeeze → double
-
The angular compactness of the children on the wheel.
final
- useMagnifier → bool
-
Whether to use the magnifier for the center item of the wheel.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< StatefulWidget> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited