Skip to main content

Using Cupertino Widgets

1. Using CupertinoApp Widget

Property Values Description
home Widget Root widget of the app, displayed when the app is launched
routes Map<String, WidgetBuilder> Named routes for navigation
initialRoute String Route to display when the app is launched
onGenerateRoute Route<Widget> Function(RouteSettings) Function to generate routes dynamically
onUnknownRoute Route<Widget> Function(RouteSettings) Function to handle unknown routes
navigatorKey GlobalKey<NavigatorState> Key to access the Navigator state
navigatorObservers List<NavigatorObserver> Observers to listen to navigator events
title String App title, used for accessibility

Example: Basic CupertinoApp

import 'package:flutter/cupertino.dart';

void main() {
  runApp(
    CupertinoApp(
      home: CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: Text('My App'),
        ),
        child: Center(
          child: Text('Hello, World!'),
        ),
      ),
    ),
  );
}

2. Using CupertinoPageScaffold Widget

Property Values Description
navigationBar CupertinoNavigationBar Displays a navigation bar at the top of the page.
backgroundColor Color Background color of the page.
child Widget The main content of the page.
persistentFooter Widget A widget that is displayed at the bottom of the page and remains visible during scrolling.

Example: Basic CupertinoPageScaffold

import &#x27;package:flutter/cupertino.dart&#x27;;

class MyCupertinoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text(&#x27;My App&#x27;),
      ),
      child: Center(
        child: Text(&#x27;Hello, Cupertino!&#x27;),
      ),
    );
  }
}
Note: CupertinoPageScaffold is designed to mimic the iOS-style UI and provides a structured layout for Cupertino apps.

3. Using CupertinoNavigationBar Widget

Property Values Description
leading Widget? Widget to display on the left side of the navigation bar
middle Widget? Widget to display in the center of the navigation bar
trailing Widget? Widget to display on the right side of the navigation bar
backgroundColor Color? Background color of the navigation bar
border Border? Border to display around the navigation bar

Example: Basic CupertinoNavigationBar

import 'package:flutter/cupertino.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Home'),
      ),
      child: Center(
        child: Text('Hello, World!'),
      ),
    );
  }
}
Note: The CupertinoNavigationBar is designed to work with CupertinoPageScaffold and follows iOS design principles.

4. Using CupertinoTabScaffold and CupertinoTabBar

Property Values Description
backgroundColor Color Background color of the scaffold
controller CupertinoTabController Controls the currently selected tab
home Widget Widget to display in the first tab
items List<BottomNavigationBarItem> Tab bar items to display
onTap Function(int) Callback when a tab is tapped

Example: Basic CupertinoTabScaffold

import 'package:flutter/cupertino.dart';

class MyCupertinoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        items: [
          CupertinoTabBarItem(icon: Icon(CupertinoIcons.home), label: 'Home'),
          CupertinoTabBarItem(icon: Icon(CupertinoIcons.settings), label: 'Settings'),
        ],
      ),
      tabBuilder: (context, index) {
        return CupertinoTabView(
          builder: (context) {
            return index == 0
                ? CupertinoPageScaffold(
                    navigationBar: CupertinoNavigationBar(
                      middle: Text('Home'),
                    ),
                    child: Center(child: Text('Home Content')),
                  )
                : CupertinoPageScaffold(
                    navigationBar: CupertinoNavigationBar(
                      middle: Text('Settings'),
                    ),
                    child: Center(child: Text('Settings Content')),
                  );
          },
        );
      },
    );
  }
}
Note: CupertinoTabScaffold is designed to work with CupertinoTabBar and CupertinoTabView for native iOS-style tab navigation.

5. Using CupertinoButton Widget

Property Values Description
child Widget? The widget to display inside the button
onPressed VoidCallback? Callback when the button is pressed
padding EdgeInsetsGeometry? Padding around the button content
minSize double? Minimum size of the button
materialTapTargetSize MaterialTapTargetSize? Size of the tap target area
elevation double? Elevation of the button (iOS style)
color Color? Background color of the button
textStyle TextStyle? Text style for the button content

Example: Basic CupertinoButton

<CupertinoButton(
  child: Text("Tap Me"),
  onPressed: () {
    print("Button pressed!");
  },
)>

Example: CupertinoButton with Custom Color

<CupertinoButton(
  child: Text("Primary"),
  onPressed: () {
    Navigator.push(context, MaterialPageRoute(builder: (context) => MyPage()));
  },
  color: CupertinoColors.activeBlue,
)>
Note: CupertinoButton follows iOS design principles and provides a native look and feel on iOS devices.

6. Using CupertinoTextField Widget

Property Values Description
controller TextEditingController Controls the text being displayed and manages the text input.
keyboardType TextInputType.text, TextInputType.number, etc. Defines the type of keyboard to show (e.g., numeric, email).
obscureText true, false Whether to hide the text input (e.g., for passwords).
enabled true, false Whether the text field is interactive.
placeholder String Hint text displayed when the field is empty.
textInputAction TextInputAction.done, next, etc. Defines the action button on the keyboard (e.g., "Done", "Next").

Example: Basic CupertinoTextField

<CupertinoTextField
  placeholder: "Enter your name",
  controller: TextEditingController(),
  keyboardType: TextInputType.text,
/>

Example: Password Input with Obscure Text

<CupertinoTextField
  placeholder: "Enter password",
  obscureText: true,
  controller: TextEditingController(),
/>
Note: CupertinoTextField is designed to match iOS-style UI and behavior, providing a native look and feel on iOS devices.

7. Using CupertinoPicker Widget

The CupertinoPicker widget in Flutter provides a native iOS-style picker component that allows users to select from a list of items. It is commonly used in Cupertino (iOS-style) applications.

Property Description Values
children A list of widgets to display in the picker. List<Widget>
itemExtent The height of each item in the picker. double
onSelect Callback function triggered when an item is selected. Function(int index)
scrollController Controller to manage scrolling behavior. ScrollController

Example: Basic CupertinoPicker Usage

<CupertinoPicker
  itemExtent: 32.0,
  children: [
    Text('Option 1'),
    Text('Option 2'),
    Text('Option 3'),
  ],
  onSelect: (index) {
    print('Selected index: $index');
  },
/>
Note: The CupertinoPicker is designed to mimic the iOS picker style and is best used in Cupertino apps for consistency.

8. Using CupertinoDatePicker Widget

Property Values Description
initialDateTime DateTime The initially selected date/time.
onDateTimeChanged Function(DateTime) Callback when the date/time is changed.
dateOrder DatePickerDateOrder Defines the order of date components (e.g., MDY, DMY).
mode CupertinoDatePickerMode Specifies the mode: date, time, or date and time.
minimumDate DateTime Minimum selectable date/time.
maximumDate DateTime Maximum selectable date/time.
itemExtent double Height of each item in the picker.
use24hFormat bool Whether to use 24-hour format for time display.

Example: Basic CupertinoDatePicker

import 'package:flutter/cupertino.dart';

CupertinoDatePicker(
  initialDateTime: DateTime.now(),
  onDateTimeChanged: (DateTime newDateTime) {
    print('Selected date: $newDateTime');
  },
  mode: CupertinoDatePickerMode.date,
)

Example: Time Picker with 24h Format

CupertinoDatePicker(
  initialDateTime: DateTime.now(),
  onDateTimeChanged: (DateTime newDateTime) {
    print('Selected time: $newDateTime');
  },
  mode: CupertinoDatePickerMode.time,
  use24hFormat: true,
)
Note: CupertinoDatePicker is designed to match iOS-style UI and is best used in apps targeting iOS or with an iOS-like design language.

9. Using CupertinoAlertDialog Widget

Property Values Description
title Widget? Title of the dialog, displayed at the top
content Widget? Main content of the dialog, usually a text or list
actions List<Widget> Buttons to show at the bottom of the dialog
barrierDismissible bool If true, tapping outside the dialog dismisses it

Example: Basic CupertinoAlertDialog

<CupertinoAlertDialog
  title: Text('Alert Title'),
  content: Text('This is the alert content.'),
  actions: [
    CupertinoDialogAction(
      child: Text('Cancel'),
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),
    CupertinoDialogAction(
      child: Text('OK'),
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),
  ],
)>
Note: CupertinoAlertDialog is designed to match iOS-style alerts and should be used in apps targeting iOS or with an iOS-like UI.

10. Using CupertinoActionSheet Widget

The CupertinoActionSheet widget in Flutter provides a native iOS-style action sheet that can be used to present a set of options or actions to the user.

Property Description Values
title Optional title for the action sheet Widget?
message Optional message or description for the action sheet Widget?
actions List of actions to display in the action sheet List<Widget>
cancelAction Optional cancel action button CupertinoActionSheetAction?
backgroundColor Background color of the action sheet Color?

Example: Basic CupertinoActionSheet

showCupertinoModalPopup(
  context: context,
  builder: (BuildContext context) {
    return CupertinoActionSheet(
      title: Text('Select an option'),
      message: Text('Choose one of the following actions'),
      actions: [
        CupertinoActionSheetAction(
          child: Text('Option 1'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        CupertinoActionSheetAction(
          child: Text('Option 2'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ],
      cancelAction: CupertinoActionSheetAction(
        child: Text('Cancel'),
        onPressed: () {
          Navigator.pop(context);
        },
      ),
    );
  },
);
Note: The CupertinoActionSheet is designed to be used with showCupertinoModalPopup for a modal presentation.

11. Using CupertinoSwitch and CupertinoSlider

Property Values Description
activeColor Color Color of the thumb when the switch is active.
inactiveColor Color Color of the track when the switch is inactive.
value bool Whether the switch is currently on or off.
onChanged Function(bool?) Called when the user toggles the switch.
trackColor MaterialStateProperty<Color?> Customizes the color of the track.
thumbColor MaterialStateProperty<Color?> Customizes the color of the thumb.

Example: CupertinoSwitch

import 'package:flutter/cupertino.dart';

CupertinoSwitch(
  value: isSwitched,
  onChanged: (bool value) {
    setState(() {
      isSwitched = value;
    });
  },
  activeColor: CupertinoColors.activeBlue,
  inactiveColor: CupertinoColors.inactiveGray,
)
Property Values Description
min double The minimum value the slider can take.
max double The maximum value the slider can take.
value double The current value of the slider.
onChanged Function(double?) Called when the user changes the slider value.
divisions int Number of discrete divisions for the slider.
activeColor Color The color of the active portion of the slider.

Example: CupertinoSlider

import 'package:flutter/cupertino.dart';

CupertinoSlider(
  min: 0.0,
  max: 100.0,
  value: sliderValue,
  divisions: 100,
  onChangeEnd: (double value) {
    setState(() {
      sliderValue = value;
    });
  },
  activeColor: CupertinoColors.activeBlue,
)

12. Using CupertinoContextMenu Widget

The CupertinoContextMenu widget in Flutter provides a native iOS-style context menu for widgets. It allows users to perform actions on a selected item by long-pressing or tapping with two fingers.

Property Description Values
child The widget that will show the context menu when long-pressed. Widget
actions A list of CupertinoContextMenuAction widgets to display in the menu. List<CupertinoContextMenuAction>
builder A builder function to customize the appearance of the context menu. WidgetBuilder

Example: Basic CupertinoContextMenu Usage

<CupertinoContextMenu
  child: Container(
    height: 100,
    width: 200,
    color: CupertinoColors.systemBlue,
    child: Center(
      child: Text("Long press me"),
    ),
  ),
  actions: [
    CupertinoContextMenuAction(
      child: Text("Share"),
      onPressed: () {
        print("Share action tapped");
      },
    ),
    CupertinoContextMenuAction(
      child: Text("Delete"),
      onPressed: () {
        print("Delete action tapped");
      },
    ),
  ],
)>
Note: The CupertinoContextMenu is designed to work with iOS-style UI and should be used in apps targeting iOS or apps that aim for a Cupertino design language.

Was this article helpful?