简体   繁体   中英

ListView.builder inside another ListView.builder

The listView.builder that is inside of the first, when i scroll the screen delete the data. I used .insert that adds another widget inside the list.

in the first ListView.builder the data does not dissapear.

this what i do.

this is the first listView that keeps the data.

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.

 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

in the constructor this:

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

and add the implemetation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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