Created
March 19, 2019 12:23
-
-
Save friebetill/8afc5523962461a42d52eb220c2bd397 to your computer and use it in GitHub Desktop.
ShowTimePicker Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'ShowTimePicker Demo', | |
home: MyHomePage(title: 'ShowTimePicker Demo Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
TimeOfDay _time; | |
void _handleEditTime() async { | |
final selectedTime = await showTimePicker( | |
initialTime: TimeOfDay.now(), | |
context: context, | |
); | |
if (selectedTime == null) return; | |
setState(() { | |
_time = selectedTime; | |
}); | |
} | |
Text _buildTimeText() { | |
String _addLeadingZeroIfNeeded(int value) { | |
if (value < 10) return '0$value'; | |
return value.toString(); | |
} | |
if (_time == null) return Text('No time selected'); | |
return Text( | |
'Your selected time is: ${_addLeadingZeroIfNeeded(_time.hour)}:${_addLeadingZeroIfNeeded(_time.minute)}'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: _buildTimeText(), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _handleEditTime, | |
tooltip: 'Select time', | |
child: Icon(Icons.edit), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment