广告
返回顶部
首页 > 资讯 > 移动开发 >FlutterDrawer抽屉菜单示例详解
  • 243
分享到

FlutterDrawer抽屉菜单示例详解

2024-04-02 19:04:59 243人浏览 薄情痞子
摘要

本文实例为大家分享了Flutter Drawer抽屉菜单示例代码,供大家参考,具体内容如下 一.Flutter Drawer组件简介 1.源码查看 const Drawer({  

本文实例为大家分享了Flutter Drawer抽屉菜单示例代码,供大家参考,具体内容如下

一.Flutter Drawer组件简介

1.源码查看

const Drawer({
    Key? key,
    this.elevation = 16.0, //阴影效果大小
    this.child, //内容元素
    this.semanticLabel, //关闭/打开抽屉时的通知信息
  }) 

二.抽屉菜单示例

1.菜单项,使用 ListTile 实现

Expanded(
              child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: const Icon(Icons.person),
                    title: const Text('Personal'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.message),
                    title: const Text('infORMation'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.settings),
                    title: const Text('about'),
                  ),
                ],
              ),
),

2.底部导航栏,使用BottomNavigationBar实现

bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.blue,
        
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "首页",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: "通讯录",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.find_in_page),
              label: "发现",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.flip_outlined),
              label: "我的",
              //backgroundColor:Colors.blue
          ),
        ],
 
        onTap: (index){
          setState(() {
            print("the index is :$index");
            currentIndex=index;
          });
        },
      ),

参考:flutter底部导航栏

3.悬浮按钮,使用FloatingActionButton实现

floatingActionButton: FloatingActionButton( //悬浮按钮
          child: Icon(Icons.add),
          onPressed:_onAddNum
      ),

三.Demo及实际效果

1.mydrawer.dart

import 'package:flutter/material.dart';
 
class MyDrawer extends StatelessWidget {
  const MyDrawer({
    Key? key,
  }) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return Drawer(
      elevation: 30,
      child: MediaQuery.removePadding(
        context: context,
        //移除抽屉菜单顶部默认的空白
        removeTop: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 30.0),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15.0),
                    child: ClipOval(
                      child: Image.asset(
                        "images/cc.png",
                        width: 60,
                      ),
                    ),
                  ),
                  Text(
                    "jon",
                    style: TextStyle(fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: const Icon(Icons.person),
                    title: const Text('Personal'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.message),
                    title: const Text('information'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.settings),
                    title: const Text('about'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

2.MainPage.dart

import 'package:flutter/material.dart';
import 'findpage.dart';
import 'mypage.dart';
import 'contactpage.dart';
import 'homepage.dart';
import 'mydrawer.dart';
 
class MainPage extends StatefulWidget{
  const MainPage({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState()=>_MainPageState();
}
 
class _MainPageState extends State<MainPage>{
 
  var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()];
  var currentIndex=0;
 
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar( //导航栏
        title: Text("App Name"),
        actions: <Widget>[ //导航栏右侧分享菜单
          IconButton(icon: Icon(Icons.share), onPressed: () {}),
        ],
      ),
      drawer: MyDrawer(), //菜单抽屉
      body: allPages[currentIndex],
      backgroundColor: Colors.green,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.blue,
        
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "首页",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: "通讯录",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.find_in_page),
              label: "发现",
              //backgroundColor:Colors.blue
          ),
 
          BottomNavigationBarItem(
              icon: Icon(Icons.flip_outlined),
              label: "我的",
              //backgroundColor:Colors.blue
          ),
        ],
 
        onTap: (index){
          setState(() {
            print("the index is :$index");
            currentIndex=index;
          });
        },
      ),
 
      floatingActionButton: FloatingActionButton( //悬浮按钮
          child: Icon(Icons.add),
          onPressed:_onAddNum
      ),
    );
  }
  void _onAddNum(){
  }
}

3.效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: FlutterDrawer抽屉菜单示例详解

本文链接: https://www.lsjlt.com/news/143567.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作