NumberPicker
NumberPicker copied to clipboard
Values doesn't match between picker and onchanged
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),
),
),
),
],
),
),`
Hi,
I have a similar problem. I made a gif.
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: () {},
),
],
));
}
}
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.
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;
}
Is it still a problem?