Fiutter- 案例1 (主界面)

前言

本來想把之前自己的ReactNativeDemo項(xiàng)目拿來變成Flutter版本,但是這幾年一直搞Native開發(fā)回過頭去看RN的項(xiàng)目好多都不明白了,真是開發(fā)者的噩夢(mèng),接觸好多東西不使用的話漸漸地都模糊了,所以還是得把有的東西重點(diǎn)記下來非区,就相當(dāng)于是自己的筆記一樣沒事翻著看看,這次Flutter就選擇豆瓣去嘗試下盹廷,畢竟豆瓣也有開放的API供開發(fā)者使用

底部導(dǎo)航

微信圖片_20220529161305.jpg

首頁 書影音 小組 市集 我

這部分選擇使用BottomNavigationBar 去做底部導(dǎo)航欄的實(shí)現(xiàn)

搭建項(xiàng)目

創(chuàng)建一個(gè)新的Flutter項(xiàng)目

HomePage相關(guān)抽離出來到另一個(gè)Dart文件中征绸,并更改部分命名為MainPage

使用Route管理頁面切換,默認(rèn)進(jìn)入Main頁面

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.green,
      ),
      routes: {"/main": (BuildContext context) => MainPage(title: "Main")},
      initialRoute: "/main",
    );
  }
}

使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航UI俄占,并為底部導(dǎo)航添加事件

class MainPage extends StatefulWidget {
  MainPage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int currentIndex = 0;

  void onNavigationChanged(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    print('build');

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁'),
          BottomNavigationBarItem(icon: Icon(Icons.book), label: '書影音'),
          BottomNavigationBarItem(icon: Icon(Icons.group), label: '小組'),
          BottomNavigationBarItem(icon: Icon(Icons.store), label: '市集'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '我')
        ],
        currentIndex: currentIndex,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.green,
        type: BottomNavigationBarType.fixed,
        onTap: (index)=>{
          onNavigationChanged(index)
        },
      ),
    );
  }
}

這里默認(rèn)使用了有狀態(tài)可變的StatefulWidget是因?yàn)槲覀兊捻撁媸强勺兊墓艿。谖覀冞x中其他底部功能時(shí)頁面是需要刷新的

onTap函數(shù)調(diào)用之后,我們更改了屬性currentIndex 缸榄,同時(shí)調(diào)用了

   setState(() {
      currentIndex = index;
    });

然后界面就會(huì)刷新渤弛,build函數(shù)會(huì)重新去渲染已得到更新的頁面,細(xì)節(jié)后面我們可以一起看看setState函數(shù)的實(shí)現(xiàn)

效果

微信圖片_20220529173942.jpg

歡迎關(guān)注Mike的簡(jiǎn)書

Android 知識(shí)整理

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末甚带,一起剝皮案震驚了整個(gè)濱河市她肯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鹰贵,老刑警劉巖醋拧,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝗岖,死亡現(xiàn)場(chǎng)離奇詭異汽纤,居然都是意外死亡胰丁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門腊瑟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人块蚌,你說我怎么就攤上這事闰非。” “怎么了峭范?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵财松,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng)辆毡,這世上最難降的妖魔是什么菜秦? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮舶掖,結(jié)果婚禮上球昨,老公的妹妹穿的比我還像新娘。我一直安慰自己眨攘,他們只是感情好主慰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鲫售,像睡著了一般共螺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上情竹,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天藐不,我揣著相機(jī)與錄音,去河邊找鬼秦效。 笑死雏蛮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的棉安。 我是一名探鬼主播底扳,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼贡耽!你這毒婦竟也來了衷模?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蒲赂,失蹤者是張志新(化名)和其女友劉穎阱冶,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體滥嘴,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡木蹬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了若皱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片镊叁。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖走触,靈堂內(nèi)的尸體忽然破棺而出晦譬,到底是詐尸還是另有隱情,我是刑警寧澤互广,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布敛腌,位于F島的核電站卧土,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏像樊。R本人自食惡果不足惜尤莺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望生棍。 院中可真熱鬧颤霎,春花似錦、人聲如沸足绅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽氢妈。三九已至粹污,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間首量,已是汗流浹背壮吩。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留加缘,地道東北人鸭叙。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像拣宏,于是被迫代替她去往敵國(guó)和親沈贝。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

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