[英]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.