列表视图.在另一个listview.builder中



在第一个里面的listView.builder,当我滚动屏幕删除数据时。我使用.insert这将在列表中添加另一个小部件。

在第一个ListView。Builder:数据不会消失

这就是我做的。

这是第一个保存数据的listView。

class _TestsPage extends State<TestsPage> with TickerProviderStateMixin {
final _commentController = TextEditingController();
bool _isWriting = false;
final List<CommentaryBox> _commentariBox = [];
@override
Widget build(BuildContext context) {
final model = Provider.of<Model>(context);
return DraggableScrollableSheet(
expand: false,
maxChildSize: 0.8,
initialChildSize: 0.6,
minChildSize: 0.6,
builder: (BuildContext context, ScrollController controller) => Column(
children: [
Expanded(
child: ListView.builder(
controller: controller,
physics: const BouncingScrollPhysics(),
itemBuilder: (_, i) => _commentariBox[i],
itemCount: _commentariBox.length,
//
reverse: false,
),
),

第二个列表视图。

Visibility(
visible: _showComments,
child: ExpansionTile(

// initiallyExpanded: true,
title: _deployText
? Text('see less commentaries')
: Text('see commentaries'),
onExpansionChanged: (bool expanded) {
setState(
() {
_deployText = expanded;
},
);
},
children: [
ListView.builder(
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemBuilder: (_, i) => responseBox[i],
itemCount: responseBox.length,
reverse: true,
),
],
),
),

我插入数据到列表的方式是相同的

_handleResponse(String reply) {
final model = Provider.of<Model>(context, listen: false);
if (reply.isEmpty) return;
respController.clear();
final newAnswer = ResponseWidget(
reply: reply,
animationController: AnimationController(
vsync: this,
duration: Duration(milliseconds: 400),
),
);
responseBox.insert(0, newAnswer);
newAnswer.animationController.forward();
setState(() {
model.showComments= true;
});
}
}

我找到解决方案了!

我只需要在我的appState中添加这个:AutomaticKeepAliveClientMixin

构造函数this中的:

@override小部件构建(BuildContext) {super.build(上下文);

并添加实现:

@override//TODO:实现wantKeepAlivebool get wantKeepAlive =>真的,

最新更新