如何在颤抖中将一个有状态的小部件的状态从另一个状态更改为? [英] how to change the state of one statefulwidget from another in flutter?

查看:48
本文介绍了如何在颤抖中将一个有状态的小部件的状态从另一个状态更改为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有状态的小部件,有条件地在堆栈中渲染两个孩子,我想更改第三个孩子的出借条件.有什么主意吗?

I have a stateful widget which is conditionally rendering two childs inside stack, and i want to change the condition of the rending from a third child . any idea ?

父代码:

  class MapPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Body()
    );
  }
}

class Body extends StatefulWidget { 
  final String showScreen;
  
  const Body({
    Key key, 
    this.showScreen="post",
  }) : super(key:key);

  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  
  
  Widget _conditionedWidget(){
  if(this.widget.showScreen=="map"){
   return MapScreen();
 }else if(this.widget.showScreen == "post"){
   return PostScreen();
 }
}

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
      DrawerScreen(),
      _conditionedWidget(),
      ],
    );
  }
}

子代码

class DrawerScreen extends StatefulWidget {
  @override
  _DrawerScreenState createState() => _DrawerScreenState();
}

class _DrawerScreenState extends State<DrawerScreen> {
  
  @override
  Widget build(BuildContext context) {
    return Container(
      color:kPrimaryColor,
      padding:EdgeInsets.only(top:70),  
      child:Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              SizedBox(width:20.0),
              CircleAvatar(),
              SizedBox(width:10.0),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('Biswas Sampad',style:TextStyle(
                    color:Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  )),
                  Text('@biswassampad',style:TextStyle(
                    color:Colors.grey[200],
                    fontSize: 15.0,
                  ))
                ],
              )
            ],
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 20,vertical:20),
            margin: EdgeInsets.symmetric(vertical:30),
            child: Column(
              children: <Widget>[
                MenuButton(icon:Icons.style, name:'Explore',action:(){
                      print('showing maop');
                }),
                MenuButton(icon:Icons.tag_faces, name:'Profile',action:(){
                      print('showing profile');
                }),
                MenuButton(icon:Icons.people, name:'People',action:(){
                      print('showing People');
                }),
                MenuButton(icon:Icons.speaker_notes, name:'Messages',action:(){
                      print('showing messages');
                }),
                MenuButton(icon:Icons.notifications, name:'Notifications',action:(){
                      print('showing Notifications');
                }),
                MenuButton(icon:Icons.satellite,name:'Settings',action:(){
                      print('showing settings');
                })
              ],
            ),
          ),
          LogoutSection()
        ],
      )
    );
  }
}

所以基本上我想从DrawerScreen> MenuButton> action更改父窗口小部件的showScreen值?

So basically i want to change the showScreen value of the parent widget from DrawerScreen>MenuButton>action ?

任何想法怎么做!预先感谢.

any idea how to do it !! Thanks in advance.

推荐答案

您可以在"DrawerScreen"窗口中使用 Function .这样的小部件:

You can use the Function in "DrawerScreen" widget like this :

将此代码写到类的标题中:

write this code into the header of the class :

final Function onChangeState = Function();
DrawerScreen({@rquired onChangeState});

并在MenuButton中调用 onChangeState 函数,如下所示:

and in MenuButton call onChangeState function , like this:

 MenuButton(icon:Icons.satellite,name:'Settings',action:(){
                      widget.onChangeState("Settings");
                })

并将 Body 小部件中的旧代码更改为:

and change old code in Body widget to :

 @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
      DrawerScreen(onChangeState : (newState){
       setState(){
          this.widget.showScreen = newState;
         };
       }),
      _conditionedWidget(),
      ],
    );
  }

这篇关于如何在颤抖中将一个有状态的小部件的状态从另一个状态更改为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆