废话不多说,先看图:
上述动图中的效果,相信大多数小伙伴已经很熟悉了,在刚开始接触Android时,小伙伴做的最多的应该就是底部导航栏和侧滑导航栏了,作为Android开发中的经典业务场景,它们使用跨平台Flutter框架该怎么实现呢?接下来我们来一起看下侧滑导航栏在Flutter内的实现。
添加Drawer Widget
首先在Android Studio中新建Flutter工程,并删除无用代码,删除后的main.dart长这样:
import 'package:flutter/material.dart';void main() => runApp(MyApp());//main主入口,相当于Android原生的Application,随后runApp用于指定应用的首个Widget页面,这里就是MyAppclass MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);}
}class MyHomePage extends StatefulWidget {MyHomePage({Key key, this.title}) : super(key: key);final String title;@override_MyHomePageState createState() => _MyHomePageState();
}class _MyHomePageState extends State
}
随后添加Drawer
到页面布局中后,修改_MyHomePageState#build
函数代码如下
@overrideWidget build(BuildContext context) {return Scaffold(drawer: new Drawer(//添加侧滑菜单Widget),appBar: AppBar(title: Text(widget.title),),body: Center(child: Text("Drawer Demo Home Page!"),),);}
运行查看效果:
添加Drawer Content
定义DrawerHeader Widget,代码如下:
Widget _buildUserAccountDrawerHeader() {return new UserAccountsDrawerHeader(//系统提供的一种DrawerHeader layout样式accountName: Text("Thai"),//用户名accountEmail: Text("Thai@gmail.com"),//用户邮箱currentAccountPicture: Image.network( //头像"https://b-ssl.duitang.com/uploads/item/201602/15/20160215235057_EU3tS.thumb.700_0.jpeg?s=#34;,fit: BoxFit.cover,),);}
随后定义DrawerBody Widget:
Widget _buildDrawerBody() {return new Column(//列控件children:
定义Drawer content并填充到drawer内:
Widget buildDrawer(BuildContext context) {return new ListView(children:
drawer: new Drawer(child: buildDrawer(context),
),
运行即可看到开头的效果。 当前我们也可以自定义DrawerHeader布局,这里简单实现了,代码如下:
Widget _buildCustomDrawerHeader() {return new DrawerHeader(child: new Container(child: Row(mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.center,children:
运行效果如下:
个人微信公众号,欢迎大家加入。