workouttest_app/lib/widgets/number_picker.dart
2021-04-12 00:51:09 +02:00

101 lines
3.0 KiB
Dart

import 'package:aitrainer_app/util/trans.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// ignore: must_be_immutable
class NumberPickerWidget extends StatefulWidget {
final Function(double) onChange;
final int minValue;
final int maxValue;
final int initalValue;
final String unit;
final Color color;
double? fontSize;
NumberPickerWidget(
{Key? key,
required this.minValue,
required this.maxValue,
required this.initalValue,
required this.unit,
this.fontSize,
required this.color,
required this.onChange})
: super(key: key) {
fontSize = fontSize ?? 20;
}
@override
_NumberPickerWidgetState createState() => _NumberPickerWidgetState();
}
class _NumberPickerWidgetState extends State<NumberPickerWidget> with Trans {
late FixedExtentScrollController _scrollController;
@override
void initState() {
super.initState();
_scrollController = FixedExtentScrollController(initialItem: widget.initalValue);
_scrollController.animateToItem(widget.initalValue, duration: Duration(milliseconds: 100), curve: Curves.easeIn);
}
@override
void didUpdateWidget(NumberPickerWidget oldWidget) {
super.didUpdateWidget(oldWidget);
}
Widget durationPicker({bool inSeconds = false, bool inHundredths = false}) {
double value = 0;
return CupertinoPicker(
scrollController: _scrollController,
useMagnifier: true,
magnification: 1.2,
diameterRatio: 0.85,
backgroundColor: Colors.transparent,
selectionOverlay: Container(),
onSelectedItemChanged: (x) {
currentData = x.toDouble();
value = x.toDouble();
//print("sec" + seconds.toStringAsFixed(2));
setState(() {});
widget.onChange(value);
},
children: List.generate(
widget.maxValue, (index) => Text('$index ' + widget.unit, style: TextStyle(color: widget.color, fontSize: widget.fontSize))),
itemExtent: 30,
);
}
double currentData = 0;
@override
Widget build(BuildContext context) {
setContext(context);
return Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width * .40,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width * .35,
child: Center(
child: Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width * 0.95,
//height: MediaQuery.of(context).size.height * 0.25,
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child: durationPicker()),
],
)),
),
),
],
),
);
}
}