Flutter setState 到另一个类? [英] Flutter setState to another class?
问题描述
我有一个根类 RootPage
,它是一个始终在视图中的 StatefulWidget
.我想更改 RootPage
中的 body
,它由来自不同类的 RootPage's currentPage Widget
控制,例如我的 FeedPage
班级和我开设的任何其他班级?
I have a root class RootPage
which is a StatefulWidget
which is always in view. I would like to change the body
in RootPage
which is controlled by RootPage's currentPage Widget
from different classes such as my FeedPage
class and any other class that I make?
示例代码
import 'package:flutter/material.dart';
class RootPage extends StatefulWidget {
@override
_RootPageState createState() => new _RootPageState();
}
class _RootPageState extends State<RootPage> {
FeedPage feedPage;
Widget currentPage;
@override
void initState() {
super.initState();
feedPage = FeedPage();
currentPage = feedPage;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
//Current page to be changed from other classes too?
body: currentPage
);
}
}
class FeedPage extends StatefulWidget {
@override
_feedPageState createState() => new _feedPageState();
}
class _feedPageState extends State<FeedPage> {
@override
Widget build(BuildContext context) {
return new FlatButton(
onPressed: () {
setState(() {
//change the currentPage in RootPage so it switches FeedPage away and gets a new class that I'll make
});
},
child: new Text('Go to a new page but keep root, just replace this feed part'),
);
}
}
我确定有一个明显的答案,但我似乎无法弄清楚如何有效地做到这一点,因此很容易集成未来的课程并始终保持我的 Root 可见.
I'm sure there's an obvious answer but I can't seem to figure out how to do it efficiently so it'll be easy to integrate future classes and keep my Root always in view.
推荐答案
您可以使用回调函数来实现这一点.请参考以下代码.
You can use callbacks functions to achieve this. Please refer to the below code.
import 'package:flutter/material.dart';
class RootPage extends StatefulWidget {
@override
_RootPageState createState() => new _RootPageState();
}
class _RootPageState extends State<RootPage> {
FeedPage feedPage;
Widget currentPage;
@override
void initState() {
super.initState();
feedPage = FeedPage(this.callback);
currentPage = feedPage;
}
void callback(Widget nextPage) {
setState(() {
this.currentPage = nextPage;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
//Current page to be changed from other classes too?
body: currentPage
);
}
}
class FeedPage extends StatefulWidget {
Function callback;
FeedPage(this.callback);
@override
_feedPageState createState() => new _feedPageState();
}
class _feedPageState extends State<FeedPage> {
@override
Widget build(BuildContext context) {
return new FlatButton(
onPressed: () {
this.widget.callback(new NextPage());
// setState(() {
// //change the currentPage in RootPage so it switches FeedPage away and gets a new class that I'll make
// });
},
child: new Text('Go to a new page but keep root, just replace this feed part'),
);
}
}
这与这个问题非常相似 你可以参考我的回答中的第三点.
This is very similar to this problem and you could refer 3rd point in my answer.
这篇关于Flutter setState 到另一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!