在flutter中的不少頁面中,都會有下面這段代碼:app
對應就是下圖中的紅色線框區域,被稱做AppBar頂部導航。less
在使用AppBar以前,咱們先新建一個tabBar的項目:ide
而後在pages文件夾下新建AppBarDemo.dart頁面:ui
import 'package:flutter/material.dart'; class AppBarDemoPage extends StatelessWidget { const AppBarDemoPage({Key key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar:AppBar( title:Text('AppBarDemo'), ), body:Text('1111'), ); } }
而後配置路由:spa
並在Home.dart中添加跳轉按鈕:3d
可是這種狀況下,項目啓動後,默認加載的仍是Home.dart頁面,而後點擊按鈕,跳轉到AppBarDemo.dart頁面,爲了方便操做,這裏設置默認加載AppBarDemo.dart頁面,只須要修改main.dart中的code
在flutter中,AppBar有如下經常使用的可選屬性:blog
AppBarDemo.dart路由
import 'package:flutter/material.dart'; class AppBarDemoPage extends StatelessWidget { const AppBarDemoPage({Key key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title:Text("AppBarDemoPage"), // backgroundColor: Colors.red, centerTitle:true, leading: IconButton( icon: Icon(Icons.menu), onPressed: (){ print('menu'); }, ), actions: <Widget>[ IconButton( icon: Icon(Icons.search), onPressed: (){ print('search'); }, ), IconButton( icon: Icon(Icons.settings), onPressed: (){ print('settings'); }, ) ], ), body: Text('1111'), ); } }
代碼下載:點這裏(提取碼:fu4v)get