服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Flutter实现底部导航栏

Flutter实现底部导航栏

2022-09-24 18:17夏沫凡尘 Android

这篇文章主要为大家详细介绍了Flutter实现底部导航栏的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Flutter实现底部导航栏的具体代码,供大家参考,具体内容如下

效果

Flutter实现底部导航栏

Flutter实现底部导航栏

实现

先将自动生成的main.dart里面的代码删除,

?
1
2
3
4
5
6
import 'package:flutter/material.dart';
import 'package:flutter_guohe/pages/main.dart';
 
void main() {
 runApp(new Guohe());
}

创建app.dart作为首页的页面文件

?
1
2
3
4
5
6
7
8
9
10
11
class Guohe extends StatefulWidget {
 @override
 GuoheState createState() => new GuoheState();
}
 
class GuoheState extends State<Guohe> {
 @override
 Widget build(BuildContext context) {
 
 }
}

创建today.dart、kb.dart、playground.dart三个页面文件作为tabview的填充文件,这里用playground.dart为例。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import 'package:flutter/material.dart';
 
class Playground extends StatefulWidget {
 @override
 PlaygroundState createState() => new PlaygroundState();
}
 
class PlaygroundState extends State<Playground> {
 @override
 Widget build(BuildContext context) {
 return new MaterialApp(
  home: new Scaffold(
  appBar: new AppBar(
   title: new Text("操场"),
   backgroundColor: Color.fromARGB(255, 119, 136, 213), //设置appbar背景颜色
   centerTitle: true, //设置标题是否局中
  ),
  body: new Center(
   child: new Text('操场'),
  ),
  ),
 );
 }
}

app.dart的完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * APP的主入口文件
 */
 
import 'package:flutter/material.dart';
 
import 'package:flutter_guohe/pages/main/today.dart';
import 'package:flutter_guohe/pages/main/playground.dart';
import 'package:flutter_guohe/pages/main/kb.dart';
import 'package:flutter_guohe/pages/main/leftmenu.dart';
 
import 'package:flutter_guohe/common/eventBus.dart';
 
//果核的主界面
class Guohe extends StatefulWidget {
 @override
 GuoheState createState() => new GuoheState();
}
 
class GuoheState extends State<Guohe> with SingleTickerProviderStateMixin {
 TabController controller;
 
 @override
 void initState() {
 controller = new TabController(length: 3, vsync: this);
 }
 
 @override
 void dispose() {
 controller.dispose();
 super.dispose();
 }
 
 @override
 Widget build(BuildContext context) {
 return new MaterialApp(
  home: new Scaffold(
  drawer: new LeftMenu(),
  body: new TabBarView(
   controller: controller,
   children: <Widget>[
   new Today(),
   new Kb(),
   new Playground(),
   ],
  ),
  bottomNavigationBar: new Material(
   color: Colors.white,
   child: new TabBar(
   controller: controller,
   labelColor: Colors.deepPurpleAccent,
   unselectedLabelColor: Colors.black26,
   tabs: <Widget>[
    new Tab(
    text: "今日",
    icon: new Icon(Icons.brightness_5),
    ),
    new Tab(
    text: "课表",
    icon: new Icon(Icons.map),
    ),
    new Tab(
    text: "操场",
    icon: new Icon(Icons.directions_run),
    ),
   ],
   ),
  ),
  ),
 );
 }
}

其中

?
1
2
labelColor: Colors.deepPurpleAccent,
unselectedLabelColor: Colors.black26,

第一个属性是控制标签颜色,这里我选了紫色,第二个属性是未选中标签时的颜色。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_33419925/article/details/84987282

延伸 · 阅读

精彩推荐