1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Go to the bottom on chat

Discussão em 'Mobile' iniciado por Stack, Outubro 26, 2024 às 08:23.

  1. Stack

    Stack Membro Participativo

    I'd like to go to the bottom in the list builder, on the last message and auto scroll to the bottom. I would like the newer messages to the bottom like WhatsApp or other chat.

    I'm tested a lot of solution without success. Please let me know how can I solve this issue.

    Thanks you.

    here the code :

    final ScrollController _scrollController = ScrollController();
    (...)
    test with scrollToBottom :
    void scrollToBottom() {
    _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
    }
    or :
    void scrollToBottom() {
    // Vérifier si le contrôleur est prêt pour défiler
    if (_scrollController.hasClients) {
    _scrollController.animateTo(
    _scrollController.position.maxScrollExtent,
    duration: const Duration(milliseconds: 300),
    curve: Curves.easeOut,
    );
    }
    }
    (...)
    @override
    void initState() {
    // TODO: implement initState
    super.initState();
    futureCreateDiscussion =
    createDiscussion(name: "", idUser: widget.varToPage);

    WidgetsBinding.instance.addPostFrameCallback((_) {
    scrollToBottom();
    });
    scrollToBottom();

    }
    (...)
    @override
    void dispose() {
    // TODO: implement dispose
    _textNotifier.dispose();
    _controllerEnvoieMessage.dispose();

    _scrollController.dispose();

    super.dispose();
    }

    (...)
    else {

    return ListView
    .builder(
    controller:
    _scrollController,
    // reverse: true,
    itemCount:
    snapshot
    .data
    ?.result
    .length,
    scrollDirection:
    Axis
    .vertical,
    shrinkWrap:
    true,
    physics:
    const NeverScrollableScrollPhysics(),
    itemBuilder:
    (BuildContext
    contex,
    int index) {
    String? lastUser = index ==
    0
    ? null
    : snapshot
    .data!
    .result[index -
    1]
    .firstname;

    bool isFirstMessageForUser = lastUser !=
    snapshot
    .data!
    .result[index]
    .firstname;

    return Align(
    alignment: snapshot.data!.result[index].isOwner ==
    false
    ? Alignment.topLeft
    : Alignment.topRight,
    child: Container(
    margin: const EdgeInsets.only(right: 15, left: 15),
    constraints: BoxConstraints(
    maxWidth: MediaQuery.of(context).size.width * 0.8,
    ),
    child: IntrinsicWidth(
    child: Card(

    (...)


    I would like to read the message with the last to the top and last to the bottom.

    Continue reading...

Compartilhe esta Página