[英]How to send onPressed value to another widget Flutter
I want to send data from widget to another widget, in my example I want to send onPressed value as a variable.我想将数据从小部件发送到另一个小部件,在我的示例中,我想将 onPressed 值作为变量发送。
appBar: CancelAppBar(onPressed: ),
What should I write after onPressed (in the above code) to send the value: Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));
我应该在 onPressed 之后写什么(在上面的代码中)来发送值:
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));
to a widget in a seperate dart file?到单独的 dart 文件中的小部件?
Following is the other file:以下是另一个文件:
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,
);
}
}
You can access any StatefulWidget
variable in the State
class with widget
getter, like:您可以使用
widget
getter 访问State
类中的任何StatefulWidget
变量,例如:
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: widget.onPressed, /// widget.yourVariable
icon: Icon(Icons.close),
)
],
),
backgroundColor: AppColors.dark,
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.