i create search icon in appbar to open search and when i click on it there is error appear
the error is "Navigator operation requested with a context that does not include a Navigator"
and it’s my code
void main() {
runApp(rekomo2());
}
class rekomo2 extends StatefulWidget {
rekomo2({super.key});
@override
State<rekomo2> createState() => _rekomo2State();
}
class _rekomo2State extends State<rekomo2> {
int curindex = 0;
final screen = [chats(), groups(), nearme(), status(), favourite()];
@override
Widget build(BuildContext context) {
return MaterialApp(
// routes: {"nearme": (context) => nearme()},
debugShowCheckedModeBanner: false,
home: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
backgroundColor: Colors.purple[300],
onPressed: () {
// showSearch(context: context , delegate: search(),);
},
child: const Icon(
Icons.search,
color: Colors.white,
size: 30,
),
),
Container(
height: 10,
),
FloatingActionButton(
backgroundColor: Colors.purple[300],
onPressed: () {},
child: const Icon(
Icons.add,
color: Colors.white,
size: 30,
),
),
],
),
appBar: AppBar(
actions: [
IconButton(
onPressed: () {
showSearch(context: context, delegate: search());
},
icon: Icon(Icons.search, size: 30, color: Colors.purple[300]))
],
leading: Builder(
builder: (context) => IconButton(
onPressed: () {
Scaffold.of(context).openDrawer();
},
icon: const Icon(Icons.menu),
color: Colors.purple[300],
)),
backgroundColor: Colors.white,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Rekomo2",
style: TextStyle(
fontSize: 25, fontWeight: FontWeight.bold, color: Colors.purple[300]),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: curindex,
onTap: (value) {
setState(() {
curindex = value;
});
},
selectedItemColor: Colors.purple[600],
// currentIndex: screen[Key.],
type: BottomNavigationBarType.fixed,
showSelectedLabels: true,
showUnselectedLabels: false,
items: const [
BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chats"),
BottomNavigationBarItem(
icon: Icon(Icons.group),
label: "Groups",
),
BottomNavigationBarItem(
icon: Icon(Icons.location_on_outlined), label: "People Near Me"),
BottomNavigationBarItem(
icon: Icon(Icons.switch_access_shortcut_sharp), label: "Status"),
BottomNavigationBarItem(icon: Icon(Icons.star), label: "Favourite"),
]),
// backgroundColor: Colors.red,
drawer: const Drawer(),
body: screen[curindex],
)));
}
}
class search extends SearchDelegate {
List searchr = ["devil", "karma", "reem", "ahmed", "moaa"];
@override
List<Widget>? buildActions(BuildContext context) {
return [
IconButton(
onPressed: () {
query = "";
},
icon: Icon(Icons.close))
];
}
@override
Widget? buildLeading(BuildContext context) {
return IconButton(
onPressed: () {
query = "";
},
icon: Icon(Icons.search));
}
@override
Widget buildResults(BuildContext context) {
return Text("data");
}
@override
Widget buildSuggestions(BuildContext context) {
if (query.isEmpty) {
return Text("");
} else {
List filterlist = searchr.where((element) => element.contains(query)).toList();
return ListView.builder(
itemCount: filterlist.length,
itemBuilder: (context, i) {
return InkWell(onTap: () => context, child: Card(child: Text(filterlist[i])));
},
);
}
}
}
and i don’t know what cause the error and how to fix it
and i try to use search delegate with floating button but the same error appear
2
Answers
the error disappear after add builder and hero code :
I have reviewed your code and found that you were trying to use the
showSearch
function with a context that does not have aNavigator
in its widget tree.In your code, you are using a nested
MaterialApp
, which can lead to issues. There should only be oneMaterialApp
at the root of your widget tree. Here’s how you can fix your code:MaterialApp
.showSearch
with the correct context.I have modified your code with some additions, feel free to makes changes and see if it works.
Code: