// 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; 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.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; @override void initState() { super.initState(); widget.focusNode.addListener(_onFocus); _open = widget.focusNode.hasFocus; } @override void dispose() { widget.focusNode.removeListener(_onFocus); super.dispose(); } void _onFocus() { final open = widget.focusNode.hasFocus; 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) ...[ 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, ], ), ), ); } }