Reputation: 103
Here is I wanna do. When MenuScreen's checkboxListTile selected, parent screen navigation button change state to switch some text.
multipule selection , there is no navigation pop return.
no async function, InheritedWidget seems not any update event.
I never found global variable onChange Listener, May be I should know.
import 'package:flutter/material.dart';
class SandBoxScreen extends StatefulWidget {
@override
SandBoxState createState() => SandBoxState();
}
class SandBoxState extends State<SandBoxScreen> with TickerProviderStateMixin {
@override
void initState() {
super.initState();
}
bool isSelected = false;
@override
Widget build(BuildContext context) {
String nextText = isSelected ? 'next' : 'plz select';
return new Scaffold(
appBar: AppBar(),
body: new Container(
child:Column(children: <Widget>[
_MenuSelection(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
RaisedButton(child:Text('prev')),
RaisedButton(child:Text(nextText)),
],
)
],)
)
);
}
}
class _MenuSelection extends StatefulWidget{
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<_MenuSelection>{
List<bool> selection = [false,false,false,false];
@override
Widget build(BuildContext context) {
return Container(child:Column(
children: <Widget>[
CheckboxListTile(value: selection[0], onChanged: (a){setState(() { selection[0] = a; });} , title: Text('item1'),),
CheckboxListTile(value: selection[1], onChanged: (a){setState(() { selection[1] = a; });} , title: Text('item2'),),
CheckboxListTile(value: selection[2], onChanged: (a){setState(() { selection[2] = a; });} , title: Text('item3'),),
CheckboxListTile(value: selection[3], onChanged: (a){setState(() { selection[3] = a; });} , title: Text('item4'),),
],
));
}
}
Upvotes: 0
Views: 1302
Reputation: 103
I just leave complete code for this.
import 'package:flutter/material.dart';
class SandBoxScreen extends StatefulWidget {
@override
SandBoxState createState() => SandBoxState();
}
class SandBoxState extends State<SandBoxScreen> with TickerProviderStateMixin {
@override
void initState() {
super.initState();
}
bool isSelected = false;
List<bool> selection = [false, false, false, false];
@override
Widget build(BuildContext context) {
String nextText = isSelected ? 'next' : 'plz select';
return new Scaffold(
appBar: AppBar(),
body: new Container(
child: Column(
children: <Widget>[
_MenuSelection(selection,(index, value){
setState(() {
selection[index] = value;
if(selection.contains(true)){
isSelected = true;
}else{
isSelected = false;
}
});
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
RaisedButton(child: Text('prev')),
RaisedButton(child: Text(nextText)),
],
)
],
)));
}
}
class _MenuSelection extends StatefulWidget {
final Function onSelect;
final List selection;
_MenuSelection(this.selection, this.onSelect);
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<_MenuSelection> {
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
CheckboxListTile(
value: widget.selection[0],
onChanged: (a) {
setState(() {
widget.selection[0] = a;
widget.onSelect(0,a);
});
},
title: Text('item1'),
),
],
));
}
}
Upvotes: 0
Reputation: 5894
The simplest way to handle this use case is to pass a callback function to your child widget. That way you can say to the parent to rebuild using setState()
class _MenuSelection extends StatefulWidget {
final Function onSelect;
_MenuSelection(this.onSelect);
}
// child
CheckboxListTile(value: selection[0], onChanged: (a) {
setState(() {
selection[0] = a;
onSelect(0, a);
});
} , title: Text('item1'),),
// SandBoxState
Column(children: <Widget>[
_MenuSelection((index, value) {
// rebuild SandBoxState
setState(() {
...
});
},
Upvotes: 1