fluffychat/lib/widgets/default_app_bar_search_field.dart

129 lines
3.9 KiB
Dart
Raw Normal View History

2022-01-28 18:21:20 +01:00
//@dart=2.12
2020-12-06 12:51:40 +01:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
2021-02-13 13:27:44 +01:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-12-06 12:51:40 +01:00
2021-05-22 08:53:52 +02:00
import '../config/app_config.dart';
2021-02-07 09:12:34 +01:00
class DefaultAppBarSearchField extends StatefulWidget {
2022-01-28 18:21:20 +01:00
final TextEditingController? searchController;
final void Function(String)? onChanged;
final void Function(String)? onSubmit;
final Widget? suffix;
2020-12-20 17:00:09 +01:00
final bool autofocus;
2022-01-28 18:21:20 +01:00
final String? prefixText;
final String? hintText;
final String? labelText;
final EdgeInsets? padding;
final bool readOnly;
2022-01-28 18:21:20 +01:00
final Widget? prefixIcon;
2021-05-01 07:40:12 +02:00
final bool unfocusOnClear;
final bool autocorrect;
2020-12-06 12:51:40 +01:00
2021-10-14 18:09:30 +02:00
const DefaultAppBarSearchField({
2022-01-28 18:21:20 +01:00
Key? key,
2020-12-06 12:51:40 +01:00
this.searchController,
this.onChanged,
2021-02-06 20:35:52 +01:00
this.onSubmit,
2020-12-06 12:51:40 +01:00
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.labelText,
this.readOnly = false,
2021-02-02 09:00:06 +01:00
this.prefixIcon,
2021-05-01 07:40:12 +02:00
this.unfocusOnClear = true,
this.autocorrect = true,
2020-12-06 12:51:40 +01:00
}) : super(key: key);
@override
2021-02-05 16:30:52 +01:00
DefaultAppBarSearchFieldState createState() =>
DefaultAppBarSearchFieldState();
}
2021-02-05 16:30:52 +01:00
class DefaultAppBarSearchFieldState extends State<DefaultAppBarSearchField> {
2022-01-28 18:21:20 +01:00
late final TextEditingController _searchController;
bool _lastTextWasEmpty = false;
2021-02-05 16:30:52 +01:00
final FocusNode _focusNode = FocusNode();
void requestFocus() => _focusNode.requestFocus();
void _updateSearchController() {
2022-01-28 18:21:20 +01:00
final thisTextIsEmpty = _searchController.text.isEmpty;
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);
2022-01-28 18:21:20 +01:00
_focusNode.addListener(() => setState(() {}));
}
@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-10-14 18:09:30 +02:00
padding: widget.padding ?? const EdgeInsets.only(right: 12),
2021-01-24 17:26:59 +01:00
child: TextField(
autofocus: widget.autofocus,
autocorrect: widget.autocorrect,
enableSuggestions: widget.autocorrect,
keyboardType: widget.autocorrect ? null : TextInputType.visiblePassword,
2021-01-24 17:26:59 +01:00
controller: _searchController,
onChanged: widget.onChanged,
focusNode: _focusNode,
readOnly: widget.readOnly,
2021-02-06 20:35:52 +01:00
onSubmitted: widget.onSubmit,
2021-01-24 17:26:59 +01:00
decoration: InputDecoration(
prefixText: widget.prefixText,
labelText: widget.labelText,
2021-02-02 09:00:06 +01:00
enabledBorder: OutlineInputBorder(
2021-02-07 09:12:34 +01:00
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
2021-02-02 09:00:06 +01:00
borderSide:
BorderSide(color: Theme.of(context).secondaryHeaderColor),
),
2021-10-14 18:09:30 +02:00
contentPadding: const EdgeInsets.only(
2021-01-24 17:26:59 +01:00
top: 8,
bottom: 8,
left: 16,
2020-12-06 12:51:40 +01:00
),
2021-01-24 17:26:59 +01:00
hintText: widget.hintText,
2021-02-02 09:00:06 +01:00
prefixIcon: widget.prefixIcon,
2021-01-24 17:26:59 +01:00
suffixIcon: !widget.readOnly &&
(_focusNode.hasFocus ||
(widget.suffix == null &&
2022-01-28 18:21:20 +01:00
(_searchController.text.isNotEmpty)))
2021-01-24 17:26:59 +01:00
? IconButton(
2022-01-28 18:21:20 +01:00
tooltip: L10n.of(context)!.clearText,
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.backspace_outlined),
2021-01-24 17:26:59 +01:00
onPressed: () {
_searchController.clear();
widget.onChanged?.call('');
2021-05-01 07:40:12 +02:00
if (widget.unfocusOnClear) _focusNode.unfocus();
2021-01-24 17:26:59 +01:00
},
)
: widget.suffix,
2020-12-06 12:51:40 +01:00
),
),
);
}
}