溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Flutter如何實現菜單彈出框PopupMenuButton功能

發布時間:2021-07-09 16:23:20 來源:億速云 閱讀:1705 作者:小新 欄目:web開發

這篇文章將為大家詳細講解有關Flutter如何實現菜單彈出框PopupMenuButton功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

相信在實際開發過程當中,肯定少不了這樣的功能:

Flutter如何實現菜單彈出框PopupMenuButton功能

點擊 AppBar 右上角的按鈕,彈出一個菜單供用戶選擇。

幸運的是,Flutter 提供給我們了一個 Widget,直接就能實現如上的效果。

PopupMenuButton

還是老規矩,先看官方的說明:

Displays a menu when pressed and calls onSelected [1] when the menu is dismissed because an item was selected. The value passed to  onSelected [2] is the value of the selected menu item.

One of child [3] or  icon [4] may be provided, but not both. If  icon [5] is provided, then  PopupMenuButton [6] behaves like an  IconButton [7] .

If both are null, then a standard overflow icon is created (depending on the platform).

大致意思為:

當按下的時候顯示一個菜單,選擇了一個項目的時候會回調 onSelected ,傳遞的值是所選菜單的值。

可以提供 child or  icon ,但是不能同時提供。

如果為空,則提供一個默認的圖標,取決于平臺。

構造函數

看完了官方說明,再來看構造函數:

const PopupMenuButton({
 Key key,
 @required this.itemBuilder,
 this.initialValue,
 this.onSelected,
 this.onCanceled,
 this.tooltip,
 this.elevation = 8.0,
 this.padding = const EdgeInsets.all(8.0),
 this.child,
 this.icon,
 this.offset = Offset.zero,
 this.enabled = true,
}) : assert(itemBuilder != null),
assert(offset != null),
assert(enabled != null),
assert(!(child != null && icon != null)), // fails if passed both parameters
super(key: key);

這里面每一個參數應該都很好理解,就不做過多的解釋了,

唯一必傳的參數就是 itemBuilder ,也可以看到后面的斷言:

assert(!(child != null && icon != null)) 判斷了 child 、icon 是否同時不為空,如果是的話就報錯了。

簡單 Demo

構造函數理解了,官方也提供了一個 Demo,我們來看一下運行效果:

Flutter如何實現菜單彈出框PopupMenuButton功能

再來看一下代碼:

/// 首先定義了一個枚舉
enum WhyFarther {
 harder,
 smarter,
 selfStarter,
 tradingCharter,
}
/// ------------------------------------
/// build 方法
Widget build(BuildContext context) {
 return Scaffold(
 appBar: AppBar(
 title: Text('PopupMenuButtonPage'),
 actions: <Widget>[
 PopupMenuButton<WhyFarther>(
 onSelected: (WhyFarther result) {
 setState(() {
 _selection = result;
 });
 },
 icon: Icon(Icons.more_vert),
 itemBuilder: (BuildContext context) => <PopupMenuEntry<WhyFarther>>[
 const PopupMenuItem<WhyFarther>(
 value: WhyFarther.harder,
 child: Text('Working a lot harder'),
 ),
 const PopupMenuItem<WhyFarther>(
 value: WhyFarther.smarter,
 child: Text('Being a lot smarter'),
 ),
 const PopupMenuItem<WhyFarther>(
 value: WhyFarther.selfStarter,
 child: Text('Being a self-starter'),
 ),
 const PopupMenuItem<WhyFarther>(
 value: WhyFarther.tradingCharter,
 child: Text('Placed in charge of trading charter'),
 ),
 ],
 ),
 ],
 ),
 body: Container(),
 );
}

解釋一下邏輯:

1. 首先定義了一個枚舉

2. 然后在  AppBar  的「actions」里定義了  PopupMenuButton

3. 設置 icon 為  Icon(Icons.more_vert)

4. itemBuilder  需返回一個  List<PopupMenuEntry<T>>

5. 這里傳入的值就是  PopupMenuItem<WhyFarther>

6. 然后定義  onSelected  參數接收點擊回調

這樣整體的邏輯就是定義好了,運行一下:

Flutter如何實現菜單彈出框PopupMenuButton功能

關于“Flutter如何實現菜單彈出框PopupMenuButton功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女