Reputation: 85
Widget build(BuildContext context) {
// TODO: implement build
var TaskTextField;
var _newValue = false;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("To-Do List V2 R1"),
),
body: Container(
child: Column(
children: <Widget>[
Container(
child: TextField(
decoration:
InputDecoration(hintText: "Enter task to be added"),
onChanged: (taskTextField) {
TaskTextField = taskTextField;
print(TaskTextField);
},
),
margin: EdgeInsets.all(16.0),
),
Text("Click Me!"),
Checkbox(
activeColor: Colors.blue,
value: _newValue,
onChanged: (val) {
setState(() {
_newValue = !_newValue;
});
},
),
],
),
),
),
);
}
}
The checkmark does not show up even though the debugger shows that _newValue
is true when clicked. I tried different methods, such as using
_newValue = !_newValue
but it still does not work. I am running on Flutter 1.2.1.
Upvotes: 1
Views: 475
Reputation: 657238
Variables declared inside a function/method are recreated every time the function is executed. setState()
causes build()
to rerun, so _newValue
is initialized to false
every time.
Move the field out of the build()
method to retain the value
var _newValue = false;
Widget build(BuildContext context) {
// TODO: implement build
var TaskTextField;
...
Upvotes: 1