溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Flutter實(shí)現(xiàn)底部導(dǎo)航

發(fā)布時間:2020-09-09 00:38:26 來源:腳本之家 閱讀:131 作者:An-Ding 欄目:移動開發(fā)

本文實(shí)例為大家分享了Flutter實(shí)現(xiàn)底部導(dǎo)航的具體代碼,供大家參考,具體內(nèi)容如下

BottomNavigationBar使用

底部導(dǎo)航欄 主文件 main.dart (注意導(dǎo)入文件路徑)

import 'package:flutter/material.dart';
import './views/firstPage.dart';
import './views/secondPage.dart';
import './views/thirdPage.dart';
//首先導(dǎo)入三個界面

void main() {
 runApp(new MyApp());
}

class MyApp extends StatefulWidget {
 @override
 _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyApp> with TickerProviderStateMixin{

 int _tabIndex = 0;

 List<BottomNavigationBarItem> _navigationViews;

 var appBarTitles = ['首頁', '發(fā)現(xiàn)', '我的'];

 PageController pageController;

 var _body;

 initData() {
  _body = new IndexedStack(
   children: <Widget>[new FirstPage(), new SecondPage(), new ThirdPage()],
   index: _tabIndex,
  );
 }

 @override
 void initState() {
  super.initState();
  _navigationViews = <BottomNavigationBarItem>[
   new BottomNavigationBarItem(
    icon: const Icon(Icons.home),
    title: new Text(appBarTitles[0]),
    backgroundColor: Colors.blue,
   ),
   new BottomNavigationBarItem(
    icon: const Icon(Icons.widgets),
    title: new Text(appBarTitles[1]),
    backgroundColor: Colors.blue,
   ),
   new BottomNavigationBarItem(
    icon: const Icon(Icons.person),
    title: new Text(appBarTitles[2]),
    backgroundColor: Colors.blue,
   ),
  ];
 }

 final navigatorKey = GlobalKey<NavigatorState>();
 @override
 Widget build(BuildContext context) {

  initData();

  return new MaterialApp(
   navigatorKey: navigatorKey,
   theme: new ThemeData(
     primaryColor: Colors.blue,
     accentColor: Colors.blue
   ),
   home: new Scaffold(
    appBar: new AppBar(
     title: new Text(
      appBarTitles[_tabIndex],
      style: new TextStyle(color: Colors.white),
     ),
    ),
    body: _body,
    bottomNavigationBar: new BottomNavigationBar(
     items: _navigationViews
       .map((BottomNavigationBarItem navigationView) => navigationView)
       .toList(),
     currentIndex: _tabIndex,
     type: BottomNavigationBarType.fixed,
     onTap: (index) {
      setState(() {
       _tabIndex = index;
      });
     },
    ),
   ),
  );
 }
}

底部包含三個導(dǎo)航按鈕,分別對應(yīng)三個界面:

firstPage.dart

import 'package:flutter/material.dart';

class FirstPage extends StatefulWidget {
 @override
 State<StatefulWidget> createState() => new FirstPageState();

}

class FirstPageState extends State<FirstPage> {
 @override
 Widget build(BuildContext context) {
  return new Scaffold(
   body: new Center(
    child: new Text('這是第一個界面'),
   ),
  );
 }

}

secondPage.dart

import 'package:flutter/material.dart';

class SecondPage extends StatefulWidget {
 @override
 State<StatefulWidget> createState() => SecondPageState();

}

class SecondPageState extends State<SecondPage> {
 @override
 Widget build(BuildContext context) {
  return new Scaffold(
   body: new Center(
    child: new Text("這是我第二個頁面"),
   ),
  );
 }
}

thirdPage.dart

import 'package:flutter/material.dart';

class ThirdPage extends StatefulWidget {
 @override
 State<StatefulWidget> createState() => ThirdPageState();

}

class ThirdPageState extends State<ThirdPage>{
 @override
 Widget build(BuildContext context) {
  return new Scaffold(
   body: new Center(
    child: new Text('我是界面三'),
   ),
  );
 }
}

運(yùn)行截圖:

Flutter實(shí)現(xiàn)底部導(dǎo)航

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI