Created
March 5, 2021 09:06
-
-
Save nhancv/d0cfe39a43b5c7baa6ecdef24bb52e47 to your computer and use it in GitHub Desktop.
Flutter time picker
This file contains 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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:nft/services/safety/base_stateful.dart'; | |
import 'package:nft/utils/app_extension.dart'; | |
import 'package:nft/utils/app_style.dart'; | |
import 'package:nft/widgets/w_bottom_action_sheet.dart'; | |
import 'package:nft/widgets/w_button_inkwell.dart'; | |
import 'package:nft/widgets/w_divider_line.dart'; | |
/// Use: final Duration duration = await WTimePicker.showPicker(context); | |
class WTimePicker extends StatefulWidget { | |
const WTimePicker({Key key, this.initDuration}) : super(key: key); | |
final Duration initDuration; | |
static Future<Duration> showPicker(BuildContext context, | |
{Duration initDuration}) { | |
return showCupertinoModalPopup<Duration>( | |
context: context, | |
builder: (BuildContext context) { | |
return WTimePicker( | |
initDuration: initDuration, | |
); | |
}); | |
} | |
@override | |
_WTimePickerState createState() => _WTimePickerState(); | |
} | |
class _WTimePickerState extends BaseStateful<WTimePicker> { | |
Duration selected = const Duration(minutes: 1); | |
@override | |
Widget build(BuildContext context) { | |
super.build(context); | |
final bool isTimeValid = selected > Duration.zero; | |
return WBottomActionSheet( | |
body: Container( | |
child: Column( | |
children: <Widget>[ | |
Container( | |
height: 50, | |
alignment: Alignment.center, | |
child: Text( | |
'Pick a duration', | |
style: normalTextStyle(17.SP, color: const Color(0xFF8F8F8F)), | |
), | |
), | |
const WDividerLine(), | |
Container( | |
child: CupertinoTimerPicker( | |
initialTimerDuration: widget.initDuration ?? selected, | |
mode: CupertinoTimerPickerMode.hm, | |
onTimerDurationChanged: (Duration value) { | |
HapticFeedback.selectionClick(); | |
setState(() { | |
selected = value; | |
}); | |
}, | |
), | |
), | |
const WDividerLine(), | |
WButtonInkwell( | |
child: Container( | |
height: 57, | |
alignment: Alignment.center, | |
child: Text( | |
'Confirm', | |
style: normalTextStyle(17.SP, | |
color: const Color(0xFF0080FA) | |
.withOpacity(isTimeValid ? 1 : 0.4)), | |
), | |
), | |
onPressed: isTimeValid | |
? () { | |
Navigator.of(context).pop(selected); | |
} | |
: null, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment