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

import 'package:flutter/material.dart';
class appbardemopage extends statelesswidget {
const appbardemopage({key key}) : super(key: key);
@override
widget build(buildcontext context) {
return defaulttabcontroller(
//导航栏的长度
length: 4,
child: scaffold(
appbar: appbar(
title: text("appbardemopage"),
backgroundcolor: colors.red,
centertitle: true,
bottom: tabbar(
// isscrollable: true, //可滚动
indicatorcolor: colors.bluegrey, //指示器的颜色
labelcolor: colors.bluegrey, //选中文字颜色
unselectedlabelcolor: colors.white, //为选中文字颜色
// indicatorsize: tabbarindicatorsize.label, //指示器与文字等宽
tabs: <widget>[
tab(text: "热门"),
tab(text: "推荐"),
tab(text: "好友"),
tab(text: "动态"),
],
),
),
body: tabbarview(
children: <widget>[
container(
child: text("hello"),
),
listview(
children: <widget>[
listtile(
title: text("第二个tab"),
)
],
),
listview(
children: <widget>[
listtile(
title: text("第三个tab"),
)
],
),
listview(
children: <widget>[
listtile(
title: text("第四个tab"),
)
],
),
],
),
),
);
}
}如果底部导航栏和顶部导航栏同时存在的

在这里只写顶部导航栏的实现,底部的可以参照我之前的文章
import 'package:flutter/material.dart';
class categorypage extends statefulwidget {
categorypage({key key}) : super(key: key);
@override
_categorypagestate createstate() => _categorypagestate();
}
class _categorypagestate extends state<categorypage> {
@override
widget build(buildcontext context) {
return defaulttabcontroller(
length: 4,
child: scaffold(
appbar: appbar(
title: row(
children: <widget>[
expanded(
child: tabbar(
tabs: <widget>[
tab(text: "精选"),
tab(text: "电影"),
tab(text: "动漫"),
tab(text: "nba"),
],
),
)
],
),
),
body: tabbarview(
children: <widget>[
listview(
children: <widget>[
listtile(
title: text("第一个tab"),
)
],
),
listview(
children: <widget>[
listtile(
title: text("第二个tab"),
)
],
),
listview(
children: <widget>[
listtile(
title: text("第三个tab"),
)
],
),
listview(
children: <widget>[
listtile(
title: text("第四个tab"),
)
],
),
],
),
),
);
}
}这么写是对导航栏点击做的监听,实现效果一样
import 'package:flutter/material.dart';
class navbarpage extends statefulwidget {
navbarpage({key key}) : super(key: key);
@override
_navbarpagestate createstate() => _navbarpagestate();
}
class _navbarpagestate extends state<navbarpage>
with singletickerproviderstatemixin {
tabcontroller _tabcontroller;
@override
void initstate() {
super.initstate(); //length为导航栏的个数
_tabcontroller = new tabcontroller(vsync: this, length: 2);
_tabcontroller.addlistener(() {
print(_tabcontroller.index);//打印点击的索引
});
}
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(
title: text("navbar"),
bottom: tabbar(
controller: this._tabcontroller,
tabs: <widget>[
tab(text: "热销"),
tab(text: "推荐"),
],
),
),
body: tabbarview(
controller: this._tabcontroller,
children: <widget>[
center(child: text("热销")),
center(child: text("推荐"))
],
));
}
}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
看完文章,还可以扫描下面的二维码下载快手极速版领4元红包
除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。
邀请两个好友奖最高196元,如下图所示:







