|
| 1 | +import 'package:catalyst_voices/common/ext/time_of_day_ext.dart'; |
| 2 | +import 'package:catalyst_voices_brands/catalyst_voices_brands.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +class VoicesTimePicker extends StatefulWidget { |
| 6 | + final ValueChanged<TimeOfDay> onTap; |
| 7 | + final TimeOfDay? selectedTime; |
| 8 | + final String? timeZone; |
| 9 | + |
| 10 | + const VoicesTimePicker({ |
| 11 | + super.key, |
| 12 | + required this.onTap, |
| 13 | + this.selectedTime, |
| 14 | + this.timeZone, |
| 15 | + }); |
| 16 | + |
| 17 | + @override |
| 18 | + State<VoicesTimePicker> createState() => _VoicesTimePickerState(); |
| 19 | +} |
| 20 | + |
| 21 | +class _VoicesTimePickerState extends State<VoicesTimePicker> { |
| 22 | + late final ScrollController _scrollController; |
| 23 | + late final List<TimeOfDay> _timeList; |
| 24 | + |
| 25 | + final double itemExtent = 40; |
| 26 | + |
| 27 | + @override |
| 28 | + void initState() { |
| 29 | + super.initState(); |
| 30 | + |
| 31 | + _timeList = _generateTimeList(); |
| 32 | + _scrollController = ScrollController(); |
| 33 | + |
| 34 | + final initialSelection = widget.selectedTime; |
| 35 | + if (initialSelection != null) { |
| 36 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 37 | + _scrollToTime(initialSelection); |
| 38 | + }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + void dispose() { |
| 44 | + _scrollController.dispose(); |
| 45 | + super.dispose(); |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + Widget build(BuildContext context) { |
| 50 | + return Material( |
| 51 | + type: MaterialType.transparency, |
| 52 | + clipBehavior: Clip.hardEdge, |
| 53 | + child: Container( |
| 54 | + height: 350, |
| 55 | + width: 150, |
| 56 | + decoration: BoxDecoration( |
| 57 | + color: Theme.of(context).colors.elevationsOnSurfaceNeutralLv1Grey, |
| 58 | + borderRadius: BorderRadius.circular(20), |
| 59 | + ), |
| 60 | + child: ListView.builder( |
| 61 | + controller: _scrollController, |
| 62 | + itemExtent: itemExtent, |
| 63 | + itemCount: _timeList.length, |
| 64 | + itemBuilder: (context, index) { |
| 65 | + final timeOfDay = _timeList[index]; |
| 66 | + |
| 67 | + return _TimeText( |
| 68 | + key: ValueKey(timeOfDay.formatted), |
| 69 | + value: timeOfDay, |
| 70 | + onTap: widget.onTap, |
| 71 | + isSelected: timeOfDay == widget.selectedTime, |
| 72 | + timeZone: widget.timeZone, |
| 73 | + ); |
| 74 | + }, |
| 75 | + ), |
| 76 | + ), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + void _scrollToTime(TimeOfDay value) { |
| 81 | + final index = _timeList.indexWhere((e) => e == widget.selectedTime); |
| 82 | + |
| 83 | + if (index != -1) { |
| 84 | + _scrollController.jumpTo(index * itemExtent); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + List<TimeOfDay> _generateTimeList() { |
| 89 | + return [ |
| 90 | + for (var hour = 0; hour < 24; hour++) |
| 91 | + for (final minute in [0, 30]) TimeOfDay(hour: hour, minute: minute), |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class _TimeText extends StatelessWidget { |
| 97 | + final ValueChanged<TimeOfDay> onTap; |
| 98 | + final TimeOfDay value; |
| 99 | + final bool isSelected; |
| 100 | + final String? timeZone; |
| 101 | + |
| 102 | + const _TimeText({ |
| 103 | + super.key, |
| 104 | + required this.value, |
| 105 | + required this.onTap, |
| 106 | + this.isSelected = false, |
| 107 | + this.timeZone, |
| 108 | + }); |
| 109 | + |
| 110 | + @override |
| 111 | + Widget build(BuildContext context) { |
| 112 | + final timeZone = this.timeZone; |
| 113 | + |
| 114 | + return Material( |
| 115 | + clipBehavior: Clip.hardEdge, |
| 116 | + type: MaterialType.transparency, |
| 117 | + child: InkWell( |
| 118 | + onTap: () => onTap(value), |
| 119 | + child: ColoredBox( |
| 120 | + color: !isSelected |
| 121 | + ? Colors.transparent |
| 122 | + : Theme.of(context).colors.onSurfaceNeutral08!, |
| 123 | + child: Padding( |
| 124 | + key: key, |
| 125 | + padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), |
| 126 | + child: Row( |
| 127 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 128 | + children: [ |
| 129 | + Text( |
| 130 | + value.formatted, |
| 131 | + style: Theme.of(context).textTheme.bodyLarge, |
| 132 | + ), |
| 133 | + if (isSelected && timeZone != null) Text(timeZone), |
| 134 | + ], |
| 135 | + ), |
| 136 | + ), |
| 137 | + ), |
| 138 | + ), |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments