颤振侧抽屉作为小部件

时间:2018-08-27 09:14:47

标签: dart flutter navigation-drawer

我正在尝试在Flutter应用中将侧抽屉实现为小部件

home.dart

import '../widgets/navigation_drawer_widget.dart'; //imported the nav drawer class from widgets directory

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(length: 2,
        child: Scaffold(
          drawer: DrawerWidget(),
          appBar: AppBar(
            title: Text('Home'),
            bottom: TabBar(tabs: <Widget>[
              Tab(
                icon: Icon(Icons.access_time),
                text: 'Tab 1',
              ),
              Tab(
                icon: Icon(Icons.calendar_today),
                text: 'Tab 2',
              )
            ]),
          ),
          body: TabBarView(children: <Widget>[
            Tab1(),
            Tab2()
          ]),
        )
    );
  }
}

我的抽屉小部件** navigation_drawer_widget.dart **文件

import 'package:flutter/material.dart';

class DrawerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        AppBar(
          automaticallyImplyLeading: false,
          title: Text('Menu'),
        ),
        ListTile(
          leading: Icon(Icons.home),
          title: Text('Home'),
          onTap: () {
            Navigator.pushReplacementNamed(context, '/home');
          },
        ),
        ListTile(
          leading: Icon(Icons.person),
          title: Text('My Profile'),
          onTap: () {
            Navigator.pushReplacementNamed(context, '/profile');
          },
        ),
        ListTile(
          leading: Icon(Icons.school),
          title: Text('My Education'),
          onTap: () {
            Navigator.pushReplacementNamed(context, '/education');
          },
        ),
      ],
    );
  }
}

但是,当我单击导航汉堡包图标时,它会向我显示类似的内容

enter image description here

如您所见,导航抽屉变得透明,并延伸到页面的整个宽度。如果我将抽屉代码移至主页(而不是执行DrawerWidget()),它将显示常规的良好导航抽屉。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

如果要使用默认的材质设计抽屉,则需要用ColumnDrawerWidget包裹在Drawer中。

来自文档:

  

抽屉可以是任何小部件,但是通常最好使用材料库中的Drawer小部件,该材料符合《材料设计》规范。

https://flutter.io/cookbook/design/drawer/

答案 1 :(得分:1)

您可以将Drawer声明为Widget而不是Class,然后导入文件内部。

Widget myDrawer = Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      DrawerHeader(...),
      ListTile(...)
    ],
  ),
);

然后:

import 'mydrawer.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(...),
      body: Container(...),
      drawer: myDrawer
    );
  }
}

答案 2 :(得分:0)

使用容器更改透明抽屉

示例:

 class DrawerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext contx) {
    return Container(
      color: Colors.amber[600],
      child: Column(
        children: <Widget>[
          AppBar(automaticallyImplyLeading: false, title: Text('menu')),
          ListTile(
            leading: Icon(Icons.home),
            title: Text('Home'),
            onTap: () {
              Navigator.pushReplacementNamed(contx, '/home');
            },
          ),
          ListTile(
            leading: Icon(Icons.person),
            title: Text('My Profile'),
            onTap: () {
              Navigator.pushReplacementNamed(contx, '/profile');
            },
          ),
          ListTile(
            leading: Icon(Icons.school),
            title: Text('My Education'),
            onTap: () {
              Navigator.pushReplacementNamed(contx, '/education');
            },
          ),
        ],
      ),
    );
  }
}