简体   繁体   English

ListView.builder 在另一个 ListView.builder 中

[英]ListView.builder inside another ListView.builder

The listView.builder that is inside of the first, when i scroll the screen delete the data.第一个里面的listView.builder ,当我滚动屏幕时删除数据。 I used .insert that adds another widget inside the list.我使用.insert在列表中添加另一个小部件。

in the first ListView.builder the data does not dissapear.在第一个 ListView.builder 中,数据不会消失。

this what i do.这就是我所做的。

this is the first listView that keeps the data.这是第一个保留数据的 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,
            ),
          ),

second listView.builder that delete data.第二个 listView.builder 删除数据。

 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,
                  ),
                ],
              ),
            ),

the way how I insert data to the list is the same for both我将数据插入列表的方式对于两者都是相同的

_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;
    });
  }
}

I found the solution!我找到了解决方案!

I just had to add this in my appState this: AutomaticKeepAliveClientMixin我只需要在我的 appState 中添加这个: AutomaticKeepAliveClientMixin

in the constructor this:在构造函数中:

@override Widget build(BuildContext context) { super.build(context); @override Widget build(BuildContext context) { super.build(context);

and add the implemetation:并添加实现:

@override // TODO: implement wantKeepAlive bool get wantKeepAlive => true; @override // TODO: 实现 wantKeepAlive bool get wantKeepAlive => true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM