我是扑朔迷离的新手,并一直在尝试创建一个基于用户选择刷新ListView.builder的函数。我正在将城市名称保存为用户集合中我的firestore文档中的字符串。 我有多个按钮可以显示不同的城市,并且根据选择,我需要ListView构建器进行重建。我一直在努力寻找解决方案。 这里有人可以帮忙吗?
这就是我从Firestore检索数据的方式
StreamBuilder(
stream: Firestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Text('loading...');
return Container(
width: 890.0,
height: 320.0,
margin: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 00.0),
child: new ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
User user = User.fromDoc(snapshot.data
.documents[index]);
return Padding(
padding: const EdgeInsets.only(top: 0),
child: Container(
height: 300,
width: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(0),
),
child: _buildCard(user)),
);
}),
);
},
),
答案 0 :(得分:0)
我刚刚编写了这段代码来显示静态城市的实现,单击按钮会更改索引,然后更改文本(您将其更改为具有自定义城市流的流构建器),还可以将其缩放为动态通过操作城市列表进行列表。
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int stackIndex = 0;
final List<String> cities = ['Berlin', 'Denver', 'Nairobi', 'Tokyo', 'Rio'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample'),
),
body: Center(
child: Column(
mainAxisAlignment : MainAxisAlignment.spaceEvenly,
children : [
Row(
mainAxisAlignment : MainAxisAlignment.spaceEvenly,
mainAxisSize : MainAxisSize.max,
children : cities.map((city){
return RaisedButton(
child : Text(city),
onPressed : (){
setState((){
this.stackIndex = cities.indexOf(city);
});
}
);
}).toList()
),
IndexedStack(
index : stackIndex,
children: cities.map((city){
return yourStreamBuilder(city);
}).toList()
),
])
),
);
}
Widget yourStreamBuilder(String city){
//you can use your custom stream here
//Stream stream = Firestore.instance.collection('users').where('myCity', isEqualTo: city).snapshots();
return Text(city);//replace this with your streamBuilder
}
}
答案 1 :(得分:0)
int stackIndex = 0;
最终列表城市= [ “斯德哥尔摩”, “马尔默”, “乌普萨拉”, “Västerås”, 'Örebro', “林雪平”, “赫尔辛堡”, '延雪平', “诺尔雪平”, “隆德” 'Umeå', 'Gävle', 'Södertälje', 'Borås', 'Huddinge', 'Eskilstuna', 'Nacka', “哈姆斯塔德” 'Sundsvall', 'Södertälje', 'Växjö', “卡尔斯塔德”, “海宁” “克里斯蒂安斯塔德”, 'Kungsbacka', 'Solna', 'Järfälla', 'Sollentuna', 'Skellefteå', 'Kalmar', “ Varberg”, 'Östersund', 'Trollhättan', 'Uddevalla', “尼雪平”, 'Skövde', ];
@override 小部件build(BuildContext context){ 返回脚手架(
body: Center(
child: Column(
mainAxisAlignment : MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
mainAxisAlignment : MainAxisAlignment.spaceEvenly,
mainAxisSize : MainAxisSize.max,
children: cities.map((city) {
return OutlineButton(
child: Text(city),
onPressed: (){
setState(() {
this.stackIndex = cities.indexOf(city);
});
},
);
}).toList()
),
IndexedStack(
index: stackIndex,
children: cities.map((city){
return myStreamBuilder(city);
})
)
],
),
),
);
}
小部件myStreamBuilder(字符串城市){ 流stream = Firestore.instance.collection('users')。where('myCity',isEqualTo:city).snapshots(); 返回Text(city); } }