Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a side menu that swipes from the left?

Tags:

flutter

dart

menu

I am new to Flutter and just finished the get started tutorial. I would like to create a side menu, which will appear from the left side when you swipe (like Gmail on Android).

Unfortunately, I can't find such a layout on the documentation and the example from the flutter gallery is a bit messy.

Could someone please explain me how to implement such a Widget?

like image 504
Alexi Coard Avatar asked May 01 '17 14:05

Alexi Coard


2 Answers

Here is an example for a simple Drawer (I just adapted the default project setup from flutter create):

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

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

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('TestProject'),
      ),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget> [
            new DrawerHeader(child: new Text('Header'),),
            new ListTile(
              title: new Text('First Menu Item'),
              onTap: () {},
            ),
            new ListTile(
              title: new Text('Second Menu Item'),
              onTap: () {},
            ),
            new Divider(),
            new ListTile(
              title: new Text('About'),
              onTap: () {},
            ),
          ],
        )
      ),
      body: new Center(
        child: new Text(
          'Center',
        ),
      ),
    );
  }
}

The docs are a good place to start ;)

Btw: including a drawer in your scaffold also takes care of the menu button and the left swipe gesture for you.

like image 110
Rainer Wittmann Avatar answered Nov 13 '22 16:11

Rainer Wittmann


The Stocks example has a somewhat less complicated use of Drawer.

like image 21
Collin Jackson Avatar answered Nov 13 '22 17:11

Collin Jackson