我有2个StreamBuilder
里面ListView
那个听一样Stream
的,就是valueStream
。在那里面StreamBuilder
,我SwitchListTile
给 . 添加了新的价值valueStream
。
这是我的Stream
:
Observable<bool> get valueStream => valueController.stream;
这是我的ListView
:
ListView(
children: <Widget>[
Switch1(key: UniqueKey(),),
Switch2(key: UniqueKey(),),
],
),
这是Switch1
小部件:
class Switch1 extends StatefulWidget {
const Switch1(
{@required Key key})
: super(key: key);
@override
_Switch1State createState() => _Switch1State();
}
class _Switch1State extends State<Switch1> {
DummyBloc _dummyBloc;
@override
Widget build(BuildContext context) {
return StreamBuilder(
key: UniqueKey(),
stream: _dummyBloc.valueStream,
builder: (context, AsyncSnapshot<bool> snapshot) {
print('rebuild Switch1');
if(!snapshot.hasData) return Container();
return SwitchListTile(
value: snapshot.data,
onChanged: _dummyBloc.updateValue,
);
},
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_dummyBloc = DummyBloc();
}
}
这是Switch2
小部件:
class Switch2 extends StatefulWidget {
const Switch2(
{@required Key key})
: super(key: key);
@override
_Switch2State createState() => _Switch2State();
}
class _Switch2State extends State<Switch2> {
DummyBloc _dummyBloc;
@override
Widget build(BuildContext context) {
return StreamBuilder(
key: UniqueKey(),
stream: _dummyBloc.valueStream,
builder: (context, AsyncSnapshot<bool> snapshot) {
print('rebuild Switch2');
if(!snapshot.hasData) return Container();
return SwitchListTile(
activeColor: Theme.of(context).buttonColor,
value: snapshot.data,
onChanged: _dummyBloc.updateValue,
);
},
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_dummyBloc = DummyBloc();
}
}
如果我触摸Switch1
小部件,StreamBuilder
内部Switch2
应该会重建,反之亦然,但这并没有发生。我试图添加UniqueKey
到Switch1
and Switch2
,但这没有帮助。任何建议如何使这项工作?