fix: Make filter input field auto-lose focus when entering room view

This commit is contained in:
Sorunome 2020-12-19 11:19:49 +01:00
parent 68a5efbcae
commit bdb695e59e
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
class DefaultAppBarSearchField extends StatelessWidget { class DefaultAppBarSearchField extends StatefulWidget {
final TextEditingController searchController; final TextEditingController searchController;
final void Function(String) onChanged; final void Function(String) onChanged;
final Widget suffix; final Widget suffix;
@ -13,9 +13,45 @@ class DefaultAppBarSearchField extends StatelessWidget {
this.suffix, this.suffix,
}) : super(key: key); }) : super(key: key);
@override
_DefaultAppBarSearchFieldState createState() =>
_DefaultAppBarSearchFieldState();
}
class _DefaultAppBarSearchFieldState extends State<DefaultAppBarSearchField> {
final FocusNode _focusNode = FocusNode();
TextEditingController _searchController;
bool _lastTextWasEmpty = false;
void _updateSearchController() {
final thisTextIsEmpty = _searchController.text?.isEmpty ?? false;
if (_lastTextWasEmpty != thisTextIsEmpty) {
setState(() => _lastTextWasEmpty = thisTextIsEmpty);
}
}
@override
void initState() {
super.initState();
_searchController = widget.searchController ?? TextEditingController();
// we need to remove the listener in the dispose method, so we need a reference to the callback
_searchController.addListener(_updateSearchController);
_focusNode.addListener(() => setState(() => null));
}
@override
void dispose() {
_focusNode.dispose();
_searchController.removeListener(_updateSearchController);
if (widget.searchController == null) {
// we need to dispose our own created searchController
_searchController.dispose();
}
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final focusNode = FocusNode();
return Container( return Container(
height: 40, height: 40,
padding: EdgeInsets.only(right: 16), padding: EdgeInsets.only(right: 16),
@ -24,9 +60,9 @@ class DefaultAppBarSearchField extends StatelessWidget {
borderRadius: BorderRadius.circular(32), borderRadius: BorderRadius.circular(32),
child: TextField( child: TextField(
autocorrect: false, autocorrect: false,
controller: searchController, controller: _searchController,
onChanged: onChanged, onChanged: widget.onChanged,
focusNode: focusNode, focusNode: _focusNode,
decoration: InputDecoration( decoration: InputDecoration(
contentPadding: EdgeInsets.only( contentPadding: EdgeInsets.only(
top: 8, top: 8,
@ -37,15 +73,17 @@ class DefaultAppBarSearchField extends StatelessWidget {
borderRadius: BorderRadius.circular(32), borderRadius: BorderRadius.circular(32),
), ),
hintText: L10n.of(context).searchForAChat, hintText: L10n.of(context).searchForAChat,
suffixIcon: focusNode.hasFocus suffixIcon: _focusNode.hasFocus ||
(widget.suffix == null &&
(_searchController.text?.isNotEmpty ?? false))
? IconButton( ? IconButton(
icon: Icon(Icons.backspace_outlined), icon: Icon(Icons.backspace_outlined),
onPressed: () { onPressed: () {
searchController.clear(); _searchController.clear();
focusNode.unfocus(); _focusNode.unfocus();
}, },
) )
: suffix, : widget.suffix,
), ),
), ),
), ),