Is there a way to make the search dialog (showSearch()
) Dismissible
? I'd like to be able to swipe away the search page to close it.
Asked
Active
Viewed 339 times
2 Answers
0
https://stackoverflow.com/a/55050804/12059260
He wrote code for detecting swiping left or right. Write navigation.pop() on which swipe u wanna close search page, with animation u can make it smoother

M O N I S H
- 9
- 6
-
How? `showSearch()` doesn't expose a `Widget` that we can pass as a `child`. – Westy92 Aug 13 '21 at 14:50
0
You could use swipedetector as a parent for the search page and use the function onSwipeLeft or onSwipeRight to close the page calling pop function https://pub.dev/documentation/swipedetector/latest/
There ya go
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
final List<String> list = List.generate(10, (index) => "Text $index");
MyHomePage({
Key? key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Search Bar'),
actions: <Widget>[
IconButton(
onPressed: () {
showSearch(context: context, delegate: Search(widget.list));
},
icon: Icon(Icons.search),
)
],
),
body: ListView.builder(
itemCount: widget.list.length,
itemBuilder: (context, index) => ListTile(
title: Text(
widget.list[index],
),
),
),
);
}
}
class Search extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
query = "";
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
);
}
String selectedResult = "";
@override
Widget buildResults(BuildContext context) {
return Container(
child: Center(
child: Text(selectedResult),
),
);
}
final List<String> listExample;
Search(this.listExample);
List<String> recentList = ["Text 4", "Text 3"];
@override
Widget buildSuggestions(BuildContext context) {
List<String> suggestionList = [];
query.isEmpty
? suggestionList = recentList //In the true case
: suggestionList.addAll(listExample.where(
// In the false case
(element) => element.contains(query),
));
return GestureDetector(
onPanUpdate: (details) {
// Swiping in right direction.
if (details.delta.dx > 0) {
print('right');
Navigator.pop(context);
}
// Swiping in left direction.
if (details.delta.dx < 0) {
print('left');
}
},
child: Container(
child: ListView.builder(
itemCount: suggestionList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
suggestionList[index],
),
leading: query.isEmpty ? Icon(Icons.access_time) : SizedBox(),
onTap: () {
selectedResult = suggestionList[index];
showResults(context);
},
);
},
),
),
);
}
}
With that code you could drag from anywhere in the body
-
How? `showSearch()` doesn't expose a `Widget` that we can pass as a `child`. – Westy92 Aug 13 '21 at 14:49