【发布时间】:2021-12-21 06:31:04
【问题描述】:
我在 _buildCategoryListings() 中有一个扩展面板,当单击标题或下拉按钮时它不会展开。 isExpanded 设置为布尔 categoryView.isExpanded。通过控制台打印,我可以看到 setState 实际上正在更新 bool 值,但看起来实际的小部件可能没有被重绘?如果我手动将 isExpanded 设置为 true,我会从 GUI 中看到我想要的结果。我还将 isExtended 设置为 theExpanded(位于 MovieListingView 中),这引发了一个可变变量位于扩展 StatefulWidget 的类中的问题,但这确实给了我想要的结果。
问题:如何让扩展面板更新 categoryView.isExpanded(通过 theListings[panelIndex].isExpanded)bool 并通过 GUI 显示?
提前谢谢你。
旁注我曾考虑使用提供程序来跟踪此布尔值,但这似乎有点过头了。
class MovieListingView extends StatefulWidget {
@override
_MovieListingView createState() => _MovieListingView();
MovieListingView(this.movieList);
final MovieCatalog movieList;
//bool theExpanded = false;
List<MovieCategoryView> generateCategoryList() {
List<MovieCategoryView> tempList = [];
List<String> movieCategories = movieList.Categories;
movieCategories.forEach((category) {
MovieCategoryView categoryView = new MovieCategoryView(
movieCategoryName: category.toString(),
movieList: movieList.getMovieCardListByCategory(category));
tempList.add(categoryView);
});
return tempList;
}
}
class _MovieListingView extends State<MovieListingView> {
Widget build(BuildContext context) {
// TODO: implement build
return SingleChildScrollView(
physics: ScrollPhysics(),
padding: EdgeInsets.all(5.0),
child: _buildCategoryListings(),
);
}
List<MovieCategoryView> generateCategoryList() {
List<MovieCategoryView> tempList = [];
List<String> movieCategories = widget.movieList.Categories;
int counter = 0;
movieCategories.forEach((category) {
MovieCategoryView categoryView = new MovieCategoryView(
movieCategoryName: category.toString(),
movieList:
widget.movieList.getMenuItemCardListByCategory(category),
isExpanded: false);
tempList.add(categoryView);
});
return tempList;
}
Widget _buildCategoryListings() {
final List<MovieCategoryView> theListings = generateCategoryList();
return ExpansionPanelList(
expansionCallback: (panelIndex, isExpanded) {
setState(() {
theListings[panelIndex].isExpanded = !isExpanded;
//widget.theExpanded = !isExpanded;
});
},
children: theListings.map((MovieCategoryView movieCategoryView) {
return ExpansionPanel(
canTapOnHeader: true,
headerBuilder: (BuildContext context, bool isExpanded) {
return ListTile(
title: Text(movieCategoryView.movieCategoryName),
);
},
body: Column(
children: movieCategoryView.movieList,
),
isExpanded: movieCategoryView.isExpanded);
}).toList(),
);
}
}
class MovieCategoryView {
MovieCategoryView(
{@required this.movieCategoryName,
@required this.movieList,
this.isExpanded});
String movieCategoryName;
List<MovieCard> movieList;
bool isExpanded = false;
}
【问题讨论】:
标签: flutter dart user-interface