import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'chat_find_controller.dart'; /// The chat's find bar, like the terminal's: the query field, the match /// count, Match case, the older/newer and close buttons. Matches count from /// the newest; Enter moves to an older match and Shift+Enter to a newer one; /// Esc closes. [onStep] moves and reveals; [onClose] closes the bar and /// returns the keyboard to the composer. class ChatFindBar extends StatefulWidget { const ChatFindBar({ super.key, required this.find, required this.focus, required this.onStep, required this.onClose, }); final ChatFindController find; final FocusNode focus; final ValueChanged onStep; final VoidCallback onClose; @override State createState() => _ChatFindBarState(); } class _ChatFindBarState extends State { late final TextEditingController _text = TextEditingController( text: widget.find.query, ); @override void dispose() { _text.dispose(); super.dispose(); } KeyEventResult _key(FocusNode node, KeyEvent event) { if (event is KeyUpEvent) return KeyEventResult.ignored; if (event.logicalKey == LogicalKeyboardKey.escape) { widget.onClose(); return KeyEventResult.handled; } if (event.logicalKey == LogicalKeyboardKey.enter || event.logicalKey == LogicalKeyboardKey.numpadEnter) { widget.onStep(HardwareKeyboard.instance.isShiftPressed ? -1 : 1); return KeyEventResult.handled; } return KeyEventResult.ignored; } String _count(ChatFindController find) { if (find.query.isEmpty) return ''; if (find.count == 0) return find.capped ? 'Searching…' : 'No results'; final total = find.capped ? '${find.count}+' : '${find.count}'; return '${find.index + 1} of $total'; } @override Widget build(BuildContext context) { final find = widget.find; final scheme = Theme.of(context).colorScheme; // The buttons count as part of the field, so clicking one keeps the // keyboard in it (Enter steps on from there). return TextFieldTapRegion( child: Material( elevation: 3, color: scheme.surfaceContainerHigh, borderRadius: BorderRadius.circular(8), child: Padding( padding: const EdgeInsetsDirectional.only(start: 12, end: 4), child: ListenableBuilder( listenable: find, builder: (context, _) => Row( mainAxisSize: MainAxisSize.min, children: [ Flexible( child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 240), child: Focus( onKeyEvent: _key, skipTraversal: true, child: TextField( key: const ValueKey('chat-find-field'), controller: _text, focusNode: widget.focus, autofocus: true, decoration: const InputDecoration( hintText: 'Find in chat', border: InputBorder.none, isDense: true, ), onChanged: (value) => find.query = value, ), ), ), ), Semantics( liveRegion: true, child: Text( _count(find), key: const ValueKey('chat-find-count'), style: Theme.of(context).textTheme.bodySmall, ), ), IconButton( key: const ValueKey('chat-find-case'), tooltip: 'Match case', isSelected: find.caseSensitive, onPressed: find.toggleCase, icon: const Text('Aa'), ), IconButton( key: const ValueKey('chat-find-older'), tooltip: 'Older match (Enter)', onPressed: find.count == 0 ? null : () => widget.onStep(1), icon: const Icon(Icons.keyboard_arrow_up), ), IconButton( key: const ValueKey('chat-find-newer'), tooltip: 'Newer match (Shift+Enter)', onPressed: find.count == 0 ? null : () => widget.onStep(-1), icon: const Icon(Icons.keyboard_arrow_down), ), IconButton( key: const ValueKey('chat-find-close'), tooltip: 'Close find (Esc)', onPressed: widget.onClose, icon: const Icon(Icons.close), ), ], ), ), ), ), ); } }