You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.5 KiB
Dart
119 lines
3.5 KiB
Dart
4 years ago
|
import 'package:flutter/material.dart';
|
||
4 years ago
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||
4 years ago
|
|
||
4 years ago
|
import '../config/app_config.dart';
|
||
4 years ago
|
|
||
4 years ago
|
class DefaultAppBarSearchField extends StatefulWidget {
|
||
4 years ago
|
final TextEditingController searchController;
|
||
|
final void Function(String) onChanged;
|
||
4 years ago
|
final void Function(String) onSubmit;
|
||
4 years ago
|
final Widget suffix;
|
||
4 years ago
|
final bool autofocus;
|
||
|
final String prefixText;
|
||
|
final String hintText;
|
||
4 years ago
|
final EdgeInsets padding;
|
||
4 years ago
|
final bool readOnly;
|
||
4 years ago
|
final Widget prefixIcon;
|
||
4 years ago
|
final bool unfocusOnClear;
|
||
4 years ago
|
|
||
4 years ago
|
DefaultAppBarSearchField({
|
||
4 years ago
|
Key key,
|
||
|
this.searchController,
|
||
|
this.onChanged,
|
||
4 years ago
|
this.onSubmit,
|
||
4 years ago
|
this.suffix,
|
||
4 years ago
|
this.autofocus = false,
|
||
|
this.prefixText,
|
||
|
this.hintText,
|
||
4 years ago
|
this.padding,
|
||
4 years ago
|
this.readOnly = false,
|
||
4 years ago
|
this.prefixIcon,
|
||
4 years ago
|
this.unfocusOnClear = true,
|
||
4 years ago
|
}) : super(key: key);
|
||
|
|
||
4 years ago
|
@override
|
||
4 years ago
|
DefaultAppBarSearchFieldState createState() =>
|
||
|
DefaultAppBarSearchFieldState();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
class DefaultAppBarSearchFieldState extends State<DefaultAppBarSearchField> {
|
||
4 years ago
|
TextEditingController _searchController;
|
||
|
bool _lastTextWasEmpty = false;
|
||
4 years ago
|
final FocusNode _focusNode = FocusNode();
|
||
|
|
||
|
void requestFocus() => _focusNode.requestFocus();
|
||
4 years ago
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Container(
|
||
|
height: 40,
|
||
4 years ago
|
padding: widget.padding ?? EdgeInsets.only(right: 12),
|
||
4 years ago
|
child: TextField(
|
||
|
autofocus: widget.autofocus,
|
||
|
autocorrect: false,
|
||
|
controller: _searchController,
|
||
|
onChanged: widget.onChanged,
|
||
|
focusNode: _focusNode,
|
||
|
readOnly: widget.readOnly,
|
||
4 years ago
|
onSubmitted: widget.onSubmit,
|
||
4 years ago
|
decoration: InputDecoration(
|
||
|
prefixText: widget.prefixText,
|
||
4 years ago
|
enabledBorder: OutlineInputBorder(
|
||
4 years ago
|
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
|
||
4 years ago
|
borderSide:
|
||
|
BorderSide(color: Theme.of(context).secondaryHeaderColor),
|
||
|
),
|
||
4 years ago
|
contentPadding: EdgeInsets.only(
|
||
|
top: 8,
|
||
|
bottom: 8,
|
||
|
left: 16,
|
||
4 years ago
|
),
|
||
4 years ago
|
hintText: widget.hintText,
|
||
4 years ago
|
prefixIcon: widget.prefixIcon,
|
||
4 years ago
|
suffixIcon: !widget.readOnly &&
|
||
|
(_focusNode.hasFocus ||
|
||
|
(widget.suffix == null &&
|
||
|
(_searchController.text?.isNotEmpty ?? false)))
|
||
|
? IconButton(
|
||
4 years ago
|
tooltip: L10n.of(context).clearText,
|
||
4 years ago
|
icon: Icon(Icons.backspace_outlined),
|
||
|
onPressed: () {
|
||
|
_searchController.clear();
|
||
|
widget.onChanged?.call('');
|
||
4 years ago
|
if (widget.unfocusOnClear) _focusNode.unfocus();
|
||
4 years ago
|
},
|
||
|
)
|
||
|
: widget.suffix,
|
||
4 years ago
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|