CupertinoTimerPicker class
A countdown timer picker in iOS style.
This picker shows a countdown duration with hour, minute and second spinners. The duration is bound between 0 and 23 hours 59 minutes 59 seconds.
There are several modes of the timer picker listed in CupertinoTimerPickerMode.
The picker has a fixed size of 320 x 216, in logical pixels, with the exception of CupertinoTimerPickerMode.hms, which is 330 x 216. If the parent widget provides more space than it needs, the picker will position itself according to its alignment property.
This example shows a CupertinoTimerPicker that returns a countdown duration.
To see it in action, copy and run this code snippet on DartPad.
import 'package:cupertino_ui/cupertino_ui.dart';
/// Flutter code sample for [CupertinoTimerPicker].
void main() => runApp(const TimerPickerApp());
class TimerPickerApp extends StatelessWidget {
const TimerPickerApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
theme: CupertinoThemeData(brightness: .light),
home: TimerPickerExample(),
);
}
}
class TimerPickerExample extends StatefulWidget {
const TimerPickerExample({super.key});
@override
State<TimerPickerExample> createState() => _TimerPickerExampleState();
}
class _TimerPickerExampleState extends State<TimerPickerExample> {
Duration duration = const Duration(hours: 1, minutes: 23);
// This shows a CupertinoModalPopup with a reasonable fixed height which hosts
// a CupertinoTimerPicker.
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('CupertinoTimerPicker Sample'),
),
child: DefaultTextStyle(
style: TextStyle(
color: CupertinoColors.label.resolveFrom(context),
fontSize: 22.0,
),
child: Center(
child: Column(
mainAxisAlignment: .center,
children: <Widget>[
_TimerPickerItem(
children: <Widget>[
const Text('Timer'),
CupertinoButton(
// Display a CupertinoTimerPicker with hour/minute mode.
onPressed: () => _showDialog(
CupertinoTimerPicker(
mode: .hm,
initialTimerDuration: duration,
// This is called when the user changes the timer's
// duration.
onTimerDurationChanged: (Duration newDuration) {
setState(() => duration = newDuration);
},
),
),
// In this example, the timer's value is formatted manually.
// You can use the intl package to format the value based on
// the user's locale settings.
child: Text(
'$duration',
style: const TextStyle(fontSize: 22.0),
),
),
],
),
],
),
),
),
);
}
}
// This class simply decorates a row of widgets.
class _TimerPickerItem extends StatelessWidget {
const _TimerPickerItem({required this.children});
final List<Widget> children;
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(color: CupertinoColors.inactiveGray, width: 0.0),
bottom: BorderSide(color: CupertinoColors.inactiveGray, width: 0.0),
),
),
child: Padding(
padding: const .symmetric(horizontal: 16.0),
child: Row(mainAxisAlignment: .spaceBetween, children: children),
),
);
}
}
See also:
- CupertinoDatePicker, the class that implements different display modes of the iOS-style date picker.
- CupertinoPicker, the class that implements a content agnostic spinner UI.
- developer.apple.com/design/human-interface-guidelines/ios/controls/pickers/
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- CupertinoTimerPicker
Constructors
-
CupertinoTimerPicker({Key? key, CupertinoTimerPickerMode mode = CupertinoTimerPickerMode.hms, Duration initialTimerDuration = Duration.zero, int minuteInterval = 1, int secondInterval = 1, AlignmentGeometry alignment = Alignment.center, Color? backgroundColor, double itemExtent = _kItemExtent, required ValueChanged<
Duration> onTimerDurationChanged, ChangeReportingBehavior changeReportingBehavior = ChangeReportingBehavior.onScrollUpdate, SelectionOverlayBuilder? selectionOverlayBuilder}) - Constructs an iOS style countdown timer picker.
Properties
- alignment → AlignmentGeometry
-
Defines how the timer picker should be positioned within its parent.
final
- backgroundColor → Color?
-
Background color of timer picker.
final
- changeReportingBehavior → ChangeReportingBehavior
-
The behavior of reporting the selected duration.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialTimerDuration → Duration
-
The initial duration of the countdown timer.
final
- itemExtent → double
-
The uniform height of all children.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- minuteInterval → int
-
The granularity of the minute spinner. Must be a positive integer factor
of 60.
final
- mode → CupertinoTimerPickerMode
-
The mode of the timer picker.
final
-
onTimerDurationChanged
→ ValueChanged<
Duration> -
Callback called when the timer duration changes.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- secondInterval → int
-
The granularity of the second spinner. Must be a positive integer factor
of 60.
final
- selectionOverlayBuilder → SelectionOverlayBuilder?
-
A function that returns a widget that is overlaid on the picker
to highlight the currently selected entry.
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