|
import 'package:flutter/material.dart'; |
|
import 'package:flutter_hooks/flutter_hooks.dart'; |
|
import 'package:number_inc_dec/number_inc_dec.dart'; |
|
|
|
const useController = useTextEditingController; |
|
|
|
void main() { |
|
runApp(const MyApp()); |
|
} |
|
|
|
class MyApp extends StatelessWidget { |
|
const MyApp({super.key}); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return const MaterialApp( |
|
home: Scaffold( |
|
body: Stepper(), |
|
), |
|
); |
|
} |
|
} |
|
|
|
class Stepper extends HookWidget { |
|
const Stepper({super.key}); |
|
|
|
int controllerValue(TextEditingController controller) => |
|
int.parse(controller.value.text); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
final contMin = useController.fromValue(const TextEditingValue(text: '10')); |
|
final contMax = useController.fromValue(const TextEditingValue(text: '14')); |
|
final contInc = useController.fromValue(const TextEditingValue(text: '2')); |
|
final contVal = useController.fromValue(const TextEditingValue(text: '0')); |
|
|
|
useEffect(() { |
|
contVal.text = controllerValue(contVal) |
|
.clamp(controllerValue(contMin), controllerValue(contMax)) |
|
.toString(); |
|
return; |
|
}, [ |
|
useValueListenable(contMin), |
|
useValueListenable(contMax), |
|
useValueListenable(contVal), |
|
]); |
|
|
|
useValueListenable(contInc); |
|
|
|
return Column( |
|
children: [ |
|
Row( |
|
children: [ |
|
SizedBox( |
|
width: 100, |
|
child: NumberInputWithIncrementDecrement( |
|
controller: contMin, |
|
initialValue: controllerValue(contMin), |
|
max: controllerValue(contMax), |
|
), |
|
), |
|
SizedBox( |
|
width: 100, |
|
child: NumberInputWithIncrementDecrement( |
|
controller: contMax, |
|
initialValue: controllerValue(contMax), |
|
min: controllerValue(contMin), |
|
), |
|
), |
|
SizedBox( |
|
width: 100, |
|
child: NumberInputWithIncrementDecrement( |
|
controller: contInc, |
|
initialValue: controllerValue(contInc), |
|
min: 0, |
|
), |
|
), |
|
], |
|
), |
|
Row( |
|
children: [ |
|
TextButton( |
|
onPressed: controllerValue(contVal) <= controllerValue(contMin) |
|
? null |
|
: () => contVal.text = |
|
(controllerValue(contVal) - controllerValue(contInc)) |
|
.toString(), |
|
child: Text('- ${controllerValue(contInc)}'), |
|
), |
|
SizedBox( |
|
width: 100, |
|
child: NumberInputWithIncrementDecrement( |
|
controller: contVal, |
|
initialValue: controllerValue(contVal), |
|
min: controllerValue(contMin), |
|
max: controllerValue(contMax), |
|
), |
|
), |
|
TextButton( |
|
onPressed: controllerValue(contVal) >= controllerValue(contMax) |
|
? null |
|
: () => contVal.text = |
|
(controllerValue(contVal) + controllerValue(contInc)) |
|
.toString(), |
|
child: Text('+ ${controllerValue(contInc)}'), |
|
), |
|
], |
|
), |
|
], |
|
); |
|
} |
|
} |
svelte 3 repl