請我試圖通過路由將陣列資料從一個螢屏傳遞到另一個螢屏。下面是我的代碼。謝謝你們。
class BallGamesWidget extends StatefulWidget {
@override
BallGamesWidgetState createState() => new BallGamesWidgetState();
}
class BallGamesWidgetState extends State {
Map<String, bool> List = {
'Bubble Football ?': false,
'Futsal ??': false,
'Beach Volleyball ??': false,
'Volleyball ??': false,
'Dodgeball ??': false,
'Rugby ??': false,
'American Footbal ??': false,
'Korftbal ??': false,
'Netbal ?': false,
};
var holder_1 = [];
getItems() {
List.forEach((key, value) {
if (value == true) {
holder_1.add(key);
}
});
從我的螢屏之一。我通過串列映射并將真實值存盤到holder_1
AppLargeButton(
text: "Next",
textColor: Colors.white,
backgroundColor: Colors.black,
onTap: () {
// getItems();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BallGamesSelectedItems(holder_1),
),
);
}),
然后從按鈕,我試圖使用 Navigator.push 將 holder_1 推送到我的第二個 scrren 'BallGamesSelectedItems()'
首先,我不知道我對 Naviagtion.push 的使用是否正確,其次,我不知道如何在我的第二個螢屏中取回它。謝謝
uj5u.com熱心網友回復:
參考:https : //docs.flutter.dev/cookbook/navigation/passing-data
第一屏,
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BallGamesSelectedItems(holder_1:holder_1),
),
);
第二個螢屏,
class BallGamesSelectedItems extends StatefulWidget {
final var holder_1;
BallGamesSelectedItems(Key key, this.holder_1});
@override
_BallGamesSelectedItemsState createState() => _BallGamesSelectedItemsState(holder_1);
}
class _BallGamesSelectedItemsState extends State<BallGamesSelectedItems>
{
var holder_1;
}
uj5u.com熱心網友回復:
正如您的問題所說,您喜歡使用路由傳遞資料,但是您正在通過建構式傳遞資料。您可以查看更多有關navigation-with-arguments 的詳細資訊。
使用 ModalRoute 傳遞資料
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => BallGamesSelectedItems(),
settings: RouteSettings(
arguments: holder_1,
)),
);
在build方法中接收資料,如
final data = ModalRoute.of(context)!.settings;
我強烈建議檢查這個關于傳遞資料和導航引數的答案。
uj5u.com熱心網友回復:
請參考以下代碼
class BallGamesWidget extends StatefulWidget {
BallGamesWidget({Key key}) : super(key: key);
@override
BallGamesWidgetState createState() => new BallGamesWidgetState();
}
// Change this
class BallGamesWidgetState extends State<BallGamesWidget> {
Map<String, bool> List = {
'Bubble Football ?': false,
'Futsal ??': false,
'Beach Volleyball ??': false,
'Volleyball ??': false,
'Dodgeball ??': false,
'Rugby ??': false,
'American Footbal ??': false,
'Korftbal ??': false,
'Netbal ?': false,
};
var holder_1 = [];
getItems() {
List.forEach((key, value) {
if (value == true) {
holder_1.add(key);
}
});
}
}
請參考下面的示例代碼來傳遞引數
class FirstScreen extends StatefulWidget {
const FirstScreen({Key key}) : super(key: key);
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(
dataList: ["Data"],
),
),
);
},
child: Text(
"Second Screen",
),
),
),
);
}
}
class SecondScreen extends StatefulWidget {
final List dataList;
const SecondScreen({Key key, this.dataList}) : super(key: key);
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
widget.dataList[0].toString(),
),
),
);
}
}
BallGamesSelectedItem 螢屏
class BallGamesSelectedItems extends StatefulWidget {
final List? holder_1;
BallGamesSelectedItems({this.holder_1, Key? key}) : super(key: key);
@override
_BallGamesSelectedItemsState createState() => _BallGamesSelectedItemsState();
}
class _BallGamesSelectedItemsState extends State<BallGamesSelectedItems> {
@override
// var holder_1;
Widget build(BuildContext context) {
// final data = ModalRoute.of(context)!.settings;
double h = MediaQuery.of(context).size.height;
double w = MediaQuery.of(context).size.width;
return Scaffold(
body: Column(
children: [
Flexible(
fit: FlexFit.tight,
child: Container(
padding: EdgeInsets.only(top: 0, bottom: 10, left: 0, right: 0),
color: Colors.white,...
//===============
Container(
child: Center(
child: Text(
widget.holder_1.toString(),
),
),
),
uj5u.com熱心網友回復:
在您的螢屏頁面上,您宣告并傳遞建構式
class BallGamesSelectedItems {
final List holder_1;
BallGamesSelectedItems(this.holder_1)
}
如果你用過stateful然后可以在你的螢屏上呼叫它widget.holder_1
class BallGamesWidget extends StatefulWidget {
@override
BallGamesWidgetState createState() => new BallGamesWidgetState();
}
class BallGamesWidgetState extends State {
Map<String, bool> list = {
'Bubble Football ?': false,
'Futsal ??': false,
'Beach Volleyball ??': false,
'Volleyball ??': false,
'Dodgeball ??': false,
'Rugby ??': false,
'American Footbal ??': false,
'Korftbal ??': false,
'Netbal ?': false,
};
getItems() {
list.forEach((key, value) {
if (value == true) {
widget.holder_1.add(key);
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375660.html
