Flutter 颤振阵型设计-通知页面更改
这方面有最佳实践吗?(我使用这个Todo示例,因为这里更容易解释我的问题)Flutter 颤振阵型设计-通知页面更改,flutter,bloc,Flutter,Bloc,这方面有最佳实践吗?(我使用这个Todo示例,因为这里更容易解释我的问题) TODO概览页面(显示所有TODO) TodoAddPage(添加TODO的页面) 每个页面都有一个自己的区块 步骤: 在ToDoOverview页面中,我通过pushNamed导航到ToLoaddPage 在TODOADPAGE中,我添加了几个TODO 使用“导航返回”按钮返回到ToDoOverview页面 问题:我应该如何通知TodoOverviewPage有新的TODO 我不确定这是不是正确的方法。 解决方案: 覆
- 添加了我的临时解决方案,直到我找到更让我满意的东西
- 继承小部件
- ToLoaddPage中的ValueCallback 例如:
- 您可以通过不同的方式实现
class Item {
String reference;
Item(this.reference);
}
class _MyInherited extends InheritedWidget {
_MyInherited({
Key key,
@required Widget child,
@required this.data,
}) : super(key: key, child: child);
final MyInheritedWidgetState data;
@override
bool updateShouldNotify(_MyInherited oldWidget) {
return true;
}
}
class MyInheritedWidget extends StatefulWidget {
MyInheritedWidget({
Key key,
this.child,
}): super(key: key);
final Widget child;
@override
MyInheritedWidgetState createState() => new MyInheritedWidgetState();
static MyInheritedWidgetState of(BuildContext context){
return (context.inheritFromWidgetOfExactType(_MyInherited) as _MyInherited).data;
}
}
class MyInheritedWidgetState extends State<MyInheritedWidget>{
/// List of Items
List<Item> _items = <Item>[];
/// Getter (number of items)
int get itemsCount => _items.length;
/// Helper method to add an Item
void addItem(String reference){
setState((){
_items.add(new Item(reference));
});
}
@override
Widget build(BuildContext context){
return new _MyInherited(
data: this,
child: widget.child,
);
}
}
class MyTree extends StatefulWidget {
@override
_MyTreeState createState() => new _MyTreeState();
}
class _MyTreeState extends State<MyTree> {
@override
Widget build(BuildContext context) {
return new MyInheritedWidget(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Title'),
),
body: new Column(
children: <Widget>[
new WidgetA(),
new Container(
child: new Row(
children: <Widget>[
new Icon(Icons.shopping_cart),
new WidgetB(),
new WidgetC(),
],
),
),
],
),
),
);
}
}
class WidgetA extends StatelessWidget {
@override
Widget build(BuildContext context) {
final MyInheritedWidgetState state = MyInheritedWidget.of(context);
return new Container(
child: new RaisedButton(
child: new Text('Add Item'),
onPressed: () {
state.addItem('new item');
},
),
);
}
}
class WidgetB extends StatelessWidget {
@override
Widget build(BuildContext context) {
final MyInheritedWidgetState state = MyInheritedWidget.of(context);
return new Text('${state.itemsCount}');
}
}
class WidgetC extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Text('I am Widget C');
}
}
类项目{
字符串引用;
项目(本参考文件);
}
类_MyInherited扩展InheritedWidget{
_我继承的({
关键点,
@必需的小部件子项,
@需要这个数据,
}):super(key:key,child:child);
最终MyInheritedWidgetState数据;
@凌驾
bool updateShouldNotify(\u mywidget){
返回true;
}
}
类MyInheritedWidget扩展了StatefulWidget{
MyInheritedWidget({
关键点,
这个孩子,
}):super(key:key);
最后一个孩子;
@凌驾
MyInheritedWidgetState createState()=>新建MyInheritedWidgetState();
的静态MyInheritedWidgetState(BuildContext上下文){
返回(context.inheritFromWidgetOfExactType(_MyInherited)为_MyInherited).data;
}
}
类MyInheritedWidgetState扩展了状态{
///项目清单
列表_项=[];
///Getter(项目数)
int get itemsCount=>\u items.length;
///用于添加项的帮助器方法
void addItem(字符串引用){
设置状态(){
_项目。添加(新项目(参考));
});
}
@凌驾
小部件构建(构建上下文){
返回新的(
资料:这,,
child:widget.child,
);
}
}
类MyTree扩展了StatefulWidget{
@凌驾
_MyTreeState createState()=>new_MyTreeState();
}
类_MyTreeState扩展状态{
@凌驾
小部件构建(构建上下文){
返回新的MyInheritedWidget(
儿童:新脚手架(
appBar:新的appBar(
标题:新文本(“标题”),
),
正文:新栏目(
儿童:[
新WidgetA(),
新容器(
孩子:新的一排(
儿童:[
新图标(图标、购物车),
新的WidgetB(),
新的WidgetC(),
],
),
),
],
),
),
);
}
}
类WidgetA扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
最终MyInheritedWidgetState=MyInheritedWidget.of(上下文);
退回新货柜(
孩子:新升起的按钮(
子项:新文本(“添加项”),
已按下:(){
说明.附件(“新项目”);
},
),
);
}
}
类WidgetB扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
最终MyInheritedWidgetState=MyInheritedWidget.of(上下文);
返回新文本(“${state.itemscont}”);
}
}
类WidgetC扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新文本(“我是小部件C”);
}
}
临时解决方案:
每个有一个Bloc的(根)页面现在总是在构建时重新加载
该集团负责缓存
Widget build(BuildContext context) {
final PageBloc pBloc = BlocProvider.of<PageBloc >(context);
bool isNewBuild = true;
return Scaffold(
...
body: BlocBuilder<PageBlocEvent, PageBlocState>(
if (isNewBuild) {
pBloc.dispatch(PageBlocEvent(PageBlocEventType.GETALL));
isNewBuild = false;
return CircularProgressIndicator();
} else {
// Draw data
...
...
}
小部件构建(构建上下文){
最终页面bloc pBloc=BlocProvider.of(上下文);
bool isNewBuild=true;
返回脚手架(
...
正文:BlocBuilder(
if(isNewBuild){
pBloc.dispatch(PageBlocEvent(PageBlocEventType.GETALL));
isNewBuild=false;
返回循环ProgressIndicator();
}否则{
//绘制数据
...
...
}
你明白我的意思吗?我想你的第二种方式就像我的第一种方式。关于你的第一种方式,我在这两个小部件页面之间没有父子关系。在我找到更好的解决方案之前,我添加了我的临时解决方案。别忘了给“Didier”评分^^