繁体   English   中英

从导航抽屉调用时,Flutter状态小部件未更新

[英]Flutter stateful widget is not updating while calling from Navigation drawer

我正在尝试从导航抽屉中调用类的有状态窗口小部件时进行更新。 从导航抽屉中调用无状态窗口小部件时将对其进行更新。 这是我称为“片段优先”的导航抽屉。

class DrawerItem {
String title;
IconData icon;
DrawerItem(this.title, this.icon);
 }

class HomePage extends StatefulWidget {
 final drawerItems = [
   new DrawerItem("First Fragment", Icons.rss_feed),
   new DrawerItem("Second Fragment", Icons.local_pizza),
   new DrawerItem("Third Fragment", Icons.info)
 ];

 @override
 State<StatefulWidget> createState() {
   return new HomePageState();
 }
}

class HomePageState extends State<HomePage> {
 int _selectedDrawerIndex = 0;

  _getDrawerItemWidget(int pos) {
    switch (pos) {
  case 0:

    return new FirstFragmen(pos);
  case 1:

    return new FirstFragmen(pos);
  case 2:

    return new FirstFragmen(pos);

  default:
    return new Text("Error");
  }
}

 _onSelectItem(int index) {
   setState(() => _selectedDrawerIndex = index);
   Navigator.of(context).pop(); // close the drawer
 }

 @override
 Widget build(BuildContext context) {
   List<Widget> drawerOptions = [];
   for (var i = 0; i < widget.drawerItems.length; i++) {
    var d = widget.drawerItems[i];
     drawerOptions.add(
       new ListTile(
      leading: new Icon(d.icon),
      title: new Text(d.title),
      selected: i == _selectedDrawerIndex,
      onTap: () => _onSelectItem(i),
    )
  );
}

return new Scaffold(
  appBar: new AppBar(
    // here we display the title corresponding to the fragment
    // you can instead choose to have a static title
    title: new Text(widget.drawerItems[_selectedDrawerIndex].title),
  ),
  drawer: new Drawer(
    child: new Column(
      children: <Widget>[
        new UserAccountsDrawerHeader(
            accountName: new Text("John Doe"), accountEmail: null),
        new Column(children: drawerOptions)
      ],
    ),
     ),
     body: _getDrawerItemWidget(_selectedDrawerIndex),
   );
    }
 }

这是片段优先:

class FirstFragment extends StatefulWidget {

 int pos;
 FirstFragment(this.pos);
@override
 _FirstFragmentState createState() => new _FirstFragmentState(pos);
 }

 class _FirstFragmentState extends State<FirstFragment> {
 int pos;
 _FirstFragmentState(this.pos);

 @override
 Widget build(BuildContext context) {

  // TODO: implement build
  return new Center(
  child: new Text("Hello Fragment $pos"), >printing 'pos' only. It remains 
  > same all time when new class is called. 
  );
 }
}

如果我使用的是无状态窗口小部件,则它会被更新,但是有状态窗口小部件不会被更新。 我尝试使用断点进行调试,但是_FirstFragmentState类仅被调用一次。 当第二次调用时,有什么方法可以重画所有小部件。

状态创建一次,然后共享给窗口小部件的多个实例。 由于您要在状态构造函数中使用pos ,因此稍后在窗口小部件更改时不会进行更新。

解决方法之一是取消了pos_FirstFragmentState ,并引用posFirstFragment直接。 您可以通过状态类的widget字段来访问它。

class _FirstFragmentState extends State<FirstFragment> {
  @override
  Widget build(BuildContext context) {
   return new Center(
     child: new Text("Hello Fragment ${widget.pos}"), // -> use pos from FirstFragment
   );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM