how to show flutter search textfield without click
3,646
Solution 1
I think you can do something like this,
class _SearchState extends State<Search> {
@override
Widget build(BuildContext context) {
void openSearch() async {
await showSearch(context: context, delegate: DataSearch(), query: '');
}
openSearch();
return Scaffold(
appBar: AppBar(
title: Text('Search App'),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () { openSearch(); },
)
],
),
);
}
}
Hope that solves your issue.
Solution 2
Though I did not find any textfield on your code, but assume your demands. Set a autofocus on your text field like below.
TextField(
autofocus: true,
)
Read more here.
Author by
kh h
Updated on December 24, 2022Comments
-
kh h over 1 year
I'd like to open search page with search textfield focused directly. I don't want to go through another step of clicking search iconbutton to type in the search word. What code do I have to write? Here is my current code. I don't want this to show up ;https://i.stack.imgur.com/bgiRE.png but this to show up when this page opens ;https://i.stack.imgur.com/VzFuz.png
class Search extends StatefulWidget { @override _SearchState createState() => _SearchState(); } class _SearchState extends State<Search> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Search App'), actions: [ Builder( builder: (context) => IconButton( icon: Icon(Icons.search), onPressed: () async { await showSearch(context: context, delegate: DataSearch()); }), ) ], ), ); } } class DataSearch extends SearchDelegate<String> { List allfields = ['a', 'b', ... ]; @override List<Widget> buildActions(BuildContext context) { ... } @override Widget buildLeading(BuildContext context) { ... } @override Widget buildResults(BuildContext context) { ... } @override Widget buildSuggestions(BuildContext context) { ... }
PS: I solved this problem by not making another page for search. I made a page with search bar on it, and now when I click it, I go directly to the searching state.
-
kh h over 3 yearsI tried your code, but the first dummyfunction part has an error saying 'The instance member 'dummyfunction' can't be accessed in an initializer'
-
kh h over 3 yearsMy code doesn't have TextField because I use flutter search delegate method.
-
Shri Hari L over 3 yearsI just updated the answer. Maybe this workaround works for you!
-
kh h over 3 yearsIt opens as I wanted, but this time search itself doesn't work.. when I click a suggestion, there's no action.
-
kh h over 3 yearsAlthough I didn't use this method, I appreciate your help, and it was kind and useful.