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 of what we are going to do in this article.
Demo Video:
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.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step-by-Step Implementation
Step 1: Create a new Flutter Application
Create a new Flutter application using the command Prompt. To create a new app, write the following command and run it.
flutter create app_nameTo know more about it refer this article: 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.
void main() {
runApp(MyApp());
}
Step 4: Create MyApp Class
In this class, we are going to implement the CupertinoApp, where we can also set the Theme of our App. Here we call the MyPickerPage class, where the CupertinoPicker Widget is implemented.
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 helps 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),
),
);
}),
),
class MyPickerPage extends StatefulWidget {
@override
_MyPickerPageState createState() => _MyPickerPageState();
}
class _MyPickerPageState extends State<MyPickerPage> {
// List of items for the picker
List<String> items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
// Selected item index
int selectedItemIndex = 0;
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
// App bar with title
navigationBar: CupertinoNavigationBar(
middle: Text('Cupertino Picker Example'),
),
// Main content
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display "Selected Item:" text
Text(
'Selected Item:',
style: TextStyle(fontSize: 20.0),
),
SizedBox(height: 20.0), // Spacer
// CupertinoPicker widget for item selection
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),
),
);
}),
),
SizedBox(height: 20.0), // Spacer
// Display the selected item
Text(
'Selected: ${items[selectedItemIndex]}',
style: TextStyle(fontSize: 20.0),
),
],
),
),
);
}
}
Complete Source Code
main.dart:
// Importing Cupertino widget library for iOS-style widgets
import 'package:flutter/cupertino.dart';
// Entry point of the Flutter application
void main() {
runApp(MyApp()); // Launches the MyApp widget
}
// MyApp is the root widget of the application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
debugShowCheckedModeBanner: false, // Hides the debug banner
home: MyPickerPage(), // Sets MyPickerPage as the home screen
);
}
}
// A StatefulWidget to manage picker state
class MyPickerPage extends StatefulWidget {
@override
_MyPickerPageState createState() => _MyPickerPageState(); // Creates mutable state
}
// State class for MyPickerPage
class _MyPickerPageState extends State<MyPickerPage> {
// A list of strings to display in the CupertinoPicker
List<String> items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
// Index to keep track of the currently selected item
int selectedItemIndex = 0;
@override
Widget build(BuildContext context) {
// Returns a page layout that mimics iOS design
return CupertinoPageScaffold(
// Top app bar with centered title
navigationBar: CupertinoNavigationBar(
middle: Text('Cupertino Picker Example'), // Title of the app bar
),
// Body content of the page
child: Center(
// Center the content vertically and horizontally
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Vertically center the column
children: <Widget>[
// Static text label above the picker
Text(
'Selected Item:',
style: TextStyle(fontSize: 20.0), // Font size of the label
),
SizedBox(height: 20.0), // Adds vertical spacing between elements
// CupertinoPicker allows iOS-style item picking
CupertinoPicker(
itemExtent: 40.0, // Height of each item in the picker
onSelectedItemChanged: (int index) {
// Called when user scrolls to a new item
setState(() {
selectedItemIndex = index; // Update the selected item index
});
},
// Generate picker items dynamically using the items list
children: List<Widget>.generate(items.length, (int index) {
return Center(
child: Text(
items[index], // Display the item text
style: TextStyle(fontSize: 24.0), // Font size for picker item
),
);
}),
),
SizedBox(height: 20.0), // Adds spacing below the picker
// Displays the currently selected item
Text(
'Selected: ${items[selectedItemIndex]}', // Shows which item is selected
style: TextStyle(fontSize: 20.0), // Style for selected item text
),
],
),
),
);
}
}