|
| 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 | +} |
0 commit comments