fluffychat/lib/components/default_app_bar_search_field.dart

107 lines
3.2 KiB
Dart
Raw Normal View History

2020-12-06 12:51:40 +01:00
import 'package:flutter/material.dart';
class DefaultAppBarSearchField extends StatefulWidget {
2020-12-06 12:51:40 +01:00
final TextEditingController searchController;
final void Function(String) onChanged;
final Widget suffix;
2020-12-20 17:00:09 +01:00
final bool autofocus;
final String prefixText;
final String hintText;
2021-01-22 21:39:37 +01:00
final EdgeInsets padding;
final bool readOnly;
2020-12-06 12:51:40 +01:00
const DefaultAppBarSearchField({
Key key,
this.searchController,
this.onChanged,
this.suffix,
2020-12-20 17:00:09 +01:00
this.autofocus = false,
this.prefixText,
this.hintText,
2021-01-22 21:39:37 +01:00
this.padding,
this.readOnly = false,
2020-12-06 12:51:40 +01:00
}) : 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();
}
2020-12-06 12:51:40 +01:00
@override
Widget build(BuildContext context) {
return Container(
height: 40,
2021-01-22 21:39:37 +01:00
padding: widget.padding ?? EdgeInsets.only(right: 12),
2020-12-06 12:51:40 +01:00
child: Material(
color: Theme.of(context).secondaryHeaderColor,
borderRadius: BorderRadius.circular(32),
child: TextField(
2020-12-20 17:00:09 +01:00
autofocus: widget.autofocus,
2020-12-06 12:51:40 +01:00
autocorrect: false,
controller: _searchController,
onChanged: widget.onChanged,
focusNode: _focusNode,
readOnly: widget.readOnly,
2020-12-06 12:51:40 +01:00
decoration: InputDecoration(
2020-12-20 17:00:09 +01:00
prefixText: widget.prefixText,
2020-12-06 12:51:40 +01:00
contentPadding: EdgeInsets.only(
top: 8,
bottom: 8,
left: 16,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32),
),
2020-12-20 17:00:09 +01:00
hintText: widget.hintText,
suffixIcon: !widget.readOnly &&
(_focusNode.hasFocus ||
(widget.suffix == null &&
(_searchController.text?.isNotEmpty ?? false)))
2020-12-06 12:51:40 +01:00
? IconButton(
icon: Icon(Icons.backspace_outlined),
onPressed: () {
_searchController.clear();
2020-12-20 16:53:37 +01:00
widget.onChanged?.call('');
_focusNode.unfocus();
2020-12-06 12:51:40 +01:00
},
)
: widget.suffix,
2020-12-06 12:51:40 +01:00
),
),
),
);
}
}