Flutter 自定義Drawer 滑出位置的大小實(shí)例代碼詳解
Flutter開發(fā)過程中,Drawer控件的使用頻率也是比較高的,其實(shí)有過移動(dòng)端開發(fā)經(jīng)驗(yàn)的人來說,F(xiàn)lutter中的Drawer控件就相當(dāng)于ios開發(fā)或者Android開發(fā)中的“抽屜”效果,從側(cè)邊欄滑出導(dǎo)航菜單。對(duì)于Flutter中的Drawer控件的常規(guī)用法就不多介紹,網(wǎng)上大把的教程。
那么本篇博文分享一個(gè)網(wǎng)上教程不多的一個(gè)知識(shí)點(diǎn),那就是自定義Drawer的滑出位置的大小,自定義Drawer滑出位置就需要修改一個(gè)double的widthPercent屬性,widthPercent一般默認(rèn)值是0.7,然后想要修改widthPercent的默認(rèn)值,或者設(shè)置想要的任何大于0小于1之間的值都可以根據(jù)這個(gè)來設(shè)置。具體操作如下所示:
1、首先封裝這個(gè)方法(看官可以直接復(fù)制使用)
class CustomDrawer extends StatelessWidget {
final double elevation;
final Widget child;
final String semanticLabel;
final double widthPercent;
const CustomDrawer({
Key key,
this.elevation = 16.0,
this.child,
this.semanticLabel,
this.widthPercent = 0.7,
}) :
assert(widthPercent!=null&&widthPercent<1.0&&widthPercent>0.0)
,super(key: key);
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context));
String label = semanticLabel;
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
label = semanticLabel;
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
label = semanticLabel ?? MaterialLocalizations.of(context)?.drawerLabel;
}
final double _width=MediaQuery.of(context).size.width*widthPercent;
return Semantics(
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
label: label,
child: ConstrainedBox(
constraints: BoxConstraints.expand(width: _width),
child: Material(
elevation: elevation,
child: child,
),
),
);
}
}

2、調(diào)用的地方

@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new AccountManagersPage('')));
},
child: new CustomDrawer( //調(diào)用修改Drawer的方法
widthPercent:0.5, //設(shè)置Drawer滑出位置居屏幕的一半寬度
child: Container(
color: Color(0xFF1F1D5B),
child: Column(
children: <Widget>[
Expanded(
child: ListView(children: <Widget>[
Column(
children: <Widget>[
ListTile(
title: Text('設(shè)備列表',
style: TextStyle(color: Color(0xFF8B89EF))),
contentPadding: EdgeInsets.symmetric(
horizontal: 15.0, vertical: 0.0),
),
ListTile(
leading: CircleAvatar(
backgroundImage: new ExactAssetImage(
'images/menu_lamp_pole.png'),
radius: 15.0,
),
title: Text('燈桿',
style: TextStyle(
color: Color(0xFFffffff),
fontSize: 18.0,
)),
onTap: () {
Navigator.of(context).pop();
//Navigator.of(context).push(new MaterialPageRoute(builder:(BuildContext context) => new BigScreenPage(0,'燈桿列表')));
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) =>
new MainPoleView()));
}),
// Divider(),
ListTile(
leading: CircleAvatar(
backgroundImage:
new ExactAssetImage('images/menu_charge.png'),
radius: 15.0,
),
title: Text('充電樁',
style: TextStyle(
color: Color(0xFFffffff),
fontSize: 18.0,
)),
onTap: () {
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) =>
new BigScreenPage(6, '充電樁列表')));
}),
],
)
]),
)
],
),
),
),
);
}
實(shí)現(xiàn)效果如下所示:

總結(jié)
到此這篇關(guān)于Flutter 自定義Drawer 滑出位置的大小的文章就介紹到這了,更多相關(guān)flutter 自定義drawer內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決
這篇文章主要介紹了Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Android編程實(shí)現(xiàn)仿優(yōu)酷旋轉(zhuǎn)菜單效果(附demo源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿優(yōu)酷旋轉(zhuǎn)菜單效果的方法,較為詳細(xì)的分析了Android實(shí)現(xiàn)旋轉(zhuǎn)菜單的布局與功能實(shí)現(xiàn)技巧,并附帶完整的demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
RecyclerView實(shí)現(xiàn)流式標(biāo)簽單選多選功能
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。這篇文章主要介紹了RecyclerView實(shí)現(xiàn)的流式標(biāo)簽單選多選功能,需要的朋友可以參考下2019-11-11
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對(duì)大家有所幫助2016-02-02
Android仿Flipboard動(dòng)畫效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿Flipboard動(dòng)畫效果的實(shí)現(xiàn)代碼,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01
Android?Camera開發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件
這篇文章主要為大家詳細(xì)介紹了Android?Camera開發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法,涉及Android基于新浪接口的調(diào)用及數(shù)據(jù)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android使用AIDL實(shí)現(xiàn)兩個(gè)App間通信
這篇文章主要為大家詳細(xì)介紹了Android使用AIDL實(shí)現(xiàn)兩個(gè)App間通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
android圖庫(kù)豎屏不顯示status bar的解決方法
圖庫(kù)在JB和JB2的版本上顯示的行為是:橫屏全屏顯示,豎屏?xí)@示status bar,圖庫(kù)在JB和JB2的版本上顯示的行為是:橫屏全屏顯示,豎屏?xí)@示status bar,具體實(shí)現(xiàn)方法如下,不會(huì)的朋友可以參考下哈2013-06-06

