How To Toggle value using Hook? I have tried this code but the state never chanched.
The Code Is:
class ExampleScreen extends HookWidget {
@override
Widget build(BuildContext context) {
var toggleValue = useState(false);
return Scaffold(
body: Checkbox(
value: toggleValue .value,
onChanged: (value) {
toggleValue.value != toggleValue.value;
print("\n toggleValue ${ toggleValue .value}\n");
},
);
}
}
CodePudding user response:
You should be using the value returned from the onChanged callback to assign the new value. The new value is not automatically assigned to the ValueNotifier.value.
Widget build(BuildContext context) {
var toggleValue = useState(false);
return Scaffold(
body: Checkbox(
value: toggleValue.value,
onChanged: (value) {
toggleValue.value = value!;
print("\n toggleValue $value\n");
},
)
);
}