124 lines
3.8 KiB
Dart
124 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:hiddingsel_app/constants/constant.dart';
|
|
|
|
class HiddingselTimePicker extends StatelessWidget {
|
|
final int _height;
|
|
final double _width;
|
|
final Duration _initTime;
|
|
final Function(Duration dateTime) _onSelected;
|
|
|
|
HiddingselTimePicker(this._initTime, this._onSelected,
|
|
{width = 300, height = 100, key})
|
|
: _width = width,
|
|
_height = height,
|
|
super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
int days = _initTime.inDays % (365);
|
|
int hours = _initTime.inHours % 24;
|
|
int minutes = _initTime.inMinutes % 60;
|
|
|
|
var daysWheel = _buildWheel(7, days, (d) {
|
|
days = d;
|
|
_onSelected(Duration(days: days, hours: hours, minutes: minutes));
|
|
}, UITheme.theme.textTheme.bodyLarge);
|
|
var hoursWheel = _buildWheel(24, hours, (h) {
|
|
hours = h;
|
|
_onSelected(Duration(days: days, hours: hours, minutes: minutes));
|
|
}, UITheme.theme.textTheme.bodyLarge);
|
|
var minutesWheel = _buildWheel(60, minutes, (m) {
|
|
minutes = m;
|
|
_onSelected(Duration(days: days, hours: hours, minutes: minutes));
|
|
}, UITheme.theme.textTheme.bodyLarge);
|
|
return Container(
|
|
height: _height.toDouble(),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Spacer(flex: 5),
|
|
_stackWheelWithText(daysWheel, 'Tage'),
|
|
Spacer(flex: 1),
|
|
_stackWheelWithText(hoursWheel, 'Stunden'),
|
|
Spacer(flex: 1),
|
|
_stackWheelWithText(minutesWheel, 'Minuten'),
|
|
Spacer(flex: 5),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_buildWheel(int maxValue, initValue, onSelected, textStyle) {
|
|
double width = textStyle.fontSize;
|
|
FixedExtentScrollController controller =
|
|
FixedExtentScrollController(initialItem: initValue);
|
|
var valList = List<Widget>.generate(
|
|
maxValue,
|
|
(int index) => Text(index.toString(), style: textStyle),
|
|
);
|
|
bool autoScroll = false;
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: (scrollNotification) {
|
|
if (!autoScroll && scrollNotification is ScrollEndNotification) {
|
|
autoScroll = true;
|
|
int index;
|
|
if (scrollNotification.metrics.pixels % width > width / 2) {
|
|
index = scrollNotification.metrics.pixels ~/ width + 1;
|
|
} else {
|
|
index = scrollNotification.metrics.pixels ~/ width;
|
|
}
|
|
controller.jumpToItem(index);
|
|
onSelected(index % maxValue);
|
|
autoScroll = false;
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
child: ListWheelScrollView.useDelegate(
|
|
controller: controller,
|
|
itemExtent: width,
|
|
diameterRatio: 1,
|
|
childDelegate: ListWheelChildLoopingListDelegate(
|
|
children: valList,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
_stackWheelWithText(wheel, String unit) {
|
|
return Container(
|
|
width: _width / 3,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Spacer(flex: 1),
|
|
Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Container(
|
|
width: 40,
|
|
child: wheel,
|
|
),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: UIColors.grey5, width: 1),
|
|
borderRadius: BorderRadius.circular(UIShapes.paddingSimple),
|
|
),
|
|
child: SizedBox(
|
|
width: 30,
|
|
height: UITheme.theme.textTheme.bodyLarge?.fontSize),
|
|
),
|
|
],
|
|
),
|
|
//Spacer(flex: 1),
|
|
Text(
|
|
unit,
|
|
style: UITheme.theme.textTheme.bodyLarge,
|
|
),
|
|
Spacer(flex: 1)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|