// The input capsule. Focus opens it into two rows — the text above, the // model / mode / keys below — and losing focus folds it back to one line, the // way dsh-Go's composer behaves. The field keeps its state across the two // layouts through a GlobalKey. import 'package:flutter/material.dart'; class ComposerBox extends StatefulWidget { final TextEditingController controller; final FocusNode focusNode; final String hint; final int maxLines; /// The soft keyboard's height, from the page (a Scaffold removes the bottom /// view inset from its body, so the composer cannot read it itself). final double keyboardInset; final ValueChanged? onChanged; final ValueChanged? onSubmitted; final TextInputAction textInputAction; /// The attach key; shown on the text's line when folded and on the second /// row when open. final Widget leading; /// Model (and thinking level) pill, second row. final Widget? model; /// The mode (Build / Plan), second row and separate from [model]. final Widget? mode; /// The context ring, right of the keys. final Widget? accessory; /// Send or stop. final Widget trailing; const ComposerBox({ super.key, required this.controller, required this.focusNode, required this.hint, required this.leading, required this.trailing, this.model, this.mode, this.accessory, this.maxLines = 5, this.keyboardInset = 0, this.onChanged, this.onSubmitted, this.textInputAction = TextInputAction.newline, }); @override State createState() => _ComposerBoxState(); } class _ComposerBoxState extends State { // The editor moves between the one-line row and the two-line column; a // GlobalKey carries its state (and its input connection) across the swap. final GlobalKey _fieldKey = GlobalKey(); bool _open = false; // The keyboard went down with the field still focused (the phone's back // gesture closes the IME without unfocusing). dsh-Go folds the composer in // that case too, so the same rule is kept here. bool _keyboardDown = false; double _keyboardPeak = 0; @override void initState() { super.initState(); widget.focusNode.addListener(_onFocus); _open = widget.focusNode.hasFocus; } @override void dispose() { widget.focusNode.removeListener(_onFocus); super.dispose(); } @override void didUpdateWidget(ComposerBox oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.focusNode != widget.focusNode) { oldWidget.focusNode.removeListener(_onFocus); widget.focusNode.addListener(_onFocus); } if (oldWidget.keyboardInset != widget.keyboardInset) { _keyboardMoved(oldWidget.keyboardInset, widget.keyboardInset); // The build that follows reads this; no second one is needed. _open = widget.focusNode.hasFocus && !_keyboardDown; } } @override void didChangeDependencies() { super.didChangeDependencies(); // A sheet over the page takes the keyboard for a moment; when the page is // current again, settle on where the composer should be. WidgetsBinding.instance.addPostFrameCallback((_) => _syncOpen()); } /// A keyboard rising clears "down"; falling to well under its peak means it /// was dismissed (a height change alone stays open). void _keyboardMoved(double before, double inset) { if (!(ModalRoute.of(context)?.isCurrent ?? true)) return; if (inset > before) { _keyboardDown = false; if (inset > _keyboardPeak) _keyboardPeak = inset; } else if (inset < _keyboardPeak * 0.5) { _keyboardDown = true; _keyboardPeak = 0; } } void _onFocus() { if (widget.focusNode.hasFocus) _keyboardDown = false; _syncOpen(); } void _syncOpen() { if (!mounted) return; // The model sheet (or any page over this one) takes focus for a moment: // leave the composer as it is until it is current again. if (!(ModalRoute.of(context)?.isCurrent ?? true)) return; final open = widget.focusNode.hasFocus && !_keyboardDown; if (open == _open) return; setState(() => _open = open); } Widget _field() { return TextField( key: _fieldKey, controller: widget.controller, focusNode: widget.focusNode, minLines: 1, maxLines: widget.maxLines, keyboardType: TextInputType.multiline, textInputAction: widget.textInputAction, onChanged: widget.onChanged, onSubmitted: widget.onSubmitted, style: const TextStyle(fontSize: 15, height: 1.45), decoration: InputDecoration( hintText: widget.hint, hintMaxLines: 1, isDense: true, border: InputBorder.none, contentPadding: const EdgeInsets.symmetric(vertical: 6), ), ); } @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; final accessory = widget.accessory; return AnimatedContainer( duration: const Duration(milliseconds: 200), curve: Curves.easeOutCubic, padding: const EdgeInsets.all(6), decoration: BoxDecoration( color: scheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(_open ? 20 : 26), ), child: AnimatedSize( duration: const Duration(milliseconds: 200), curve: Curves.easeOutCubic, alignment: Alignment.bottomCenter, child: _open ? Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsets.fromLTRB(6, 0, 6, 2), child: _field(), ), _secondRow(), ], ) : Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ widget.leading, const SizedBox(width: 4), Expanded(child: _field()), const SizedBox(width: 4), if (accessory != null) ...[ // The keys are 36 high and the row aligns to its bottom: // lift the smaller accessory by half the difference so it // sits on the keys' centre line, not below it. Padding( padding: const EdgeInsets.only(bottom: 10), child: accessory, ), const SizedBox(width: 2), ], widget.trailing, ], ), ), ); } Widget _secondRow() { final accessory = widget.accessory; // The row fades and rises a little as the composer opens. return TweenAnimationBuilder( tween: Tween(begin: 0, end: 1), duration: const Duration(milliseconds: 220), curve: Curves.easeOutCubic, builder: (context, t, child) => Opacity( opacity: t, child: Transform.translate( offset: Offset(0, (1 - t) * 4), child: child, ), ), child: Padding( padding: const EdgeInsets.fromLTRB(0, 2, 0, 0), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ widget.leading, const SizedBox(width: 6), if (widget.model != null) Flexible( fit: FlexFit.loose, child: Align( alignment: Alignment.centerLeft, child: widget.model!, ), ), const SizedBox(width: 6), if (widget.mode != null) widget.mode!, const Spacer(), if (accessory != null) ...[ accessory, const SizedBox(width: 4), ], widget.trailing, ], ), ), ); } }