赞
踩
这里的 drawer 为常见的显示在 App 左上角的组件,用于导航,web app 中 类似的组件有时也叫 hamburger 汉堡包,如下图所示三横线图标:
点击组件实现导航:
组件代码如下:
// app_drawer.dart import 'package:flutter/material.dart'; import '../screens/orders_screen.dart'; class AppDrawer extends StatelessWidget { @override Widget build(BuildContext context) { return Drawer( child: Column( children: <Widget>[ AppBar( title: Text('SHOPPING'), automaticallyImplyLeading: false, ), Divider(), ListTile( leading: Icon(Icons.shop), title: Text('Shop'), onTap: (() { // 导航 Navigator.of(context).pushReplacementNamed('/'); }), ), Divider(), ListTile( leading: Icon(Icons.payment), title: Text('Orders'), onTap: (() { // 导航 Navigator.of(context) .pushReplacementNamed(OrdersScreen.routeName); }), ), ], ), ); } }
应用此组件的组件中增加 drawer
选项,例如 products_overview_screen.dart
:
// `products_overview_screen.dart` enum FilterOptions { Favorites, All, } class ProductsOverviewScreen extends StatefulWidget { @override State<ProductsOverviewScreen> createState() => _ProductsOverviewScreenState(); } class _ProductsOverviewScreenState extends State<ProductsOverviewScreen> { var _showOnlyFavorites = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('MyShop'), actions: <Widget>[ PopupMenuButton( itemBuilder: (_) => [ PopupMenuItem( child: Text('Only favorites'), value: FilterOptions.Favorites, ), PopupMenuItem( child: Text('Show all'), value: FilterOptions.All, ), ], icon: Icon(Icons.more_vert), onSelected: (FilterOptions selectedValue) { setState(() { if (selectedValue == FilterOptions.Favorites) { _showOnlyFavorites = true; } else { _showOnlyFavorites = false; } }); }), Consumer<Cart>( builder: (_, cart, ch) => Badge( child: ch, value: cart.itemCount.toString(), ), child: IconButton( icon: Icon(Icons.shopping_cart), onPressed: () { Navigator.of(context).pushNamed( CartScreen.routeName, // arguments: product.id, ); }, ), ) ], ), / // only this line needed to add drawer drawer: AppDrawer(), / body: ProductsGrid(_showOnlyFavorites), ); } }
需要此 drawer 组件的任意组件都可以像上面那样增加一行 code 就行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。