Skip to content

Commit

Permalink
feat: Edit previous search feature (openfoodfacts#2159)
Browse files Browse the repository at this point in the history
* Edit previous search feature

* Fix warning
  • Loading branch information
g123k authored Jun 5, 2022
1 parent 2c24f9b commit e871c82
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
24 changes: 22 additions & 2 deletions packages/smooth_app/lib/pages/scan/search_history_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,30 @@ class _SearchHistoryViewState extends State<SearchHistoryView> {
_handleDismissed(context, query),
background: Container(color: RED_COLOR),
child: ListTile(
leading: const SizedBox(
height: double.infinity, // Vertically center the icon.
leading: const Padding(
padding: EdgeInsets.only(top: 4.0),
child: Icon(Icons.search, size: 18.0),
),
trailing: InkWell(
customBorder: const CircleBorder(),
onTap: () {
final TextEditingController controller =
Provider.of<TextEditingController>(
context,
listen: false,
);

controller.text = query;
controller.selection =
TextSelection.fromPosition(TextPosition(offset: query.length));

Focus.maybeOf(context)?.requestFocus();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Icons.edit, size: 18.0),
),
),
minLeadingWidth: 10,
title: Text(query, style: const TextStyle(fontSize: 20.0)),
onTap: () => widget.onTap?.call(query),
Expand Down
74 changes: 55 additions & 19 deletions packages/smooth_app/lib/pages/scan/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,42 @@ Future<void> _onSubmittedText(
context: context,
);

class SearchPage extends StatelessWidget {
class SearchPage extends StatefulWidget {
@override
State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchTextController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(toolbarHeight: 0.0),
body: Column(
children: <Widget>[
const Padding(
padding: EdgeInsets.all(10.0),
child: SearchField(autofocus: true),
),
Expanded(
child: SearchHistoryView(
onTap: (String query) => _performSearch(context, query),
body: ChangeNotifierProvider<TextEditingController>(
create: (_) => _searchTextController,
child: Column(
children: <Widget>[
const Padding(
padding: EdgeInsets.all(10.0),
child: SearchField(autofocus: true),
),
Expanded(
child: SearchHistoryView(
onTap: (String query) => _performSearch(context, query),
),
),
),
],
],
),
),
);
}

@override
void dispose() {
_searchTextController.dispose();
super.dispose();
}
}

class SearchField extends StatefulWidget {
Expand All @@ -127,35 +143,55 @@ class SearchField extends StatefulWidget {
}

class _SearchFieldState extends State<SearchField> {
final TextEditingController _textController = TextEditingController();
final FocusNode _focusNode = FocusNode();
late TextEditingController _controller;

bool _isEmpty = true;

static const Duration _animationDuration = Duration(milliseconds: 100);

@override
void initState() {
super.initState();
_textController.addListener(_handleTextChange);
_focusNode.addListener(_handleFocusChange);
if (widget.autofocus) {
_focusNode.requestFocus();
}
}

@override
void didChangeDependencies() {
super.didChangeDependencies();

try {
_controller = Provider.of<TextEditingController>(context);
} catch (err) {
_controller = TextEditingController();
}

_controller.removeListener(_handleTextChange);
_controller.addListener(_handleTextChange);
}

@override
void dispose() {
_textController.dispose();
_focusNode.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final AppLocalizations localizations = AppLocalizations.of(context);

try {
_controller = Provider.of<TextEditingController>(context);
} catch (err) {
_controller = TextEditingController();
}

return TextField(
textInputAction: TextInputAction.search,
controller: _textController,
controller: _controller,
focusNode: _focusNode,
onSubmitted: (String query) => _performSearch(context, query),
decoration: InputDecoration(
Expand Down Expand Up @@ -197,9 +233,9 @@ class _SearchFieldState extends State<SearchField> {
void _handleTextChange() {
//Only rebuild the widget if the text length is 0 or 1 as we only check if
//the text length is empty or not
if (_textController.text.isEmpty || _textController.text.length == 1) {
if (_controller.text.isEmpty || _controller.text.length == 1) {
setState(() {
_isEmpty = _textController.text.isEmpty;
_isEmpty = _controller.text.isEmpty;
});
}
}
Expand All @@ -215,7 +251,7 @@ class _SearchFieldState extends State<SearchField> {
if (_isEmpty) {
Navigator.pop(context);
} else {
_textController.clear();
_controller.clear();
}
}
}

0 comments on commit e871c82

Please sign in to comment.