Flutter 自定义顶部导航按钮和TabBar定义顶部 Tab 切换
1.Flutter AppBar 自定义顶部按钮图标、颜色
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
leading: IconButton(
icon: Icon(Icons.menu), tooltip: "Search", onPressed: () {
print('menu Pressed');
}
), title: Text('FlutterDemo'), actions: <Widget>[
IconButton(
icon: Icon(Icons.search), tooltip: "Search", onPressed: () {
print('Search Pressed');
}
), IconButton(
icon: Icon(Icons.more_horiz), tooltip: "more_horiz", onPressed: () {
print('more_horiz Pressed');
}
)
],), body: Text('这是 Appbar'),);
2.AppBar 中自定义 TabBar 实现顶部 Tab 切换,并把把 TabBar 放在导航最顶部
return DefaultTabController(
length: 2, //列数
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.tealAccent,
title: Row(
children: [
Expanded(
child: TabBar(
isScrollable: true,//多个菜单可以滑动
indicatorColor: Colors.red,
labelColor: Colors.cyan,//选中颜色
unselectedLabelColor: Colors.white,//未选中颜色
indicatorSize: TabBarIndicatorSize.label,
tabs: [
Tab(
text: '热销',
),
Tab(
text: '推荐',
),
],
),
)
],
),
),
body: TabBarView(
children: [
ListView(
children: [
ListTile(
title: Text('第一个Tab'),
),
],
),
ListView(
children: [
ListTile(
title: Text('第er个Tab'),
),
],
),
],
),
));
3.AppBar 中自定义 TabBar 实现 Tabs 的另一种方法
定义TapBar和上面的方法差不多,唯一要注意的就是_tabController
import 'package:flutter/material.dart';
class TapBarContollerPage extends StatefulWidget {
//AppBardemoPage({Key key}) : super(key: key);
_TapBarContollerPageState createState() => _TapBarContollerPageState();
}
class _TapBarContollerPageState extends State<TapBarContollerPage>
with SingleTickerProviderStateMixin { //还要继承SingleTickerProviderStateMixin
late TabController _tabController;
@override
void dispose() { //生命周期函数(销毁)
_tabController.dispose();
super.dispose();
}
void initState() { //生命周期函数
super.initState();
_tabController = new TabController(
vsync: this, //固定写法
length: 3 //长度
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('顶部 tab 切换'),
bottom: new TabBar(
tabs: <Widget>[
new Tab(
icon: new Icon(Icons.directions_bike),
),
new Tab(
icon: new Icon(Icons.directions_boat),
),
new Tab(
icon: new Icon(Icons.directions_bus),
),
],
controller: _tabController, //注意
),
),
body: new TabBarView(
controller: _tabController, //注意
children: <Widget>[
new Center(child: new Text('自行车')),
new Center(child: new Text('船')),
new Center(child: new Text('巴士')),
],
),
);
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgakiba
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01