Skip to content

Number shown is invalid if the range is externally limited #48

@dedvalson

Description

@dedvalson

The attached code demonstrates a bug with the spinbox. The spinbox itself is limited to a range of 2.0 to 4.0. The value starts at 3.0. However the onChanged callback limits the value to a range of 2.5 to 3.5. If you push the "+" button 6 times, the spinbox will display 3.6 even though the value given to it is limited to 3.5

Screenshot_1648907584

// ignore_for_file: avoid_print

import 'package:flutter/material.dart';
import 'package:flutter_spinbox/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SpinBox Bug Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'SpinBox Bug'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double value = 3.0;
  @override
  Widget build(BuildContext context) {
    print('value = $value');
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: [
            const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                'The Value resets to 3.0.\n\nIf you push the plus key 6 times\n the number shown will advance to 3.6\neven though the value being assigned is limited to 3.5',
                textAlign: TextAlign.center,
              ),
            ),
            SpinBox(
              value: value,
              min: 2.0,
              max: 4.0,
              decimals: 1,
              step: 0.1,
              onChanged: (newValue) {
                setState(() {
                  if (newValue <= 3.5 && newValue >= 2.5) {
                    value = newValue;
                  }
                  print('newValue = $newValue, value = $value');
                });
              },
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text('Value = $value'),
            ),
            ElevatedButton(
              child: const Text('RESET'),
              onPressed: () {
                setState(() {
                  value = 3.0;
                });
              },
            )
          ],
        ),
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions