栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

弹出时强制Flutter导航器重新加载状态

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

弹出时强制Flutter导航器重新加载状态

您可以在这里做几件事。@Mahi的答案虽然正确,但可能更简洁一些,实际上是在OP询问时使用push而不是showDialog。这是一个使用示例

Navigator.push

import 'package:flutter/material.dart';class SecondPage extends StatelessWidget {  @override  Widget build(BuildContext context) {    return new Container(      color: Colors.green,      child: new Column(        children: <Widget>[          new RaisedButton( onPressed: () => Navigator.pop(context), child: new Text("back"),          ),        ],      ),    );  }}class FirstPage extends StatefulWidget {  @override  State<StatefulWidget> createState() => new FirstPageState();}class FirstPageState extends State<FirstPage> {  Color color = Colors.white;  @override  Widget build(BuildContext context) {    return new Container(      color: color,      child: new Column(        children: <Widget>[          new RaisedButton(   child: new Text("next"),   onPressed: () {     Navigator         .push(       context,       new MaterialPageRoute(builder: (context) => new SecondPage()),     )         .then((value) {       setState(() {         color = color == Colors.white ? Colors.grey : Colors.white;       });     });   }),        ],      ),    );  }}void main() => runApp(      new MaterialApp(        builder: (context, child) => new SafeArea(child: child),        home: new FirstPage(),      ),    );

但是,还有另一种方法可以很好地满足您的用例。如果您使用

global
会影响首页的构建,则可以使用InheritedWidget定义全局用户首选项,并且每次更改它们时,FirstPage都会重新生成。这甚至可以在如下所示的无状态小部件中使用(但也应在有状态小部件中使用)。

应用程序的主题是flutter中的InheritedWidget的一个示例,尽管它们是在窗口小部件中定义它的,而不是像我在此处那样直接构建它。

import 'package:flutter/material.dart';import 'package:meta/meta.dart';class SecondPage extends StatelessWidget {  @override  Widget build(BuildContext context) {    return new Container(      color: Colors.green,      child: new Column(        children: <Widget>[          new RaisedButton( onPressed: () {   ColorDefinition.of(context).toggleColor();   Navigator.pop(context); }, child: new Text("back"),          ),        ],      ),    );  }}class ColorDefinition extends InheritedWidget {  ColorDefinition({    Key key,    @required Widget child,  }): super(key: key, child: child);  Color color = Colors.white;  static ColorDefinition of(BuildContext context) {    return context.inheritFromWidgetOfExactType(ColorDefinition);  }  void toggleColor() {    color = color == Colors.white ? Colors.grey : Colors.white;    print("color set to $color");  }  @override  bool updateShouldNotify(ColorDefinition oldWidget) =>      color != oldWidget.color;}class FirstPage extends StatelessWidget {  @override  Widget build(BuildContext context) {    var color = ColorDefinition.of(context).color;    return new Container(      color: color,      child: new Column(        children: <Widget>[          new RaisedButton(   child: new Text("next"),   onPressed: () {     Navigator.push(       context,       new MaterialPageRoute(builder: (context) => new SecondPage()),     );   }),        ],      ),    );  }}void main() => runApp(      new MaterialApp(        builder: (context, child) => new SafeArea(   child: new ColorDefinition(child: child), ),        home: new FirstPage(),      ),    );

如果您使用继承的窗口小部件,则不必担心观看所推送页面的弹出状态,该弹出窗口适用于基本用例,但在更复杂的情况下最终可能会遇到问题。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号