Sub topic 4
Cupertino Buttons, Switches, Sliders and Text Fields
This chapter assumes basic Dart syntax, StatefulWidget, setState, BuildContext, and familiarity with CupertinoApp and CupertinoPageScaffold. Cupertino controls are Flutter widgets that follow Apple’s visual style and interaction conventions. Their APIs resemble Material controls, but their spacing, typography, gestures, and feedback differ.
Choosing the right control
CupertinoButtonperforms an action, such as saving or opening a page.CupertinoSwitchrepresents an immediate on-or-off setting.CupertinoSliderselects one value from a continuous or divided range.CupertinoTextFieldaccepts text input.
Keep the state in the parent widget. The control receives its current value through a property and reports user changes through a callback.
import 'package:flutter/cupertino.dart';
void main() {
runApp(const SettingsApp());
}
class SettingsApp extends StatelessWidget {
const SettingsApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
home: SettingsPage(),
);
}
}
class SettingsPage extends StatefulWidget {
const SettingsPage({super.key});
@override
State<SettingsPage> createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
final _nameController = TextEditingController();
bool _notificationsEnabled = true;
double _volume = 0.6;
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
void _save() {
final name = _nameController.text.trim();
debugPrint('Saved $name, volume $_volume');
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Settings'),
),
child: SafeArea(
child: ListView(
padding: const EdgeInsets.all(20),
children: [
const Text('Display name'),
const SizedBox(height: 8),
CupertinoTextField(
controller: _nameController,
placeholder: 'Enter your name',
textInputAction: TextInputAction.done,
),
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Notifications'),
CupertinoSwitch(
value: _notificationsEnabled,
onChanged: (value) {
setState(() => _notificationsEnabled = value);
},
),
],
),
const SizedBox(height: 24),
Text('Volume: ${(_volume * 100).round()}%'),
CupertinoSlider(
value: _volume,
min: 0,
max: 1,
divisions: 10,
onChanged: (value) {
setState(() => _volume = value);
},
),
const SizedBox(height: 24),
CupertinoButton.filled(
onPressed: _save,
child: const Text('Save'),
),
],
),
),
);
}
}The switch and slider are controlled widgets. Their values live in _SettingsPageState, so rebuilding the page preserves the selected values. divisions: 10 turns the slider into ten equal steps. Omit divisions when continuous movement is appropriate.
Common errors
A switch or slider that does not update usually has a missing setState call, or its callback updates a different variable than the one passed to value.
A disabled button has onPressed: null. Use this deliberately when an action cannot currently succeed. Do not use an empty callback to simulate disabled behavior, because an empty callback still allows interaction.
A text field controller owns editable text. Dispose of it in dispose when a state object created it. If you only need the submitted value, you can instead use onChanged and store a string.
Control data flow
Exercise
Why does the slider need both a value property and an onChanged callback?
Show answer
The value property displays the current state, while onChanged reports a proposed new value so the parent can update that state.