Flutter 如何使用新数据内容重建gridView
我创建了一个flatter应用程序,它显示了一个gridview,其中填充了冥想列表(从api服务获取它们),我有一些按钮(气泡选项卡栏),它们是冥想的类别,当用户点击它们时,用新内容刷新gridview项目(所有此类冥想)。 在我的主屏幕中,我对categoryid=0调用如下的冥想网格小部件:Flutter 如何使用新数据内容重建gridView,flutter,Flutter,我创建了一个flatter应用程序,它显示了一个gridview,其中填充了冥想列表(从api服务获取它们),我有一些按钮(气泡选项卡栏),它们是冥想的类别,当用户点击它们时,用新内容刷新gridview项目(所有此类冥想)。 在我的主屏幕中,我对categoryid=0调用如下的冥想网格小部件: Expanded( child: MeditationsGrid( categoryId: 0, key: UniqueKey(),
Expanded(
child: MeditationsGrid(
categoryId: 0,
key: UniqueKey(),
),
这是冥想网格:
class MeditationsGrid extends StatefulWidget {
MeditationsGrid({Key key, this.categoryId}) : super(key: key);
final int categoryId;
@override
MeditationGrid createState() => MeditationGrid();
}
class MeditationGrid extends State<MeditationsGrid>
with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Meditation>>(
future: fetchMeditations(http.Client(), widget.categoryId),
builder: (context, snapshot) {
if (snapshot.hasData) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1,
//padding: EdgeInsets.all(16),
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: snapshot.data.length,
padding: EdgeInsets.all(16),
itemBuilder: (context, index) {
return MeditationCard(
title: snapshot.data[index].title,
image: snapshot.data[index].picPath,
route: FocusedMeditationScreen(),
);
},
);
}
if (snapshot.hasError) print(snapshot.error.toString());
return Scaffold(
body: Center(child: CircularProgressIndicator()),
);
},
);
}
}
点击第二个选项卡按钮时的打印结果:1,0
但并没有任何变化,小部件也并没有重建。
我在这里该怎么办?
有人能帮我吗,或者有什么解决办法吗
Future _handleTabChange() async {
print(_tabController.index);
// setState(() {
// MeditationsGrid(
// categoryId: _tabController.index,
// key: UniqueKey(),
// );
// });
// what should I do here
}