How to keep hamburger icon without visible appBar flutter
Asked Answered
W

4

11

I recently tried keeping a Hamburger icon for my menu slider without an AppBar or at least completely invisible. The first attempt was with a SafeArea but that emptied Scaffold. Then I tried setting the Opacity to 0.0 like shown on the code below. But it gives out the same result as SafeArea with nothing on Scaffold. Please can anyone help?

    import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: ThemeData(
        // Define the default Brightness and Colors
        brightness: Brightness.dark,
        primaryColor: Colors.lightBlue[800],
        accentColor: Colors.cyan[600],
      ),
      home: Scaffold(
          Opacity(
            opacity: 0.0,
            appBar: AppBar(),
          ),
          drawer: new Drawer(
            child: new ListView(),
          ),
          body: new Center(
              child: new Column(
            children: <Widget>[],
          ))),
    );
  }
}
Was answered 19/1, 2019 at 19:40 Comment(0)
C
29

If I have understood you well, you want to display a menu button to show the Drawer without displaying any AppBar.

One option is to use a Stack for the body of the Scaffold.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  var scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: ThemeData(
        // Define the default Brightness and Colors
        brightness: Brightness.dark,
        primaryColor: Colors.lightBlue[800],
        accentColor: Colors.cyan[600],
      ),
      home: Scaffold(
        key: scaffoldKey,
        drawer: new Drawer(
          child: new ListView(),
        ),
        body: Stack(
          children: <Widget>[
            new Center(
                child: new Column(
              children: <Widget>[],
            )),
            Positioned(
              left: 10,
              top: 20,
              child: IconButton(
                icon: Icon(Icons.menu),
                onPressed: () => scaffoldKey.currentState.openDrawer(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Chalcedony answered 19/1, 2019 at 22:17 Comment(2)
Note for Newbie like me: I used the above approach on a stateless widget, stuffs broke when coming back from a newly navigated page. "the getter 'usergesture progress' was called on null" was the error. things worked fine when the widget was changed to a stateful widget.Stereochrome
I am getting NoSuchMethodError: invalid member on null: 'openDrawer'Fullgrown
M
7

If I have understood your question well.

You have your own custom Menu button to open/close drawer. You don't want to use AppBar as well.

In that case you can use GlobalKey<ScaffoldState>() object to open Drawer.

import 'package:flutter/material.dart';

GlobalKey<ScaffoldState> _scaffoldState = GlobalKey<ScaffoldState>();

return Scaffold(
  key: _scaffoldState,
  drawer: DrawerView(),
  body: ThemeScreen(
    header: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        IconButton(
          icon: Icon(Icons.menu,
              color: Colors.white,
              size: 15),
          onPressed: (){
            _scaffoldState.currentState.openDrawer();
          },
        ),
      ],
    ),
  ),
);
Mattson answered 28/8, 2020 at 9:4 Comment(0)
C
6

You can make AppBar completely invisible by setting the same color and elevation = 0 screenshot here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF198BAA),
      appBar: AppBar(
        backgroundColor: Color(0xFF198BAA),
        elevation: 0.0,
      ),
      drawer: Drawer(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(18.0),
            child: ListView(
              children: <Widget>[
                ListTile(
                  title: Text('Item1'),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
Canine answered 15/10, 2019 at 10:16 Comment(1)
The Appbar will come in between if there is a scrollable content in the Scaffold's body.Rizika
R
2

This is similar to Ox.S's answer, but you can make the AppBar transparent and then change the icon to whatever color you want.

Scaffold(
  appBar: AppBar(
    backgroundColor: Colors.transparent,
    elevation: 0.0,
    iconTheme: IconThemeData(color: Colors.black),
  ),
  drawer: Drawer(...
Rombert answered 19/5, 2021 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.