Flutter學(xué)習(xí)第六天:Flutter如何實(shí)現(xiàn)頂部導(dǎo)航佛寿,底部導(dǎo)航菜單,側(cè)拉欄菜單等功能但壮?

1.頂部導(dǎo)航

代碼如下:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: TabbedAppBarSample(),
  ));
}

class TabbedAppBarSample extends StatefulWidget {
  @override
  _TabbedAppBarSampleState createState() => _TabbedAppBarSampleState();
}


///頂部選擇彈框
class _TabbedAppBarSampleState extends State<TabbedAppBarSample> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '頂部導(dǎo)航',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DefaultTabController(
        length: choices.length,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Tabbed AppBar'),
            bottom: TabBar(
              isScrollable: true,
              tabs:choices.map((Choice choice){
                return Tab(
                  text: choice.title,
                  icon: Icon(choice.icon),
                );
              }).toList(),
            ),
          ),
          body: TabBarView(
            children: choices.map((Choice choice){
              return Padding(
                  padding:const EdgeInsets.all(16.0),
                  child: ChoiceCard(choice:choice) ,
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}

class Choice{
  final String title;
  final IconData icon;
  const Choice({
    this.title,
    this.icon,
  });
}


///初始化數(shù)據(jù)
const List<Choice> choices=const  <Choice>[
  const Choice(title: 'CAR',icon: Icons.directions_car),
  const Choice(title: 'BICYCLE', icon: Icons.directions_bike),
  const Choice(title: 'BOAT',icon: Icons.directions_boat),
  const Choice(title: 'BUS',icon: Icons.directions_bus),
  const Choice(title: 'TRAIN', icon: Icons.directions_railway),
  const Choice(title: 'WALK', icon: Icons.directions_walk),
];


///頁面的內(nèi)容
class ChoiceCard extends StatelessWidget{
  const ChoiceCard({Key key, this.choice}):super(key:key);
  final Choice choice;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final TextStyle textStyle=Theme.of(context).textTheme.display2;
    return Card(
      color: Colors.white,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children:<Widget> [
            Icon(choice.icon,size: 128,color: textStyle.color,),
            Text(choice.title,style: textStyle,),
          ],
        ),
      ),
    );
  }
}

效果:


image

2.底部導(dǎo)航菜單

HomePage,SearchPage,TravelPage,MyPage頁面自行定義冀泻。

import 'package:flutter/material.dart';
import 'package:home_app/HomePage.dart';
import 'package:home_app/MyPage.dart';
import 'package:home_app/SearchPage.dart';
import 'package:home_app/TravelPage.dart';


void main() {
  runApp(MaterialApp(
    home: TabNavigator(),
  ));
}

class TabNavigator extends StatefulWidget {
  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> with SingleTickerProviderStateMixin {
  final _defaultColor=Colors.grey;
  final _activeColor=Colors.blue;
  int _currentIndex=0;
  var _controller=PageController(
    initialPage: 0,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: <Widget>[HomePage(),SearchPage(),TravelPage(),MyPage()],
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index){
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex=index;
          });
        },
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon:Icon(
                Icons.home,
                color: _defaultColor,
              ),
              activeIcon: Icon(
                Icons.home,
                color: _activeColor,
              ),
              title: Text(
                '首頁',
                style: TextStyle(
                  color: _currentIndex !=0 ?_defaultColor:_activeColor),
                ),
              ),
          BottomNavigationBarItem(
            icon:Icon(
              Icons.search,
              color: _defaultColor,
            ),
            activeIcon: Icon(
              Icons.search,
              color: _activeColor,
            ),
            title: Text(
              '搜索',
              style: TextStyle(
                  color: _currentIndex !=1 ?_defaultColor:_activeColor),
            ),
          ),
          BottomNavigationBarItem(
            icon:Icon(
              Icons.camera_alt,
              color: _defaultColor,
            ),
            activeIcon: Icon(
              Icons.camera_alt,
              color: _activeColor,
            ),
            title: Text(
              '旅拍',
              style: TextStyle(
                  color: _currentIndex !=2 ?_defaultColor:_activeColor),
            ),
          ),
          BottomNavigationBarItem(
            icon:Icon(
              Icons.account_circle,
              color: _defaultColor,
            ),
            activeIcon: Icon(
              Icons.account_circle,
              color: _activeColor,
            ),
            title: Text(
              '我的',
              style: TextStyle(
                  color: _currentIndex !=3 ?_defaultColor:_activeColor),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }
}

效果:


image

3.側(cè)拉欄菜單功能

main.dart頁面

import 'package:flutter/material.dart';
import 'package:home_app/TabNavigator.dart';
import 'package:home_app/TabbedAppBarSample.dart';

import 'MyHomePage.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  final appTitle = 'Drawer Demo';
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: appTitle,
      home: MyHomePage(title: appTitle),
    );
  }
}

MyHomePage頁面

import 'package:flutter/material.dart';

class MyHomePage extends StatelessWidget {
  final String title;
  MyHomePage({Key key,this.title}) :super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title),),
      body: Center(child: Text('My Page!'),),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children:<Widget> [
            DrawerHeader(
                child: Text('Drawer Header'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap:(){
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: (){
                Navigator.pop(context);
              },
            )
          ],
        ),
      ),
    );
  }
}

效果:


image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蜡饵,隨后出現(xiàn)的幾起案子弹渔,更是在濱河造成了極大的恐慌,老刑警劉巖溯祸,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肢专,死亡現(xiàn)場離奇詭異,居然都是意外死亡焦辅,警方通過查閱死者的電腦和手機(jī)博杖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來筷登,“玉大人剃根,你說我怎么就攤上這事∏胺剑” “怎么了狈醉?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵廉油,是天一觀的道長。 經(jīng)常有香客問我苗傅,道長抒线,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任金吗,我火速辦了婚禮十兢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘摇庙。我一直安慰自己旱物,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布卫袒。 她就那樣靜靜地躺著宵呛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪夕凝。 梳的紋絲不亂的頭發(fā)上宝穗,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音码秉,去河邊找鬼逮矛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛转砖,可吹牛的內(nèi)容都是我干的须鼎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼府蔗,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼晋控!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起姓赤,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤赡译,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后不铆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝌焚,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年狂男,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了综看。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡岖食,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出舞吭,到底是詐尸還是另有隱情泡垃,我是刑警寧澤析珊,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站蔑穴,受9級(jí)特大地震影響忠寻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜存和,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一奕剃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捐腿,春花似錦纵朋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宪祥,卻和暖如春聂薪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝗羊。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來泰國打工藏澳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人耀找。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓翔悠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涯呻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子凉驻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容