Flutter用BottomNavigationBar制作底部導航欄與頁面跳轉(zhuǎn)

看完flutter的基礎(chǔ)資料扣囊,準備開始做一個簡單的demo練練手,記錄下練習效果悔橄。

1靶累、創(chuàng)建頁面

先創(chuàng)建幾個界面類,作為主頁面用來顯示(創(chuàng)建三到四個橄维,代碼直接拷貝尺铣,修改下類名和appbar的屬性就可以了)

import 'package:flutter/material.dart';
class KYVMHome extends StatefulWidget {
  @override
  _KYVMHomeState createState() => _KYVMHomeState();
}

class _KYVMHomeState extends State<KYVMHome> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  Widget build(BuildContext context) {
    super.build(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('首頁'),
        backgroundColor: Colors.blue,
      ),
      body: Container(
        color: Colors.blue,
      ),
    );
  }
}

2、創(chuàng)建導航類

創(chuàng)建一個底部導航類争舞,本次使用BottomNavigationBar來作為應(yīng)用的導航

import 'package:flutter/material.dart';
import 'package:my_test/home/kyvm_home.dart';
import 'package:my_test/message/kyvm_message.dart';
import 'package:my_test/person_center/kyvm_person_center.dart';


class KYVMNavigate extends StatefulWidget {
  @override
  _NavigateState createState() => _NavigateState();
}

class _NavigateState extends State<KYVMNavigate>
  with SingleTickerProviderStateMixin {
    
  final _bottomNavigationColor = Colors.grey;
  final _bottomNavigationSelectColor = Colors.blue;
  int _currentIndex = 0;
  var _controller = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: <Widget>[
          KYVMHome(),
          KYVMMessage(),
          KYVMPersonCenter(),
        ],
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.home,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.home,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '首頁',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.message,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.message,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '消息',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.person,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.person,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '我的',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
        ],
      ),
    );
  }
}

3、修改main.dart

main.dart 是我們程序的入口澈灼。就類似于 Java竞川、OC 中的 main() 店溢,作為一個程序的入口。我們將 main.dart 作為程序的啟動入口委乌,就不做過多的操作床牧,只是指定去加載我們的導航類。

import 'package:flutter/material.dart';
import 'package:my_test/navigate/kyvm_navigate.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      // home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: KYVMNavigate(),
    );
  }
}

4遭贸、頁面跳轉(zhuǎn)

本次使用的是系統(tǒng)自帶的方法
跳轉(zhuǎn)到下一頁面

Navigator.push(
     context,
     MaterialPageRoute(builder: (context) => CarLocation()),
 );

返回上一頁面

Navigator.pop(context);

因為對各種控件和方法不太熟悉戈咳,暫時使用的都是系統(tǒng)自帶方法,等熟悉后再自定義一些公共類之類的來提高開發(fā)效率

練習demo地址: https://github.com/liuxiangsheng/flutter_bottomNavigate

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末壕吹,一起剝皮案震驚了整個濱河市著蛙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耳贬,老刑警劉巖踏堡,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異咒劲,居然都是意外死亡顷蟆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門腐魂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來帐偎,“玉大人,你說我怎么就攤上這事蛔屹∠鞣” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵判导,是天一觀的道長嫉父。 經(jīng)常有香客問我,道長眼刃,這世上最難降的妖魔是什么绕辖? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮擂红,結(jié)果婚禮上仪际,老公的妹妹穿的比我還像新娘。我一直安慰自己昵骤,他們只是感情好树碱,可當我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著变秦,像睡著了一般成榜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蹦玫,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天赎婚,我揣著相機與錄音刘绣,去河邊找鬼。 笑死挣输,一個胖子當著我的面吹牛纬凤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播撩嚼,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼停士,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了完丽?” 一聲冷哼從身側(cè)響起恋技,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎舰涌,沒想到半個月后猖任,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡瓷耙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年朱躺,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搁痛。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡长搀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鸡典,到底是詐尸還是另有隱情源请,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布彻况,位于F島的核電站谁尸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏纽甘。R本人自食惡果不足惜良蛮,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望悍赢。 院中可真熱鬧决瞳,春花似錦、人聲如沸左权。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赏迟。三九已至屡贺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背烹笔。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工裳扯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留抛丽,地道東北人谤职。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像亿鲜,于是被迫代替她去往敵國和親允蜈。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,612評論 2 350

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

  • 本文翻譯自 Add-Flutter-to-existing-apps 在已有項目中繼承Flutter 約定:本文中...
    黃二瓜閱讀 9,059評論 3 21
  • 一蒿柳、Flutter是什么饶套? 根據(jù)Flutter中文官網(wǎng)(英文官網(wǎng))的解釋:Flutter是谷歌的移動UI框架,可以...
    鴻羽羽羽羽羽閱讀 922評論 4 13
  • 特別說明 當前博客平臺賬號已廢棄垒探,如果有使用細節(jié)問題請前往我新博客平臺進行討論交流妓蛮。 個人博客平臺 HuRuWo的...
    善篤有余劫閱讀 4,947評論 0 30
  • 英文官網(wǎng):https://flutter.io/中文網(wǎng):https://flutterchina.club/ 首先...
    超威藍貓l閱讀 2,818評論 1 3
  • 湖邊的黃昏 太陽和水追逐戲弄 風兒捎來了媽媽的呼喚 于是,太陽慢慢的離去 不知為啥又紅了臉...... ...
    阿莢閱讀 220評論 0 6