我想將資料從小部件發送到另一個小部件,在我的示例中,我想將 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,
);
}
}
uj5u.com熱心網友回復:
您可以使用getter訪問類中的任何StatefulWidget
變數,例如:State
widget
此外,您還可以注意到:
- 您的
onPressed
變數的ValueGetter<String>
型別只是一個 typedefString Function()
- 在
onPressed
的IconButton
小部件有型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,
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338851.html
上一篇:Google表格中的查詢功能-根據另一列獲取列中的非空最后一個單元格
下一篇:分期付款計算功能