Advertisement
Guest User

Flutter spinbox sample

a guest
Feb 21st, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_spinbox/flutter_spinbox.dart';
  3.  
  4.  
  5.  
  6. // MySpin class
  7.  
  8. class MySpin extends StatefulWidget {
  9. const MySpin(
  10. {super.key,
  11. required this.decimals,
  12. required this.increment,
  13. required this.value,
  14. required this.onChanged});
  15.  
  16. final int decimals;
  17. final double increment;
  18. final double value;
  19. final Function(double) onChanged;
  20.  
  21. @override
  22. State<MySpin> createState() => _MySpinState();
  23. }
  24.  
  25. class _MySpinState extends State<MySpin> {
  26. late double _value = widget.value;
  27. late int _decimals = widget.decimals;
  28. late double _increment = widget.increment;
  29. double _iconSize = 20;
  30. double _textWidth = 50;
  31. TextEditingController _controller = TextEditingController();
  32.  
  33. @override
  34. void initState() {
  35. _controller.text = _value.toStringAsFixed(_decimals);
  36. //_controller.text = _value.toStringAsFixed(_digits);
  37.  
  38. _controller.addListener(() {
  39. final v = double.tryParse(_controller.text) != null
  40. ? double.parse(_controller.text)
  41. : _value;
  42. if (v == _value) return;
  43.  
  44. setState(() {
  45. _value = double.parse(_controller.text);
  46. });
  47. widget.onChanged(_value);
  48. });
  49.  
  50. super.initState();
  51. }
  52.  
  53. @override
  54. void dispose() {
  55. _controller.dispose();
  56. super.dispose();
  57. }
  58.  
  59. @override
  60. Widget build(BuildContext context) {
  61. return Container(
  62. decoration: BoxDecoration(
  63. border: Border.all(), borderRadius: BorderRadius.circular(10)),
  64. child: IntrinsicWidth(
  65. child: Row(children: [
  66. IconButton(
  67. icon: Icon(Icons.remove_circle_outline),
  68. visualDensity: VisualDensity.compact,
  69. iconSize: _iconSize,
  70. onPressed: () {
  71. setState(() {
  72. _value -= _increment;
  73. _controller.text = _value.toStringAsFixed(_decimals);
  74. });
  75. widget.onChanged(_value);
  76. }),
  77. SizedBox(
  78. width: _textWidth,
  79. child: TextField(
  80. textAlign: TextAlign.center,
  81. keyboardType: TextInputType.number,
  82. controller: TextEditingController(
  83. text: _value.toStringAsFixed(_decimals)),
  84. decoration: null,
  85. inputFormatters: [
  86. //NumberTextInputFormatter(decimalDigits: _digits)
  87. ],
  88. onChanged: (value) {
  89. TextPosition textPos = _controller.selection.base;
  90. _controller.text = value;
  91. _controller.selection = TextSelection.fromPosition(textPos);
  92. if (double.tryParse(value) != null) {
  93. setState(() {
  94. _value = double.parse(value);
  95. });
  96.  
  97. widget.onChanged(_value);
  98. }
  99. },
  100. )),
  101. IconButton(
  102. visualDensity: VisualDensity.compact,
  103. iconSize: _iconSize,
  104. icon: Icon(Icons.add_circle_outline),
  105. onPressed: () {
  106. setState(() {
  107. _value += _increment;
  108. _controller.text = _value.toStringAsFixed(_decimals);
  109. });
  110. widget.onChanged(_value);
  111. }),
  112. ]),
  113. ),
  114. );
  115. }
  116. }
  117.  
  118. // main app
  119.  
  120. void main() => runApp(const MyApp());
  121.  
  122. class MyApp extends StatelessWidget {
  123. const MyApp({super.key});
  124.  
  125. @override
  126. Widget build(BuildContext context) {
  127. return MaterialApp(
  128. title: 'Flutter Demo',
  129. debugShowCheckedModeBanner: false,
  130. theme: ThemeData(colorSchemeSeed: Colors.blue),
  131. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  132. );
  133. }
  134. }
  135.  
  136. class MyHomePage extends StatefulWidget {
  137. final String title;
  138.  
  139. const MyHomePage({super.key, required this.title});
  140.  
  141. @override
  142. State<MyHomePage> createState() => _MyHomePageState();
  143. }
  144.  
  145. class _MyHomePageState extends State<MyHomePage> {
  146. double _counter = 0;
  147.  
  148. void _incrementCounter() {
  149. setState(() {
  150. _counter++;
  151. });
  152. }
  153.  
  154. @override
  155. Widget build(BuildContext context) {
  156. return Scaffold(
  157. appBar: AppBar(title: Text(widget.title)),
  158. body: Center(
  159. child: Column(
  160. mainAxisAlignment: MainAxisAlignment.center,
  161. children: [
  162. const Text('You have pushed the button this many times:'),
  163. Text(
  164. '$_counter',
  165. style: Theme.of(context).textTheme.headlineMedium,
  166. ),
  167. SpinBox(
  168. decimals: 1,
  169. value: _counter,
  170. onChanged: (value) {
  171. setState(() {
  172. _counter = value;
  173. });
  174. },),
  175. MySpin(
  176. decimals: 1,
  177. increment: 0.1,
  178. value: _counter,
  179. onChanged: (value) {
  180. setState(() {
  181. _counter = value;
  182. });
  183. },
  184. ),
  185.  
  186. ],
  187. ),
  188. ),
  189. floatingActionButton: FloatingActionButton(
  190. onPressed: _incrementCounter,
  191. tooltip: 'Increment',
  192. child: const Icon(Icons.add),
  193. ),
  194. );
  195. }
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement