Flutter Expansion Panel 在没有不可变布尔的情况下不会扩展

Flutter Expansion Pannel not Expanding without immutable bool

我在 _buildCategoryListings() 中有一个扩展面板,在单击 header 或下拉按钮时不会扩展。 isExpanded 设置为布尔值 categoryView.isExpanded。通过控制台打印,我可以看到 setState 实际上正在更新 bool 值,但看起来实际的小部件可能没有被重绘?如果我手动将 isExpanded 设置为 true,我会从 GUI 中看到我想要的结果。我还将 isExtended 设置为 theExpanded(在 MovieListingView 中),这引发了一个可变变量在扩展 StatefulWidget 的 class 中的问题,但这确实给了我想要的结果。

问题:如何让扩展面板更新 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;
  
}

发生这种情况是因为每当调用 setstate() 时,整个小部件树都会重建,因此当您尝试更改 isexpandable 值时,它会发生变化,但是 函数 generateCategoryList();再次被调用并一次又一次地生成之前的列表。

Widget _buildCategoryListings() {
    final List<MovieCategoryView> theListings = generateCategoryList();
  

要修复此问题,请调用 generateCategoryList();一旦进入 initState() 并删除上面的行。