Flutter ExpansionPanelList.radio-如何在重建时保留扩展

我可以正常创建并显示ExpansionPanelList.radio。它的children属性包含ExpansionPanelRadio个项目的列表。它可以正常工作-除了一件事:

当我导航至其他标签,然后返回包含我的ExpansionPanelList.radio的标签时,先前展开的ExpansionPanelRadio项现在被折叠。

我希望能够保留最后一个扩展的ExpansionPanelRadio的扩展名,但是它的isExpanded属性是只读的。与ExpansionPanel不同,ExpansionPanelRadio并未在其构造函数中定义isExpanded

关于如何实现此目标的任何想法? 谢谢!

编辑:为清楚起见,这是显示EpxpansionPalnelList.radio的代码:

class ProductsByApplicationWidget extends StatelessWidget {
  final ProductsByApplication productsByApplication;
  final int indexOfExpandedFieldOfApplication;

  const ProductsByApplicationWidget(
      {Key key,@required this.productsByApplication,@required this.indexOfExpandedFieldOfApplication})
      : assert(productsByApplication != null),assert(indexOfExpandedFieldOfApplication != null),super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _buildPanel(),);
  }

  Widget _buildPanel() {
    return ExpansionPanelList.radio(
      animationDuration: Duration(milliseconds: 300),children:
      productsByApplication.productsForApplicationList
          .map<ExpansionPanel>((ProductsForApplication productsForApplication) {
        return ExpansionPanelRadio(
          canTapOnHeader: true,headerBuilder: (BuildContext context,bool isExpanded) {
            return FieldOfApplicationHeaderCellWidget(application: productsForApplication.application,);
          },body: Container(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8.0),child: Column(
                children:
                productsForApplication.products.map<ProductCellWidget>((Product product) {
                  return ProductCellWidget(product: product);
                }).toList(),),value: productsForApplication.application.id,);
      }).toList(),);
  }
}
xu8269909 回答:Flutter ExpansionPanelList.radio-如何在重建时保留扩展

要保持状态,请使用Stack来实现Offstage。这样做将保持您的状态。顺便说一句,您也可以使用Key方法添加go。

示例:

import 'package:bottom_navy_bar/bottom_navy_bar.dart';
import 'package:flutter/material.dart';

class Demo extends StatefulWidget {

  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {

  int _currentIndex = 0; // to keep the index of currently selected tab

  Stack _bodyWidgets() {
    return Stack( children: <Widget>[
      Offstage(
          offstage: _currentIndex != 0,child: TickerMode(enabled: _currentIndex == 0,child: _screen1Body())),Offstage(
        offstage: _currentIndex != 1,child: TickerMode(
          enabled: _currentIndex == 1,child: Text(("Screen 2"),),)),Offstage(
        offstage: _currentIndex != 2,child: TickerMode(
          enabled: _currentIndex == 2,child: Text("Screen 3")),]);
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
          title: Text("DEMO"),body: _bodyWidgets(),bottomNavigationBar: BottomNavyBar(
            selectedIndex: _currentIndex,onItemSelected: (index) => _onTabTapped(index),showElevation: true,items: [
              BottomNavyBarItem(
                  icon: Icon(Icons.home),title: Text('Screen1')),BottomNavyBarItem(
                  icon: Icon(Icons.photo),title: Text('Screen2'),BottomNavyBarItem(
                  icon: Icon(Icons.notifications),title: Text('Screen3'),]));
  }

  void _onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  _screen1Body() {
    return ListView.builder(
      itemCount: 100,itemBuilder: (context,i) {
        return Padding(padding: EdgeInsets.all(5),child: Text(i.toString()));
      },);
  }
}
本文链接:https://www.f2er.com/2925543.html

大家都在问