Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values doesn't match between picker and onchanged #77

Open
MauserTom opened this issue Jul 13, 2020 · 4 comments
Open

Values doesn't match between picker and onchanged #77

MauserTom opened this issue Jul 13, 2020 · 4 comments

Comments

@MauserTom
Copy link

That's my code snippet. When I change the value due scrolling, the first number does only the change and when i use the decimal Value it alway jumps back to the inital value( first digit).

` NumberPicker.decimal(

            decoration: BoxDecoration(
              backgroundBlendMode: BlendMode.modulate,
              color: shinyColor,
            ),
            highlightSelectedValue: false,
            initialValue: fieldData.amount,
            minValue: 0,
            maxValue: 6,
            onChanged: (newValue) {
              print(newValue);
              _controllerAmount.sink.add(newValue);
              fieldData.amount = newValue;
            },
          ),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: StreamBuilder(
              stream: streamAmount,
              initialData: fieldData.amount,
              builder: (context, snapshot) => Text(
                '${snapshot.requireData} to/ha',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            ),
          ),
        ],
      ),
    ),`
@amittel
Copy link

amittel commented Aug 11, 2020

Hi,
I have a similar problem. I made a gif.
0bOlRCIRJ9

And here is the dart file:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:numberpicker/numberpicker.dart';

class Circle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Circle"),
      ),
      body: new Home(title: 'NumberPicker'),
    );
  }
}

//Our Home stateful widget class
class Home extends StatefulWidget {
  //Our `Home` constructor
  //Receives a Key and title
  //A Key is an identifier for Widget
  Home({Key key, this.title}) : super(key: key);

  final String title;

  //Creates the mutable state for this widget at a given location in the tree.
  @override
  _HomeState createState() => new _HomeState();
}

//Our Home state. State is the logic and internal state for a StatefulWidget
class _HomeState extends State<Home> {
  int _rounds = 0;
  int stations = 0;
  int loadMin = 0;
  int loadSec = 0;
  double _load = 0.0;

  NumberPicker decimalNumberPicker;

  _handleValueChanged(num value) {
    if (value != null) {
      if (value is int) {
        setState(() => _rounds = value);
        //integerNumberPicker.animateInt(value);
      } else {
        setState(() => _load = value);
        //decimalNumberPicker.animateDecimalAndInteger(value);
      }
    }
  }

  Widget build(BuildContext context) {
    decimalNumberPicker = new NumberPicker.decimal(
        initialValue: _load,
        minValue: 0,
        maxValue: 60,
        decimalPlaces: 2,
        onChanged: _handleValueChanged);

    return new Scaffold(
        body: ListView(
      children: <Widget>[
        ListTile(
          leading: Icon(Icons.cached),
          title: Text('Rounds'),
          //subtitle: Text('A strong animal'),
          trailing: (new Text("$_rounds")),
          onTap: () {
            print('Rounds tapped');
          },
          selected: true,
        ),
        ListTile(
          leading: Icon(Icons.fitness_center),
          title: Text('Stations'),
          //subtitle: Text('A strong animal'),
          trailing: (new Text("$stations")),
          onTap: () {
            print('horse');
          },
          selected: true,
        ),
        ListTile(
          leading: Icon(Icons.directions_run),
          title: Text('Load'),
          //subtitle: Text('A strong animal'),
          trailing: (new Text("$_load")),
          onTap: () {
            print('horse');
            showModalBottomSheet(
                context: context,
                builder: (BuildContext bc) {
                  return Container(
                    child: new Wrap(
                      children: <Widget>[
                        decimalNumberPicker,
                      ],
                    ),
                  );
                });
          },
          selected: true,
        ),
        ListTile(
          leading: Icon(Icons.pause),
          title: Text('Pause'),
          //subtitle: Text('A strong animal'),
          trailing: (new Text("00:00")),
          onTap: () {
            print('horse');
          },
          selected: true,
        ),
        ListTile(
          leading: Icon(Icons.local_drink),
          title: Text('Pause Rounds'),
          //subtitle: Text('A strong animal'),
          trailing: (new Text("00:00")),
          onTap: () {
            print('horse');
          },
          selected: true,
        ),
        FlatButton(
          child: Text('Start'),
          color: Colors.deepOrangeAccent,
          textColor: Colors.white,
          onPressed: () {},
        ),
      ],
    ));
  }
}

@MauserTom
Copy link
Author

MauserTom commented Aug 11, 2020

Hello, maybe because you are creating everytime a new decimalPicker when you call setState() in your build() tree.

Here:

` Widget build(BuildContext context) {
    decimalNumberPicker = new NumberPicker.decimal(
        initialValue: _load,
        minValue: 0,
        maxValue: 60,
        decimalPlaces: 2,
        onChanged: _handleValueChanged);

    return new Scaffold`

Try to implement directly in the Wrap widget.

@rafaelsetragni
Copy link

rafaelsetragni commented Oct 17, 2020

Ive find the same problem.

Future<int> pickBadgeCount(BuildContext context) async {
    int amount ;

    AlertDialog alert = AlertDialog(
      title: Text("Choose the new badge amount"),
      content: NumberPicker.integer(
          initialValue: 50,
          minValue: 0,
          maxValue: 999,
          onChanged: (newValue) => amount = newValue
      ),
      actions: [
        FlatButton(
          child: Text("Cancel"),
          onPressed: (){
            amount = null;
            Navigator.of(context).pop();
          },
        ),
        FlatButton(
          child: Text("OK"),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );

    // show the dialog
    await showDialog(
      context: context,
      builder: (BuildContext context) => alert
    );

    return amount;
  }

@MarcinusX
Copy link
Owner

Is it still a problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants