Flutter – CupertinoPicker Widget
Last Updated :
06 Nov, 2023
The CupertinoPicker widget in Flutter is part of the Cupertino-themed widgets that mimic the design and behavior of iOS widgets. It’s specifically designed to create a scrollable list of items, allowing the user to select one item from the list by scrolling or picking it directly. In this article, we are going to implement the CupertinoPicker widget. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of CupertinoPicker Widget
CupertinoPicker(
itemExtent: 40.0, // Height of each item in the picker
onSelectedItemChanged: (int index) {
// Callback function when an item is selected
// Use 'index' to determine which item was selected
},
children: <Widget>[
// List of widgets representing each item in the picker
Center(
child: Text('Item 1'),
),
Center(
child: Text('Item 2'),
),
// Add more items as needed
],
)
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/cupertino.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
|
Step 4: Create MyApp Class
In this class we are going to implement the CupertinoApp , here we can also set the Theme of our App.Here we call MyPickerPage class where the CupertinoPicker Widget is implemented .
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
debugShowCheckedModeBanner: false ,
home: MyPickerPage(),
);
}
}
|
Step 5: Create MyPickerPage Class
In this class we are going to Implement the CupertinoPicker widget that help to create a scrollable list of items, allowing the user to select one item from the list by scrolling or picking it directly and the selected item is displayed using a TextView. Comments are added for better understanding.
CupertinoPicker(
itemExtent: 40.0,
onSelectedItemChanged: (int index) {
// Update the selected item index when an item is changed
setState(() {
selectedItemIndex = index;
});
},
children: List<Widget>.generate(items.length, (int index) {
// Generate the picker items based on the 'items' list
return Center(
child: Text(
items[index],
style: TextStyle(fontSize: 24.0),
),
);
}),
),
Dart
class MyPickerPage extends StatefulWidget {
@override
_MyPickerPageState createState() => _MyPickerPageState();
}
class _MyPickerPageState extends State<MyPickerPage> {
List<String> items = [ 'Item 1' , 'Item 2' , 'Item 3' , 'Item 4' , 'Item 5' ];
int selectedItemIndex = 0;
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text( 'Cupertino Picker Example' ),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Selected Item:' ,
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 20.0),
CupertinoPicker(
itemExtent: 40.0,
onSelectedItemChanged: ( int index) {
setState(() {
selectedItemIndex = index;
});
},
children: List<Widget>.generate(items.length, ( int index) {
return Center(
child: Text(
items[index],
style: TextStyle(fontSize: 24.0),
),
);
}),
),
SizedBox(height: 20.0),
Text(
'Selected: ${items[selectedItemIndex]}' ,
style: TextStyle(fontSize: 20.0),
),
],
),
),
);
}
}
|
Here is the full Code of main.dart file
Dart
import 'package:flutter/cupertino.dart' ;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
debugShowCheckedModeBanner: false ,
home: MyPickerPage(),
);
}
}
class MyPickerPage extends StatefulWidget {
@override
_MyPickerPageState createState() => _MyPickerPageState();
}
class _MyPickerPageState extends State<MyPickerPage> {
List<String> items = [ 'Item 1' , 'Item 2' , 'Item 3' , 'Item 4' , 'Item 5' ];
int selectedItemIndex = 0;
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text( 'Cupertino Picker Example' ),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Selected Item:' ,
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 20.0),
CupertinoPicker(
itemExtent: 40.0,
onSelectedItemChanged: ( int index) {
setState(() {
selectedItemIndex = index;
});
},
children: List<Widget>.generate(items.length, ( int index) {
return Center(
child: Text(
items[index],
style: TextStyle(fontSize: 24.0),
),
);
}),
),
SizedBox(height: 20.0),
Text(
'Selected: ${items[selectedItemIndex]}' ,
style: TextStyle(fontSize: 20.0),
),
],
),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...