The keyboard that vanished when the Flutter chat input expanded
You tap the expand button in a chat composer. The input flashes, grows to a few lines, and then the keyboard disappears. The screen is now a large, mostly empty editor, and the thing you were typing with is gone. On top of that, opening the keyboard in the first place feels heavier than it should — not broken, just a beat behind your thumb. Both symptoms live in the same widget, but they have…
The erratic behavior of a Flutter chat input began when the expand button was tapped. The input expanded into a larger editor, leaving the previously used keyboard behind. The keyboard's disappearance and sluggish response seemed to stem from a single widget, but the issues had different origins. One problem was a mistake in the widget tree, while the other stemmed from a subscription that unintentionally expanded the animation to the entire screen.
This article delves into the causes, solutions, and key principles that ensure a smooth composer experience.
Issue One: Keyboard Dropping on Expand
The composer initially had two layouts: a collapsed floating card and an expanded full-height editor. To switch between these states, the composer swapped the widget returned from its wrapper. This change was not a layout modification but a replacement of the widget type, causing Flutter to unmount the existing subtree and rebuild a new one.
This restructuring process led to the keyboard's disappearance and the editor's flashing appearance. Additionally, the expanded height appeared incorrect due to the keyboard's temporary absence.
The underlying issue was that the TextField, responsible for user input, was discarded when the editor was rebuilt, causing the platform input method to close. This resulted in the keyboard being hidden and the editor's layout being rebuilt from scratch. The first principle to remember is to change siblings instead of the entire element chain.
By keeping the composer content in the same position and altering only the surrounding elements, the backdrop can be managed as a sibling, preserving the content's stability. This approach avoids unnecessary reparenting or replacement of the content.
Issue Two: Sluggish Keyboard Animation
The sluggish keyboard animation was another problem that arose from the same widget replacement issue. The keyboard did not appear abruptly but rather through a gradual animation, causing Flutter to resize the Scaffold body in response to the keyboard's changing height. This issue originated from two subscriptions - one for the LayoutBuilder and another for the MediaQuery.of(context).
Both subscriptions triggered a rebuild of the chat every time the keyboard animation occurred, leading to a frame-by-frame rebuild of the entire message list and composer.
The second principle to keep in mind is that a keyboard animation is a constraint animation, not a full layout rebuild. Instead of relying on LayoutBuilder to handle the entire chat's layout, a small layout-reporting proxy was introduced to report the body height. This proxy was then consumed by the composer, which only required the body height during the expanded state.
By caching the boolean decision to hide the header while the keyboard was up and recomputing it only when necessary, the composer no longer needed to rebuild on every animation frame. This approach ensured a smoother keyboard animation experience without affecting the overall chat layout.
In conclusion, by following these two principles - changing siblings instead of the element chain and treating keyboard animations as constraint animations - developers can ensure a more stable and responsive Flutter composer experience. These fixes address the keyboard dropping and sluggish animation issues, ultimately creating a more enjoyable user experience.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to