【发布时间】:2020-10-21 08:46:39
【问题描述】:
我正在尝试隐藏传递的字符串并将其转换为已知的列表名称。
我有一个包含字符串、字符串键值对的 Map,并且我有 2 个列表。列表名称保存在地图的关键字段中。当我将列表名称传递给我的小部件时,我希望它在列表图块中向我显示列表内容,但我不知道如何将该字符串转换为列表名称。这是我的代码:
我在第 71 行发送,在第 87 行接收。
import 'package:flutter/material.dart';
void main() {
runApp(ListOfHomes());
}
////This is my list of homes this will even eventually come from firestore
Map<String, String> homes = {
'home1': 'The one with the little yellow roof',
'home2': 'The one with the outhouse',
};
//Home one has these rooms this will even eventually come from firestore
List<String> home1 = [
'Room1',
'Room2',
];
//Home2 has these rooms this will even eventually come from firestore
List<String> home2 = [
'Room1',
'Room2',
];
//This is OK for the moment
class ListOfHomes extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Home List'),
),
body: SafeArea(
child: Column(
children: [
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: homes.length,
itemBuilder: (context, index) {
String key = homes.keys.elementAt(index);
return RowsWithData(roomDecor: key, roomName: homes[key]);
})
],
),
),
));
}
}
class RowsWithData extends StatelessWidget {
RowsWithData({this.roomDecor, this.roomName});
final String roomName;
final String roomDecor;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
FlatButton(
onPressed: () {
print(roomDecor);
Navigator.push(
context,
MaterialPageRoute(
///this is where my problem lies I'm sending a String
builder: (context) => ListOfRooms(roomlist: roomDecor)));
},
child: ListTile(
title: Text(roomDecor),
subtitle: Text(roomName),
),
),
],
);
}
}
class ListOfRooms extends StatelessWidget {
///I'm receiving the string name here, but I need to return the corresponding List name
///
final String roomlist;
ListOfRooms({this.roomlist});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('list of rooms'),
),
body: SafeArea(
child: Column(
children: [
ListView.builder(
itemCount: roomlist.length,
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, position) {
return ListTile(title: Text('${roomlist[position]}'));
}),
],
),
));
}
}
【问题讨论】: