[英]Flutter/Hive - How to write Widget positional arguments?
(很抱歉,我只能發布我的部分代碼,我希望這里的代碼足夠了。)
在這段代碼中,我需要將buildApiBox
調用到page_ApiBox
class。但我還需要傳遞apibox.api_key
,它已經連接到另一個 class 中的變量。
任何建議表示贊賞。 我是 Flutter/Dart 的新手。
(部分)我的代碼:
class page_ApiBox extends StatefulWidget {
const page_ApiBox({Key? key}) : super(key: key);
@override
State<page_ApiBox> createState() => _page_ApiBoxState();
}
class _page_ApiBoxState extends State<page_ApiBox> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: buildApiBox(context, apibox)
);
}
}
Widget buildApiBox(BuildContext context, apiBox apibox) {
return Scaffold(
appBar: AppBar(
title: Text('API BOX')
),
body: Text(apibox.api_key) //I need to display what is inside the variable
);
}
您可以在此處像這樣在page_ApiBox
構造函數中傳遞參數。
class page_ApiBox extends StatefulWidget {
const page_ApiBox({Key? key, required this.apibox}) : super(key: key);
final apiBox apibox;
@override
State<page_ApiBox> createState() => _page_ApiBoxState();
}
然后,如果你想訪問你的變量,你需要使用widget.
因為它是一個StatefulWidget
class _page_ApiBoxState extends State<page_ApiBox> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: buildApiBox(context, widget.apibox)
);
}
}
Widget buildApiBox(BuildContext context, apiBox apibox) {
return Scaffold(
appBar: AppBar(
title: Text('API BOX')
),
body: Text(apibox.api_key)
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.