Flutter Drawer抽屜菜單示例詳解
本文實(shí)例為大家分享了Flutter Drawer抽屜菜單示例代碼,供大家參考,具體內(nèi)容如下
一.Flutter Drawer組件簡(jiǎn)介
1.源碼查看
const Drawer({
? ? Key? key,
? ? this.elevation = 16.0, //陰影效果大小
? ? this.child, //內(nèi)容元素
? ? this.semanticLabel, //關(guān)閉/打開(kāi)抽屜時(shí)的通知信息
? })?二.抽屜菜單示例
1.菜單項(xiàng),使用 ListTile 實(shí)現(xiàn)
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.底部導(dǎo)航欄,使用BottomNavigationBar實(shí)現(xiàn)
bottomNavigationBar: BottomNavigationBar(
? ? ? ? currentIndex: currentIndex,
? ? ? ? type: BottomNavigationBarType.fixed,
? ? ? ? unselectedItemColor: Colors.grey,
? ? ? ? selectedItemColor: Colors.blue,
? ? ? ? /*unselectedLabelStyle:TextStyle(
? ? ? ? ? color: Colors.black
? ? ? ? ),*/
? ? ? ? items: [
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.home),
? ? ? ? ? ? ? label: "首頁(yè)",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.person),
? ? ? ? ? ? ? label: "通訊錄",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.find_in_page),
? ? ? ? ? ? ? label: "發(fā)現(xiàn)",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.flip_outlined),
? ? ? ? ? ? ? label: "我的",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
? ? ? ? ],
?
? ? ? ? onTap: (index){
? ? ? ? ? setState(() {
? ? ? ? ? ? print("the index is :$index");
? ? ? ? ? ? currentIndex=index;
? ? ? ? ? });
? ? ? ? },
? ? ? ),3.懸浮按鈕,使用FloatingActionButton實(shí)現(xiàn)
floatingActionButton: FloatingActionButton( //懸浮按鈕 ? ? ? ? ? child: Icon(Icons.add), ? ? ? ? ? onPressed:_onAddNum ? ? ? ),
三.Demo及實(shí)際效果
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,
? ? ? ? //移除抽屜菜單頂部默認(rèn)的空白
? ? ? ? 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( //導(dǎo)航欄
? ? ? ? title: Text("App Name"),
? ? ? ? actions: <Widget>[ //導(dǎo)航欄右側(cè)分享菜單
? ? ? ? ? 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,
? ? ? ? /*unselectedLabelStyle:TextStyle(
? ? ? ? ? color: Colors.black
? ? ? ? ),*/
? ? ? ? items: [
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.home),
? ? ? ? ? ? ? label: "首頁(yè)",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.person),
? ? ? ? ? ? ? label: "通訊錄",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.find_in_page),
? ? ? ? ? ? ? label: "發(fā)現(xiàn)",
? ? ? ? ? ? ? //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.效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03
Flutter質(zhì)感設(shè)計(jì)之持久底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之持久底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android自定義UI手勢(shì)密碼簡(jiǎn)單版
這篇文章主要為大家詳細(xì)介紹了Android自定義UI手勢(shì)密碼簡(jiǎn)單版2016-10-10
Kotlin中的Checked Exception機(jī)制淺析
這篇文章主要給大家介紹了關(guān)于Kotlin中Checked Exception機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android顯示系統(tǒng)SurfaceFlinger詳解
本文詳細(xì)講解了Android顯示系統(tǒng)SurfaceFlinger,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

