如何将 onPressed 值发送到另一个小部件 Flutter
How to send onPressed value to another widget Flutter
我想将数据从小部件发送到另一个小部件,在我的示例中,我想将 onPressed 值作为变量发送。
appBar: CancelAppBar(onPressed: ),
在onPressed之后(上面代码中)应该写什么来发送值:
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));
到单独的 dart 文件中的小部件?
以下是另一个文件:
class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
CancelAppBar({Key? key, required this.onPressed}) : super(key: key);
final ValueGetter<String> onPressed;
static final _appBar = AppBar();
@override
Size get preferredSize => _appBar.preferredSize;
@override
_CancelAppBarState createState() => _CancelAppBarState();
}
class _CancelAppBarState extends State<CancelAppBar> {
get onPressed => null;
@override
Widget build(BuildContext context) {
return AppBar(
titleSpacing: 0.0,
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 8.w)),
IconButton(
onPressed: ,
icon: Icon(Icons.close),
)
],
),
backgroundColor: AppColors.dark,
);
}
}
您可以使用 widget
getter 访问 State
class 中的任何 StatefulWidget
变量,例如:
此外,您可以注意到:
- 您的
onPressed
变量具有 ValueGetter<String>
类型 which is only a typedef to String Function()
IconButton
小部件的 onPressed
类型为 void Function()
然后你可以像这样使用你的变量:
class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
CancelAppBar({Key? key, required this.onPressed}) : super(key: key);
final ValueGetter<String> onPressed;
static final _appBar = AppBar();
@override
Size get preferredSize => _appBar.preferredSize;
@override
_CancelAppBarState createState() => _CancelAppBarState();
}
class _CancelAppBarState extends State<CancelAppBar> {
@override
Widget build(BuildContext context) {
return AppBar(
titleSpacing: 0.0,
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 8.w)),
IconButton(
onPressed: () {
/// Access your variable by [widget.onPressed]
widget.onPressed(); /// Call it inside this function because your variable doesn't match directly the [onPressed] of [IconButton] widget
},
icon: Icon(Icons.close),
)
],
),
backgroundColor: AppColors.dark,
);
}
}
我想将数据从小部件发送到另一个小部件,在我的示例中,我想将 onPressed 值作为变量发送。
appBar: CancelAppBar(onPressed: ),
在onPressed之后(上面代码中)应该写什么来发送值:
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));
到单独的 dart 文件中的小部件?
以下是另一个文件:
class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
CancelAppBar({Key? key, required this.onPressed}) : super(key: key);
final ValueGetter<String> onPressed;
static final _appBar = AppBar();
@override
Size get preferredSize => _appBar.preferredSize;
@override
_CancelAppBarState createState() => _CancelAppBarState();
}
class _CancelAppBarState extends State<CancelAppBar> {
get onPressed => null;
@override
Widget build(BuildContext context) {
return AppBar(
titleSpacing: 0.0,
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 8.w)),
IconButton(
onPressed: ,
icon: Icon(Icons.close),
)
],
),
backgroundColor: AppColors.dark,
);
}
}
您可以使用 widget
getter 访问 State
class 中的任何 StatefulWidget
变量,例如:
此外,您可以注意到:
- 您的
onPressed
变量具有ValueGetter<String>
类型 which is only a typedef toString Function()
IconButton
小部件的onPressed
类型为void Function()
然后你可以像这样使用你的变量:
class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
CancelAppBar({Key? key, required this.onPressed}) : super(key: key);
final ValueGetter<String> onPressed;
static final _appBar = AppBar();
@override
Size get preferredSize => _appBar.preferredSize;
@override
_CancelAppBarState createState() => _CancelAppBarState();
}
class _CancelAppBarState extends State<CancelAppBar> {
@override
Widget build(BuildContext context) {
return AppBar(
titleSpacing: 0.0,
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 8.w)),
IconButton(
onPressed: () {
/// Access your variable by [widget.onPressed]
widget.onPressed(); /// Call it inside this function because your variable doesn't match directly the [onPressed] of [IconButton] widget
},
icon: Icon(Icons.close),
)
],
),
backgroundColor: AppColors.dark,
);
}
}