4

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).

5
  • what makes you prevent from doing so? Commented 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. Commented 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 drawer Commented Feb 27, 2020 at 9:11
  • But I am unable to add it. Please provide me a basic code. Commented Feb 27, 2020 at 9:15
  • check this, might help you :- stackoverflow.com/questions/57748170/… Commented Feb 27, 2020 at 9:30

2 Answers 2

6

It's very simple.

Screenshot of the output

enter image description here

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Shows the error The class 'PreferredSize' can't be used as a mixin because it extends a class other than Object.
-2

the AppBar widget alredy has mechanisms for that,


AppBar(
 drawaer: YourDrawer(),
 actions: [
   IconButton(
    icon: Icon(Icons.search),
    onPressed: (){}
   ) 
 ]
);

it will create the Gmail appbar you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.