我无法将fn的值更改为
当我单击ontap时,我想在fn中设置值changeDetail!= null 图标功能(布尔型)更改详细信息;
在有状态的第二个小部件中
Function(bool) changedDetail ;
状态第二个小部件中的
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
return this.widget.changedDetail(false);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thLarge,
color: KwanjaiColors.grey3,
)),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
return this.widget.changedDetail(true);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thList,
color: KwanjaiColors.grey3,
)),
),
),
父级
bool clicktap = false;
bool choiceTap = false;
tapFn(bool choice) {
setState(() {
choice == true ? choiceTap = true : choiceTap = false;
});
}
在小部件父级中
body: Column(children: <Widget>[
Container(
child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
),
Expanded(
child: Container(
color: KwanjaiColors.bg,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
child: Column(children: <Widget>[
Container(child: ProjectIndexWorkDaily()),
Expanded(
// cuz has gridview must have expanded !!!!
child: Column(children: <Widget>[
Expanded(
child: choiceTap == true
? ProjectViewList()
: ProjectGridList())
]),
),
]),
),
),
),
])
I / flutter(12900):引发了另一个异常:NoSuchMethodError:方法'call'在null上调用。
答案 0 :(得分:1)
尝试一下:
在父窗口小部件中,传递不带任何参数的函数
我将假定您已经为第二个窗口小部件设置了键
另一方面,您不必检查choiceTap == true,默认情况下,如果bool为空,则将其设置为false
body: Column(children: <Widget>[
Container(
child: new ProjectIndexTab(changedDetail: tapFn),
),
Expanded(
child: Container(
color: KwanjaiColors.bg,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
child: Column(children: <Widget>[
Container(child: ProjectIndexWorkDaily()),
Expanded(
// cuz has gridview must have expanded !!!!
child: Column(children: <Widget>[
Expanded(
child: choiceTap
? ProjectViewList()
: ProjectGridList())
]),
),
]),
),
),
),
])
在您的有状态第二个小部件中
final void Function(bool value) changedDetail;
在第二个小部件中,您不必返回该函数
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
widget.changedDetail(false);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thLarge,
color: KwanjaiColors.grey3,
)),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
widget.changedDetail(true);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thList,
color: KwanjaiColors.grey3,
)),
),
),
答案 1 :(得分:0)
在父窗口小部件中
class ProjectIndexScreen extends StatefulWidget {
ProjectIndexScreen({Key key, this.title}) : super(key: key);
final String title;
@override
ProjectIndexScreenState createState() => ProjectIndexScreenState();
}
class ProjectIndexScreenState extends State<ProjectIndexScreen> {
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
});
}
void initState() {
super.initState();
}
bool clicktap = false;
bool choiceTap = false;
tapFn(bool choice) {
setState(() {
choice == true ? choiceTap = true : choiceTap = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: KwanjaiColors.title,
title: Container(
child: Row(
children: <Widget>[
Expanded(
flex: 7,
child: Container(
padding: EdgeInsets.only(right: 50.0),
child: Text(
"Choose Project",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: KwanjaiColors.white),
textAlign: TextAlign.end,
),
),
),
Expanded(
flex: 1,
child: Icon(
Icons.notifications,
color: KwanjaiColors.white,
),
),
Expanded(
flex: 1,
child: Icon(
Icons.menu,
color: KwanjaiColors.white,
),
)
],
),
),
),
body: Column(children: <Widget>[
Container(
child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
),
Expanded(
child: Container(
color: KwanjaiColors.bg,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
child: Column(children: <Widget>[
Container(child: ProjectIndexWorkDaily()),
Expanded(
// cuz has gridview must have expanded !!!!
child: Column(children: <Widget>[
Expanded(
child:
choiceTap ? ProjectViewList() : ProjectGridList())
]),
),
]),
),
),
),
]));
}
}
在第二个小部件中
class ProjectIndexTab extends StatefulWidget {
final void Function(bool value) changedDetail;
ProjectIndexTab({Key key, this.changedDetail}) : super(key: key);
@override
ProjectIndexTabState createState() => new ProjectIndexTabState();
}
class ProjectIndexTabState extends State<ProjectIndexTab> {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
color: KwanjaiColors.white,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 6,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 8.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(22.0))),
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(
vertical: 10.0, horizontal: 10.0),
hintText: "Search",
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: KwanjaiColors.grey2),
borderRadius: BorderRadius.all(Radius.circular(4.0)),
gapPadding: 0.0),
fillColor: KwanjaiColors.grey2,
filled: true,
hintStyle: TextStyle(color: KwanjaiColors.grey3),
prefixIcon: Icon(
Icons.search,
color: KwanjaiColors.grey3,
)),
),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
widget.changedDetail(false);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thLarge,
color: KwanjaiColors.grey3,
)),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {
setState(() {
widget.changedDetail(true);
});
},
child: Container(
padding: EdgeInsets.only(top: 12.0),
child: Icon(
FontAwesomeIcons.thList,
color: KwanjaiColors.grey3,
)),
),
),
],
),
);
}
}