我在 _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;
}
uj5u.com熱心網友回復:
發生這種情況是因為每當呼叫 setstate() 時,整個小部件樹都會被重建,因此當您嘗試更改 isexpandable 值時,它會被更改,但函式 generateCategoryList(); 再次被呼叫并一次又一次地生成以前的串列。
Widget _buildCategoryListings() {
final List<MovieCategoryView> theListings = generateCategoryList();
為了修復這個呼叫 generateCategoryList(); 一次在 initState() 并洗掉行上方的行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/354092.html
