Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter_spinbox/flutter_spinbox.dart';
- // MySpin class
- class MySpin extends StatefulWidget {
- const MySpin(
- {super.key,
- required this.decimals,
- required this.increment,
- required this.value,
- required this.onChanged});
- final int decimals;
- final double increment;
- final double value;
- final Function(double) onChanged;
- @override
- State<MySpin> createState() => _MySpinState();
- }
- class _MySpinState extends State<MySpin> {
- late double _value = widget.value;
- late int _decimals = widget.decimals;
- late double _increment = widget.increment;
- double _iconSize = 20;
- double _textWidth = 50;
- TextEditingController _controller = TextEditingController();
- @override
- void initState() {
- _controller.text = _value.toStringAsFixed(_decimals);
- //_controller.text = _value.toStringAsFixed(_digits);
- _controller.addListener(() {
- final v = double.tryParse(_controller.text) != null
- ? double.parse(_controller.text)
- : _value;
- if (v == _value) return;
- setState(() {
- _value = double.parse(_controller.text);
- });
- widget.onChanged(_value);
- });
- super.initState();
- }
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- decoration: BoxDecoration(
- border: Border.all(), borderRadius: BorderRadius.circular(10)),
- child: IntrinsicWidth(
- child: Row(children: [
- IconButton(
- icon: Icon(Icons.remove_circle_outline),
- visualDensity: VisualDensity.compact,
- iconSize: _iconSize,
- onPressed: () {
- setState(() {
- _value -= _increment;
- _controller.text = _value.toStringAsFixed(_decimals);
- });
- widget.onChanged(_value);
- }),
- SizedBox(
- width: _textWidth,
- child: TextField(
- textAlign: TextAlign.center,
- keyboardType: TextInputType.number,
- controller: TextEditingController(
- text: _value.toStringAsFixed(_decimals)),
- decoration: null,
- inputFormatters: [
- //NumberTextInputFormatter(decimalDigits: _digits)
- ],
- onChanged: (value) {
- TextPosition textPos = _controller.selection.base;
- _controller.text = value;
- _controller.selection = TextSelection.fromPosition(textPos);
- if (double.tryParse(value) != null) {
- setState(() {
- _value = double.parse(value);
- });
- widget.onChanged(_value);
- }
- },
- )),
- IconButton(
- visualDensity: VisualDensity.compact,
- iconSize: _iconSize,
- icon: Icon(Icons.add_circle_outline),
- onPressed: () {
- setState(() {
- _value += _increment;
- _controller.text = _value.toStringAsFixed(_decimals);
- });
- widget.onChanged(_value);
- }),
- ]),
- ),
- );
- }
- }
- // main app
- void main() => runApp(const MyApp());
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- debugShowCheckedModeBanner: false,
- theme: ThemeData(colorSchemeSeed: Colors.blue),
- home: const MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- final String title;
- const MyHomePage({super.key, required this.title});
- @override
- State<MyHomePage> createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- double _counter = 0;
- void _incrementCounter() {
- setState(() {
- _counter++;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: Text(widget.title)),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Text('You have pushed the button this many times:'),
- Text(
- '$_counter',
- style: Theme.of(context).textTheme.headlineMedium,
- ),
- SpinBox(
- decimals: 1,
- value: _counter,
- onChanged: (value) {
- setState(() {
- _counter = value;
- });
- },),
- MySpin(
- decimals: 1,
- increment: 0.1,
- value: _counter,
- onChanged: (value) {
- setState(() {
- _counter = value;
- });
- },
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: _incrementCounter,
- tooltip: 'Increment',
- child: const Icon(Icons.add),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement