Can somebody please tell me how can I integrate the menu drawer inside the Row widget instead of in a Scaffold widget? Something like Gmail's app (search with drawer icon).
-
what makes you prevent from doing so?Darish– Darish2020-02-27 09:07:36 +00:00Commented Feb 27, 2020 at 9:07
-
I don't know how to do this when I the drawer overflows and no button is shown :( Can you please provide me a demo code.Yogesh Aggarwal– Yogesh Aggarwal2020-02-27 09:10:14 +00:00Commented Feb 27, 2020 at 9:10
-
in the gmail app, they are just showing an drawer icon in the search bar, and on click action they open the drawerDarish– Darish2020-02-27 09:11:53 +00:00Commented Feb 27, 2020 at 9:11
-
But I am unable to add it. Please provide me a basic code.Yogesh Aggarwal– Yogesh Aggarwal2020-02-27 09:15:48 +00:00Commented Feb 27, 2020 at 9:15
-
check this, might help you :- stackoverflow.com/questions/57748170/…Yogesh Chawla– Yogesh Chawla2020-02-27 09:30:28 +00:00Commented Feb 27, 2020 at 9:30
Add a comment
|
2 Answers
It's very simple.
Screenshot of the output
Steps:
Step 1:
Define the scaffold with a custom Appbar widget
return Scaffold(
appBar: FloatAppBar(),
body: Center(
child: Text('Body'),
),
drawer: Drawer(
child: SafeArea(
right: false,
child: Center(
child: Text('Drawer content'),
),
),
),
);
Step 2:
Implement the PreferredSizeWidget to create a custom AppBar
class FloatAppBar extends StatelessWidget with PreferredSizeWidget {
step 3:
Use Scaffold.of(context).openDrawer(); to open the drawer when required.
Here is the complete snippet.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Playground',
home: TestPage(),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: FloatAppBar(),
body: Center(
child: Text('Body'),
),
drawer: Drawer(
child: SafeArea(
right: false,
child: Center(
child: Text('Drawer content'),
),
),
),
);
}
}
class FloatAppBar extends StatelessWidget with PreferredSizeWidget {
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Positioned(
top: 10,
right: 15,
left: 15,
child: Container(
color: Colors.white,
child: Row(
children: <Widget>[
Material(
type: MaterialType.transparency,
child: IconButton(
splashColor: Colors.grey,
icon: Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openDrawer();
},
),
),
Expanded(
child: TextField(
cursorColor: Colors.black,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.go,
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 15),
hintText: "Search..."),
),
),
],
),
),
),
],
);
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
See the live demo here.
1 Comment
Anoop Thiruonam
Shows the error
The class 'PreferredSize' can't be used as a mixin because it extends a class other than Object.