Search code examples
flutterflutter-animation

Flutter List view builder doesn't shrink when Keyboard appears


I'm creating a chat feature in flutter but noticed this behavior on IOS that doesnt shrink the list so you can see the last sent message. How can I have the listview builder shrink to show the last message when the keyboard appears?

Note: This issue doesn't happen on Android

Scaffold(
      resizeToAvoidBottomInset: true,
    body: Stack(
       children: <Widget>[
         StreamBuilder(
        stream: _chats,
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Container();
          return snapshot.hasData
              ? GestureDetector(
            onPanDown: (_) {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: ListView.builder(
                shrinkWrap: true,
                controller: _scrollController,
                padding: EdgeInsets.only(top: 10, bottom: 100),
                itemCount: snapshot.data.docs.length,
                itemBuilder: (context, index) {
                  return MessageWidget(
                    tripId: widget.docId,
                    uid: snapshot.data.docs[index].data()["uid"],
                    messageId: snapshot.data.docs[index].id,
                    message: snapshot.data.docs[index].data()["message"],
                    sender: snapshot.data.docs[index].data()["senderName"],
                    sentByMe: widget.uid ==
                        snapshot.data.docs[index].data()["uid"],
                    mediaFileUrl:
                    snapshot.data.docs[index].data()["mediaFileUrl"],
                  );
                }),
          )
              : Container();
        },
          );
      ]
     )
    )   

Keyboard Opened

Keyboard Closed


Solution

  • I think you can try the 'reverse' property from the ListView.builder. Tell me if this example didn't fit your needs, can you share us your code ? (I didn't see why you use a Stack and what could be the issue around that).

    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[
              Expanded(
                child: Stack(
                  children: <Widget>[
                    StreamBuilder<dynamic>(
                      builder: (context, dynamic snapshot) {
                        return GestureDetector(
                          onPanDown: (_) {
                            FocusScope.of(context).unfocus();
                          },
                          child: ListView.builder(
                            reverse: true,
                            shrinkWrap: true,
                            itemCount: 100,
                            padding: const EdgeInsets.only(top: 10, bottom: 10),
                            itemBuilder: (context, index) {
                              return ListTile(title: Text(index.toString()));
                            },
                          ),
                        );
                      },
                    ),
                  ],
                ),
              ),
              Container(
                padding: const EdgeInsets.all(8),
                color: Colors.black12,
                child: const TextField(),
              ),
            ],
          ),
        );
      }
    }