gpt4 book ai didi

Flutter ExpansionPanelList - expansionCallback 不工作

转载 作者:行者123 更新时间:2023-12-05 03:44:16 26 4
gpt4 key购买 nike

我通过使用 Provider 包而不是使 Widget 成为 statefulWidget 来实现 ExpansionPanelList。但是,expansionCallback 永远无法正常工作。如果我点击屏幕上的第一个折叠列表,它永远不会展开。具体来说,data[0].isExpandedSummitView.dart 中始终为 false。控制台没有报错。

这是我的 SummitView.dart:

import 'package:delta_y/modules/summits/model/Item.dart';
import 'package:delta_y/modules/summits/model/ItemsList.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SummitView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Summit/Milestone"),
),
body: ChangeNotifierProvider(
create: (context) => ItemsList(),
child: Summit(),
));
}
}

class Summit extends StatelessWidget {
@override
Widget build(BuildContext context) {
final itemModel = Provider.of<ItemsList>(context);
// List<Item> _data = generateItems(8);
itemModel.generateItems(8);

return Stack(children: <Widget>[
SingleChildScrollView(
child: Container(
child: ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
itemModel.setExpanded(index, isExpanded);
print("INDEX : $index ... $isExpanded");
},
children: itemModel.data.map<ExpansionPanel>((Item item) {
bool isData0Expanded = itemModel.data[0].isExpanded;
print('**********');
print(isData0Expanded);
print('**********');

return ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return ListTile(
title: Text(item.headerValue),
);
},
body: ListTile(
title: Text(item.expandedValue),
subtitle:
Text('To delete this panel, tap the trash can icon'),
trailing: Icon(Icons.delete),
onTap: () {
itemModel.deleteItem(item);
}),
isExpanded: item.isExpanded,
);
}).toList(),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: FractionallySizedBox(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white,
onPrimary: Colors.black,
),
onPressed: () {
Navigator.pop(context);
},
child: Text('Create Summit'),
),
widthFactor: 0.9,
)),
]);
}
}

这是 ItemsList.dart:

import 'package:flutter/cupertino.dart';
import 'Item.dart';

class ItemsList extends ChangeNotifier{
List<Item> _data;

List<Item> get data => _data;

generateItems(int numberOfItems) {
_data = List.generate(numberOfItems, (int index) {
return Item(
headerValue: 'Panel $index',
expandedValue: 'This is item number $index',
);
});
}

setExpanded(int index, bool isExpanded) {
_data[index].isExpanded = !isExpanded;
notifyListeners();
}

deleteItem(Item item) {
_data.removeWhere((currentItem) => item == currentItem);
notifyListeners();
}
}

这是 Item.dart:

import 'package:flutter/material.dart';

class Item{
Item({
@required this.expandedValue,
@required this.headerValue,
this.isExpanded = false,
});

String expandedValue;
String headerValue;
bool isExpanded;
}

最佳答案

问题是由这一行引起的:

class Summit extends StatelessWidget {
@override
Widget build(BuildContext context) {

// ... other lines

itemModel.generateItems(8); <----

在 build() 中调用方法时,每次屏幕从 Provider 接收到事件时都会调用该方法。在这种情况下,您的项目列表每次都会生成,这使得 isExpanded 始终为 false

要解决此问题,最好添加一些条件以防止重置项目列表,或者在创建 ItemsList 时生成项目,例如:

body: ChangeNotifierProvider(
create: (context) => ItemsList()..generateItems(8),
child: Summit(),

结果:

enter image description here

关于Flutter ExpansionPanelList - expansionCallback 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66557090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com