//in Widget A -> in order to switch to Widget B
Navigator.push(context, new MaterialPageRoute(
builder: (_) => new WidgetB(),
));
这不起作用,因为Widget B不会停留在主Widget的WidgetTree中
如果这是不可能的,我现在想要实现我想要的东西是什么样的: - )
答案 0 :(得分:0)
你需要让一些父母了解这种子导航。您无法真正替换视图本身,它的父级是树中对该窗口小部件的引用,因此父级需要传达此信息才能使其颤动。
也就是说,您可以制作一个自定义小部件,其工作是监听子事件,并相应地更改子级。
App
/ \
Row Row
|
WidgetParent
|
WidgetA
此处WidgetParent
可以保留,例如一个Listenable
并将ChangeNotifier
传递给WidgetA
。当WidgetA
决定WidgetX
应该进入屏幕时,它可以将其发送给父母。
class WidgetParent extends AnimatedWidget {
WidgetParent() : super(listenable: ValueNotifier<Widget>(null));
ValueNotifier<Widget> get notifier => listenable as ValueNotifier<Widget>;
Widget build(BuildContext context) {
return new Center(child: notifier.value ?? WidgetA(notifier));
}
}
现在,子窗口小部件可以通知下一个人。
class WidgetA extends StatelessWidget {
WidgetA(this.notifier);
final ValueNotifier<Widget> notifier;
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => notifier.value = WidgetB(notifier),
child: Text("This is A, go to B"),
);
}
}
class WidgetB extends StatelessWidget {
WidgetB(this.notifier);
final ValueNotifier<Widget> notifier;
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => notifier.value = WidgetA(notifier),
child: Text("This is B, go to A"),
);
}
}
答案 1 :(得分:0)
我对Flutter还是很陌生,但是我可以想到的一种解决方法是-您将State与小部件一起使用。例如,如果要根据用途显示两行,则可以将所需行的高度设置为1,将第二行的高度设置为零。并按需交换。 当然,您还需要清空内容。例如将文字设置为“等” 这可能不是理想的解决方案,但可以使用。