【问题标题】:Flutter Drawer Overlaps the bottom Navigation barFlutter Drawer 与底部导航栏重叠
【发布时间】:2021-01-01 01:52:33
【问题描述】:

----------

这是我的问题的第一个屏幕截图

这是实现单子滚动视图后的第二秒截图

我已经创建了一个导航抽屉和一个底部导航小部件,我遇到了以下问题/

  1. 打开抽屉时,它说抽屉超过 XX 像素,所以我将其包裹在“单子滚动视图中,现在抽屉像一整页一样打开。

  2. 此外,当抽屉被按下时,底部导航会覆盖它。

  3. 我添加了图片,您可以通过点击上方看到。

这是我的一段代码。

    class Mydrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              UserAccountsDrawerHeader(
                accountName: Text('Name'),
                accountEmail: Text('Username'),
                currentAccountPicture: CircleAvatar(
                  backgroundColor: Colors.white,
                  child: Text('Hi'),
                ),
              ),
              ListTile(
                  leading: Icon(Icons.home),
                  title: Text(
                    'Home Page',
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    Navigator.of(context).pushNamed(MyHomepage.route);
                  }),
              ListTile(
                leading: Icon(Icons.person),
                title: Text(
                  'My Account',
                ),
                onTap: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushNamed(Account.route);
                },
              ),
              ListTile(
                leading: Icon(Icons.assignment),
                title: Text(
                  'My Lists',
                ),
                onTap: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushNamed(Mylist.route);
                },
              ),
              ListTile(
                leading: Icon(Icons.bookmark),
                title: Text(
                  'Wishlist',
                ),
                onTap: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushNamed(Wishlist.route);
                },
              ),
              Divider(),
              ListTile(
                leading: Icon(Icons.mail),
                title: Text(
                  'Contact us',
                ),
                onTap: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushNamed(Contactus.route);
                },
              ),
              ListTile(
                leading: Icon(Icons.info),
                title: Text(
                  'Info & FAQ',
                ),
                onTap: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushNamed(Infofaq.route);
                },
              ),
              Divider(),
              ListTile(
                leading: Icon(Icons.lock_open),
                title: Text(
                  'Logout',
                ),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Bottom Navigation Code

class Nav extends StatefulWidget {
  @override
  _NavState createState() => _NavState();
}

class _NavState extends State<Nav> {
  int _selectedIndex = 0;
  final List<Widget> _widgetOptions = [
    NavHome(),
    NavInspiration(),
    NavNotification(),
    NavMessages(),
  ];

  void _onitemtap(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _widgetOptions[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: false,
        showUnselectedLabels: false,
        type: BottomNavigationBarType.fixed,
        onTap: _onitemtap,
        currentIndex: _selectedIndex,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.filter_none),
            title: Text('Inspiration'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.notifications_none),
            title: Text('Notifications'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.mail_outline),
            title: Text('Messages'),
          ),
        ],
      ),
    );
  }
}

主要飞镖

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter demo',
      home: Nav(),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      //home: Homepage(),
      //initialRoute: '/',
      routes: {
        MyHomepage.route: (_) => MyHomepage(),
        Account.route: (_) => Account(),
        Mylist.route: (_) => Mylist(),
        Wishlist.route: (_) => Wishlist(),
        Contactus.route: (_) => Contactus(),
        Infofaq.route: (_) => Infofaq(),
      },
    );
  }
}


----------

【问题讨论】:

  • 这两个类你怎么称呼?
  • 在 main.dart 文件中,我在“home:”下调用了 Nav() 类,并为抽屉创建了路线。我们必须调用 main.dart 上的两个类吗?
  • 我可以看看 main.dart 吗?抱歉,我无法在脑海中想象它:/
  • 是的,我已将其添加到上面的代码中
  • 我添加了重叠透明的图片,请检查

标签: flutter user-interface dart flutter-layout


【解决方案1】:

检查一下!

It's work for me

使用drawer 的一部分scaffold()

【讨论】:

  • 谢谢你,我试过了,遇到了像素问题,我也用我的问题添加了那个图像。请查看
【解决方案2】:

在这里可以做的最好的事情是,就像您将抽屉分开一样,创建一个没有脚手架或主体的单独导航栏类,只有导航栏。现在,在包含脚手架的第三个小部件中调用这两个,抽屉和导航栏。要更改索引,您可以将函数作为参数传递给导航栏小部件。

【讨论】:

    【解决方案3】:

    我已经改变了你传递代码的方式,尝试在你的代码中实现它。我已经分解了Nav(),所以请记住我没有使用Nav()作为一个整体

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: MainEntry(),
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          //home: Homepage(),
          //initialRoute: '/',
          routes: {
            MyHomepage.route: (_) => MyHomepage(),
            Account.route: (_) => Account(),
            Mylist.route: (_) => Mylist(),
            Wishlist.route: (_) => Wishlist(),
            Contactus.route: (_) => Contactus(),
            Infofaq.route: (_) => Infofaq(),
          },
        );
      }
    }
    
    
    class MainEntry extends StatefulWidget {
      @override
      _MainEntryState createState() => _MainEntryState();
    }
    
    class _MainEntryState extends State<MainEntry> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(debugShowCheckedBanner: false,
          home: Scaffold(
            drawer: MyDrawer(), 
    
            body: _widgetOptions[_selectedIndex] //your body
    
            bottomNavigationBar: BottomNavigationBar(
              showSelectedLabels: false,
              showUnselectedLabels: false,
              type: BottomNavigationBarType.fixed,
              onTap: _onitemtap,
              currentIndex: _selectedIndex,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.filter_none),
                title: Text('Inspiration'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.notifications_none),
                title: Text('Notifications'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.mail_outline),
                title: Text('Messages'),
              ),
            ],
          )
         )
        );
      }
    }
    

    【讨论】:

    • 您遇到的错误是什么?我已经更新了答案中的代码
    • 如果它按预期工作,请将其标记为答案并投票!谢谢! :D
    • 在脚手架下,我将导航添加到底部导航,它正在生成错误,现在我已经执行了您的编辑。但条件是一样的
    • 也许重新考虑你的路由,你为什么要在抽屉类中返回MaterialApp?只返回SingleChildScrollView 应该没问题,如果不是,则用脚手架包裹SingleChildScrollView
    • @MukeshBhardwaj 试试上面我刚刚编辑的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2020-02-07
    • 2018-04-25
    相关资源
    最近更新 更多