Skip to content

Commit fc71ec5

Browse files
authored
Add CupertinoTimerPicker Interactive Example (flutter#93621)
1 parent 120d25f commit fc71ec5

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flutter code sample for CupertinoTimerPicker
6+
7+
import 'package:flutter/cupertino.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({Key? key}) : super(key: key);
13+
14+
static const String _title = 'CupertinoTimerPicker Sample';
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return const CupertinoApp(
19+
title: _title,
20+
home: CupertinoTimerPickerSample(),
21+
);
22+
}
23+
}
24+
25+
class CupertinoTimerPickerSample extends StatefulWidget {
26+
const CupertinoTimerPickerSample({Key? key}) : super(key: key);
27+
28+
@override
29+
State<CupertinoTimerPickerSample> createState() => _CupertinoTimerPickerSampleState();
30+
}
31+
32+
class _CupertinoTimerPickerSampleState extends State<CupertinoTimerPickerSample> {
33+
Duration duration = const Duration(hours: 1, minutes: 23);
34+
35+
// This shows a CupertinoModalPopup with a reasonable fixed height which hosts CupertinoTimerPicker.
36+
void _showDialog(Widget child) {
37+
showCupertinoModalPopup<void>(
38+
context: context,
39+
builder: (BuildContext context) => Container(
40+
height: 216,
41+
padding: const EdgeInsets.only(top: 6.0),
42+
// The Bottom margin is provided to align the popup above the system navigation bar.
43+
margin: EdgeInsets.only(
44+
bottom: MediaQuery.of(context).viewInsets.bottom,
45+
),
46+
// Provide a background color for the popup.
47+
color: CupertinoColors.systemBackground.resolveFrom(context),
48+
// Use a SafeArea widget to avoid system overlaps.
49+
child: SafeArea(
50+
top: false,
51+
child: child,
52+
),
53+
)
54+
);
55+
}
56+
57+
@override
58+
Widget build(BuildContext context) {
59+
return CupertinoPageScaffold(
60+
child: DefaultTextStyle(
61+
style: TextStyle(
62+
color: CupertinoColors.label.resolveFrom(context),
63+
fontSize: 22.0,
64+
),
65+
child: Center(
66+
child: Column(
67+
mainAxisAlignment: MainAxisAlignment.center,
68+
children: <Widget>[
69+
_TimerPickerItem(
70+
children: <Widget>[
71+
const Text('Timer'),
72+
CupertinoButton(
73+
// Display a CupertinoTimerPicker with hour/minute mode.
74+
onPressed: () => _showDialog(
75+
CupertinoTimerPicker(
76+
mode: CupertinoTimerPickerMode.hm,
77+
initialTimerDuration: duration,
78+
// This is called when the user changes the timer duration.
79+
onTimerDurationChanged: (Duration newDuration) {
80+
setState(() => duration = newDuration);
81+
},
82+
),
83+
),
84+
// In this example, the timer value is formatted manually. You can use intl package
85+
// to format the value based on user's locale settings.
86+
child: Text('$duration',
87+
style: const TextStyle(
88+
fontSize: 22.0,
89+
),
90+
),
91+
),
92+
],
93+
),
94+
],
95+
),
96+
),
97+
),
98+
);
99+
}
100+
}
101+
102+
// This class simply decorates a row of widgets.
103+
class _TimerPickerItem extends StatelessWidget {
104+
const _TimerPickerItem({required this.children});
105+
106+
final List<Widget> children;
107+
108+
@override
109+
Widget build(BuildContext context) {
110+
return DecoratedBox(
111+
decoration: const BoxDecoration(
112+
border: Border(
113+
top: BorderSide(
114+
color: CupertinoColors.inactiveGray,
115+
width: 0.0,
116+
),
117+
bottom: BorderSide(
118+
color: CupertinoColors.inactiveGray,
119+
width: 0.0,
120+
),
121+
),
122+
),
123+
child: Padding(
124+
padding: const EdgeInsets.symmetric(horizontal: 16.0),
125+
child: Row(
126+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
127+
children: children,
128+
),
129+
),
130+
);
131+
}
132+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_api_samples/cupertino/date_picker/cupertino_timer_picker.0.dart' as example;
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
const Offset _kRowOffset = Offset(0.0, -50.0);
9+
10+
void main() {
11+
testWidgets('Can pick a duration from CupertinoTimerPicker', (WidgetTester tester) async {
12+
await tester.pumpWidget(
13+
const example.MyApp(),
14+
);
15+
16+
// Launch the timer picker.
17+
await tester.tap(find.text('1:23:00.000000'));
18+
await tester.pumpAndSettle();
19+
20+
// Drag hour, minute to change the time.
21+
await tester.drag(find.text('1'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file
22+
await tester.drag(find.text('23'), _kRowOffset, touchSlopY: 0, warnIfMissed: false); // see top of file
23+
24+
await tester.pump();
25+
await tester.pump(const Duration(milliseconds: 500));
26+
27+
// Close the timer picker.
28+
await tester.tapAt(const Offset(1.0, 1.0));
29+
await tester.pumpAndSettle();
30+
31+
expect(find.text('3:25:00.000000'), findsOneWidget);
32+
});
33+
}

packages/flutter/lib/src/cupertino/date_picker.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,12 @@ enum CupertinoTimerPickerMode {
14961496
/// provides more space than it needs, the picker will position itself according
14971497
/// to its [alignment] property.
14981498
///
1499+
/// {@tool dartpad}
1500+
/// This example shows a [CupertinoTimerPicker] that returns a countdown duration.
1501+
///
1502+
/// ** See code in examples/api/lib/cupertino/date_picker/cupertino_timer_picker.0.dart **
1503+
/// {@end-tool}
1504+
///
14991505
/// See also:
15001506
///
15011507
/// * [CupertinoDatePicker], the class that implements different display modes

0 commit comments

Comments
 (0)