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
Picker UI not being correctly redraw once initialValue property gets updated #26
Comments
Hey, could you provide source code so I can reproduce the bug? |
@MarcinusX hi, I was just looking at the NumberPicker code, I saw there is an animateInt method that scrolls to the right position. Am I suppose to call it manually when the initialValue changes? If so, this would be more a feature request than a bug... |
Here a simple example where the initialValue is updated to 10 once you press the button, the pickerUI doesn't reflect the value though. class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _initialValue = 5;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
NumberPicker.integer(
initialValue: _initialValue,
minValue: 0,
maxValue: 100,
step: 1,
onChanged: (newValue) {},
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
_initialValue = 10; // we update to 10, expect the picker to scroll to this value
});
},
tooltip: 'Test',
child: new Icon(Icons.add),
),
);
}
} |
Ok, so yeah, I think that calling I cannot really imagine how I could animate to that changed value. I would have to run it on every build method asynchronously and I guess this is wouldn't be the cleanest approach. I guess calling that method manually is not that bad, right? |
I'm not very familiar yet with flutter/dart, how would you adapt my example to call animateInt when initialValue changes in a "clean" fashion? Maybe whenn building the widget create a |
It won't work like that because |
Maybe you want to keep this open as feature request? I'm probably not the only one that wants to set a value on the picker after an async operation (e.g. load last value from storage/API/...). This is the (worst?) way I could think of for calling the animateInt(). I'm just calling a future on the builder method sometime in the future to be sure that the widget is "attached" already. How can I improve this? new Future.delayed(new Duration(milliseconds: 100), () {
try {
picker.animateInt(model.value);
} catch(e){
print("Silently catching exception from picker ${e.toString()}"); // ScrollController not attached to any
scroll views
}
}); |
I don't see anything wrong about it. Why you don't like it? |
because its a hack, it generates exception when the scrollcontroller is not attached, is non deterministic and even worst the code sucks 👎 |
Ok, I think you are right. :) Would you consider proposing a solution in a pull request? |
Since I'm not very experienced its probably better that someone else take this task :) |
this issue still has not been solved? |
Issue still present, this makes the widget unusable since there's no way for the user to select again the initialValue once changed. |
pls fix this. |
When is a fix comming, I understand it is a picker but when something on the widget changes and setState is called why is not the initial number set? or why cant we change the current value or something? is there a hack until this is resolved pretty annoying |
I have the some problem. |
I had a similar issue. I solved it using the "animateInt" method, but without any need of a scrollcontroller. Here is my code sample. I'm calling asynchronously an alert dialog, where I fetch a new value to be displayed by the Number Picker. Note some key factors:
I hope this can be useful to anyone with the same issue. @override
Widget build(BuildContext context) {
NumberPicker pickerM = new NumberPicker.integer(
itemExtent: 40,
initialValue: _currentValueM,
minValue: 0,
maxValue: 10,
onChanged: (newValue) => setState(() => _currentValueM = newValue)
);
return AlertDialog(
title: Text('Ingrese el número'),
content: SingleChildScrollView(
child: Column(children: [
Row(
children: <Widget>[
pickerM,
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Cancelar'),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Ver recientes'),
onPressed: () async {
final redraw = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return ListTile(
title: Text(recentReports[i].horas.toString()),
onTap: () {
_currentValueM = recentReports[i].horas.floor();
Navigator.of(context).pop(true);
}
);
});
return AlertDialog(
title: Text('Actividades recientes:'),
content: SingleChildScrollView(
child: Card(
child: Column(
children: listaAct,
),
),
),
);
}
);
try {
if (redraw != null) {
if (redraw) setState(() {
pickerM.animateInt(_currentValueM);
});
}
} catch (e) {
print(e.toString());
}
},
),
FlatButton(
child: Text('Aceptar'),
onPressed: () {
if ((_formKey.currentState.validate()) && (horasActNow != 0.0)) {
// Other stuff
}
},
),
],
)
);
} |
I'm experiencing the following issue:
Without calling the async method, initialValue defaults to one (in green):
When calling the async method and the intialValue changed to 6, the UI doesn't reflect the new value:
Thanks
The text was updated successfully, but these errors were encountered: