[我看了官方文档flutter expansion panels,作为建立自己的文档的参考。我无法让它们正常工作并无法处理我的数据。我尝试过将我的小部件接收的一些数据传递到其构造函数中,以在生成面板列表时使用。我希望这些代码片段足够,如果不能,我可以添加更多。
这里是弹出错误的地方(当尝试引用widget.contracts时]
class FinancialTabContent extends StatefulWidget {
final List<Contract> contracts;
final Person travelOfficer;
FinancialTabContent({this.contracts, this.travelOfficer});
@override
_FinancialTabContentState createState() => _FinancialTabContentState();
}
class _FinancialTabContentState extends State<FinancialTabContent> {
List<ExpandableItem> expansionPanels = generateExpansionPanels(widget.contracts);
@override
Widget build(BuildContext context) {
...
这是我打算用来构建面板列表的内容,它位于_FinancialTabContentState内
Widget _buildPanelList() {
return ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
expansionPanels[index].isExpanded = !isExpanded;
});
},
children: expansionPanels.map<ExpansionPanel>((ExpandableItem item) {
return ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
....
isExpanded: item.isExpanded,
);
}).toList(),
);
这是展开式项目的代码
import 'contract.dart';
class ExpandableItem {
ExpandableItem({
this.isExpanded = false,
this.panelData
});
bool isExpanded;
Contract panelData;
}
List<ExpandableItem> generateExpansionPanels(List<Contract> panelsData) {
return List.generate(panelsData.length, (int index) {
return ExpandableItem(
panelData: panelsData[index],
);
});
}
将此行移到build
方法内:
List<ExpandableItem> expansionPanels = generateExpansionPanels(widget.contracts);
或者如果只希望执行一次,则在构造函数中执行此操作:
class _FinancialTabContentState extends State<FinancialTabContent> {
List<ExpandableItem> expansionPanels;
_FinancialTabContentState() {
expansionPanels = generateExpansionPanels(widget.contracts);
}
@override
Widget build(BuildContext context) {...}
}