简体   繁体   English

如何反映从父组件到子组件的变化

[英]How to reflect changes from parent to child component

I want to now how can I change a Boolean value from parent to child.我现在想知道如何将 Boolean 值从父项更改为子项。 On parent page i have a button on AppBar , and on body i have a child witch contains an ListView .在父页面上,我在AppBar上有一个按钮,在 body 上我有一个包含ListView的子女巫。

class Parent extends StatefulWidget {
  @override
  ParentState createState() => ParentState ();
}

class ParentState extends State<Parent> with SingleTickerProviderStateMixin  {
List<Map<String, Object>> _pages = [
   {'page': Child(isSelected: isSelect), 'title': 'Child'},
   {'page': Child2(), 'title': 'Child2'},
 ];
 bool isSelect= false;
 int _selectedPageIndex = 0;
 Widget _page;

 @override
void initState() {
  _page = _pages[_selectedPageIndex]['page'];
  // TODO: implement initState
  super.initState();
  }

 void _selectPage(int index) {
  setState(() {
    _selectedPageIndex = index;
    _page = _pages[index]['page'];
  });
 }

  toggleSelect(){
    setState(() {
      isSelect= !isSelect;
    });
  }

 Widget build(BuildContext context) {
   child: new Scaffold(
      appBar: AppBar(
      title: FlatButton(
         onPressed: toggleSelect,
         Column(
                children: <Widget>[
                     Icon(Icons.message, color: isSelect ? Colors.blue : Colors.grey),
                     Text("Select", style: TextStyle(fontSize: 14, color: isSelect? Colors.blue : Colors.grey),)
                 ],
             ),
      ),
      body: _page,
      bottomNavigationBar: BottomNavigationBar(
          onTap: _selectPage,
          backgroundColor: Theme.of(context).primaryColor,
          unselectedItemColor: Colors.grey,
          selectedItemColor: Colors.blue,
          currentIndex: _selectedPageIndex,
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('Child')),
            BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('Child2')),
          ]),
 }
}

class Child extends StatefulWidget {
 bool isSelect;
 Child({this.isSelect});

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

class ChildState extends State<Child> with SingleTickerProviderStateMixin  {
  @override
  Widget build(BuildContext context) {
  return Text(widget.isSelect)  // here is a listView but i have done like this just for example
 }

So what I what is that when I click on app bar I what that change to reflect on child page.所以我是什么,当我点击应用程序栏时,我所做的改变反映在子页面上。 Thank you谢谢

I found some syntax mistakes in your code when I copied it over.当我复制它时,我在您的代码中发现了一些语法错误。 But otherwise it works like you want it to.但否则它会像你想要的那样工作。 I'll post the full code.我会发布完整的代码。 The Text toggles from true to false and back again.文本从 true 切换到 false 然后再返回。

class Parent extends StatefulWidget {
  @override
  ParentState createState() => ParentState();
}

class ParentState extends State<Parent> with SingleTickerProviderStateMixin {
  bool isSelect = false;

  toggleSelect() {
    setState(() {
      isSelect = !isSelect;
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: FlatButton(
            onPressed: toggleSelect,
            child: Column(
              children: <Widget>[
                Icon(Icons.message,
                    color: isSelect ? Colors.blue : Colors.grey),
                Text(
                  "Select",
                  style: TextStyle(
                      fontSize: 14,
                      color: isSelect ? Colors.blue : Colors.grey),
                )
              ],
            ),
          ),
        ),
        body: Child(isSelect: isSelect));
  }
}

class Child extends StatefulWidget {
  bool isSelect;
  Child({this.isSelect});

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

class ChildState extends State<Child> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return Text(widget.isSelect
        .toString()); // here is a listView but i have done like this just for example
  }
}

点击前 点击后

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

相关问题 如何启用子组件对父应用栏进行更改? - How to enable a child component to make changes to a parent's appbar? 如何从子小部件更新父小部件的状态并将其反映在子小部件中? - How to update state of parent widget from child widget and reflect it in child widget? flutter如何调用父组件的子组件方法? - How to call method in child component from parent in flutter? 如何在 flutter 中将 set state 值更改从父窗口小部件传递给子窗口小部件? - How to pass set state value changes from parent to child widget in flutter? flutter父组件如何调用子组件? - How does the flutter parent component call a child component? 父下拉列表的选择更改后如何更新子下拉列表? - How to update child dropdown after parent dropdown's selection changes? flutter 问题:当我从 flutter 中的另一个页面搜索时,如何反映另一个页面中的更改 - flutter problem:how to reflect changes in another page when i search from another page in flutter Flutter如何防止孩子与父母一起旋转? - Flutter How to prevent child from Rotating with Parent? 如何从父级中的子小部件显示 CircularProgressIndicator()? - How to show CircularProgressIndicator() from a child widget in the parent? 如何从子更新父小部件的 state - How to update state of parent widget from child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM