網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)頭部tabTop滾動欄的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:
main.dart代碼如下:
import 'package:flutter/material.dart'; //啟動函數(shù) void main() => runApp(MyApp()); //自定義組件 class MyApp extends StatelessWidget { ? @override ? Widget build(BuildContext context) { ? ? //MaterialApp 是flutter的頁面根組件 ? ? return MaterialApp( ? ? ? title: 'main根頁面', ? ? ? debugShowCheckedModeBanner: false, ?//清除debug ? ? ? //home表示頁面信息 ? ? ? home: AppbarTop() ? ? ); ? } } //頭部tabTop滾動欄效果組件 import 'package:flutter/material.dart'; class AppbarTop extends StatefulWidget { ? @override ? _AppbarTopState createState() => _AppbarTopState(); } //混合SingleTickerProviderStateMixin類 同步屬性 class _AppbarTopState extends State? ? with SingleTickerProviderStateMixin { ? //定義一個控制器 ? TabController _tabController; ? @override ? void initState() { ? ? super.initState(); ? ? //混入SingleTickerProviderStateMixin的this ? ? //實(shí)例化一個tab控制器 作用: ? ? _tabController = TabController(length: choices.length, vsync: this); ? } ? _nextPage(index) { ? ? int currentIndex = _tabController.index + index; ? ? if (currentIndex < -0) currentIndex = _tabController.length - 1; ? ? if (currentIndex >= _tabController.length) currentIndex = 0; ? ? //控制器移動到currentIndex處 ? ? _tabController.animateTo(currentIndex); ? } ? @override ? Widget build(BuildContext context) { ? ? return Scaffold( ? ? ? appBar: AppBar( ? ? ? ? title: Text('頭部菜單欄'), ? ? ? ? centerTitle: true, //title居中顯示 ? ? ? ? leading: IconButton( ? ? ? ? ? ? icon: Icon(Icons.arrow_back), ? ? ? ? ? ? onPressed: () { ? ? ? ? ? ? ? _nextPage(-1); ? ? ? ? ? ? }), ? ? ? ? iconTheme: IconThemeData(color: Colors.yellow), //頭部icon樣式顏色 ? ? ? ? //右邊icon圖標(biāo) 可以多個 ? ? ? ? actions: [ ? ? ? ? ? IconButton( ? ? ? ? ? ? ? icon: Icon(Icons.arrow_forward), ? ? ? ? ? ? ? onPressed: () { ? ? ? ? ? ? ? ? _nextPage(1); ? ? ? ? ? ? ? }) ? ? ? ? ], ? ? ? ? actionsIconTheme: IconThemeData(color: Colors.white), ? ? ? ? //自定義導(dǎo)航欄 ? ? ? ? bottom: PreferredSize( ? ? ? ? ? ? child: Theme( ? ? ? ? ? ? ? data: Theme.of(context).copyWith(accentColor: Colors.white), ? ? ? ? ? ? ? child: Container( ? ? ? ? ? ? ? ? height: 40, ? ? ? ? ? ? ? ? alignment: Alignment.center, //圓點(diǎn)居中 ? ? ? ? ? ? ? ? //給自定義導(dǎo)航欄設(shè)置圓點(diǎn)控制器 ? ? ? ? ? ? ? ? child: TabPageSelector( ? ? ? ? ? ? ? ? ? controller: _tabController, ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ), ? ? ? ? ? ? ), ? ? ? ? ? ? preferredSize: Size.fromHeight(48)), ? ? ? ), ? ? ? //主體內(nèi)容 ? ? ? body: TabBarView( ? ? ? ? //主題內(nèi)容也跟隨控制器變化 ? ? ? ? controller: _tabController, ? ? ? ? //將數(shù)據(jù)遍歷成n個子組件數(shù)組 ? ? ? ? children: choices.map((item) { ? ? ? ? ? return Padding( ? ? ? ? ? ? padding: EdgeInsets.all(20), ? ? ? ? ? ? child: ChoiceCard( ? ? ? ? ? ? ? choice: item, ? ? ? ? ? ? ), ? ? ? ? ? ); ? ? ? ? }).toList(), ? ? ? ), ? ? ); ? } } //ChoiceCard將數(shù)據(jù)渲染到Card卡片組件上 class ChoiceCard extends StatelessWidget { ? final Choice choice; ? const ChoiceCard({Key key, this.choice}) : super(key: key); ? @override ? Widget build(BuildContext context) { ? ? //滾動時的卡片小部件 ? ? return Card( ? ? ? color: Colors.blue, ? ? ? child: Column( ? ? ? ? mainAxisAlignment: MainAxisAlignment.center, ? ? ? ? children: [ ? ? ? ? ? Icon(choice.icon, size: 120, color: Colors.white,), ? ? ? ? ? Text(choice.title), ? ? ? ? ], ? ? ? ) ? ? ); ? } } //數(shù)據(jù)的類型 class Choice { ? const Choice({this.title, this.icon}); ? final String title; ? final IconData icon; } //模擬的數(shù)據(jù) const List choices = const [ ? const Choice(title: 'CAR', icon: Icons.directions_car), ? const Choice(title: 'BICYCLE', icon: Icons.directions_bike), ? const Choice(title: 'BOAT', icon: Icons.directions_boat), ? const Choice(title: 'BUS', icon: Icons.directions_bus), ? const Choice(title: 'TRAIN', icon: Icons.directions_railway), ? const Choice(title: 'WALK', icon: Icons.directions_walk), ];
效果圖2:
代碼如下:
//頭部tabTop滾動欄效果組件 class AppBarBottom extends StatefulWidget { ? AppBarBottom({Key key}) : super(key: key); ? @override ? _AppBarBottomState createState() => _AppBarBottomState(); } class _AppBarBottomState extends State{ ? _SelectView(icon, text, id) { ? ? return PopupMenuItem( ? ? ? ? child: Row( ? ? ? mainAxisAlignment: MainAxisAlignment.spaceAround, ? ? ? children: [ ? ? ? ? Icon(icon, color: Colors.blue), ? ? ? ? Text( ? ? ? ? ? text, ? ? ? ? ? style: TextStyle(color: Colors.black), ? ? ? ? ) ? ? ? ], ? ? )); ? } ? @override ? Widget build(BuildContext context) { ? ? //tab使用這個組件 ? ? return DefaultTabController( ? ? ? ? length: choices.length, ? ? ? ? child: Scaffold( ? ? ? ? ? appBar: AppBar( ? ? ? ? ? ? title: Text('AppBar與TabBar'), ? ? ? ? ? ? centerTitle: true, ? ? ? ? ? ? actions: [ ? ? ? ? ? ? ? PopupMenuButton( ? ? ? ? ? ? ? ? ? shape: BeveledRectangleBorder( ? ? ? ? ? ? ? ? ? ? ? borderRadius: BorderRadius.circular(10)), ? ? ? ? ? ? ? ? ? itemBuilder: (BuildContext context) { ? ? ? ? ? ? ? ? ? ? return [ ? ? ? ? ? ? ? ? ? ? ? PopupMenuItem( ? ? ? ? ? ? ? ? ? ? ? ? ? child: _SelectView(Icons.message, '首頁', 'A')), ? ? ? ? ? ? ? ? ? ? ? PopupMenuItem( ? ? ? ? ? ? ? ? ? ? ? ? ? child: _SelectView(Icons.message, '商品', 'B')), ? ? ? ? ? ? ? ? ? ? ? PopupMenuItem( ? ? ? ? ? ? ? ? ? ? ? ? ? child: _SelectView(Icons.message, '消息', 'C')), ? ? ? ? ? ? ? ? ? ? ]; ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ], ? ? ? ? ? ? bottom: TabBar( ? ? ? ? ? ? ? isScrollable: true, ? ? ? ? ? ? ? indicatorSize: TabBarIndicatorSize.label, ? ? ? ? ? ? ? tabs: choices.map((item) { ? ? ? ? ? ? ? ? return Tab( ? ? ? ? ? ? ? ? ? text: item.title, ? ? ? ? ? ? ? ? ? icon: Icon(item.icon), ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? }).toList(), ? ? ? ? ? ? ), ? ? ? ? ? ), ? ? ? ? ? body: TabBarView( ? ? ? ? ? ? children: choices.map((item) { ? ? ? ? ? ? ? return Container( ? ? ? ? ? ? ? ? width: double.infinity, ? ? ? ? ? ? ? ? color: Colors.white70, ? ? ? ? ? ? ? ? child: Padding( ? ? ? ? ? ? ? ? ? padding: EdgeInsets.all(16), ? ? ? ? ? ? ? ? ? child: ChoiceCard( ? ? ? ? ? ? ? ? ? ? choice: item, ? ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ? ), ? ? ? ? ? ? ? ); ? ? ? ? ? ? }).toList(), ? ? ? ? ? ), ? ? ? ? )); ? } } //ChoiceCard將數(shù)據(jù)渲染到Card卡片組件上 class ChoiceCard extends StatelessWidget { ? final Choice choice; ? const ChoiceCard({Key key, this.choice}) : super(key: key); ? @override ? Widget build(BuildContext context) { ? ? //滾動時的卡片小部件 ? ? return Card( ? ? ? ? color: Colors.blue, ? ? ? ? child: Column( ? ? ? ? ? mainAxisAlignment: MainAxisAlignment.center, ? ? ? ? ? children: [ ? ? ? ? ? ? Icon( ? ? ? ? ? ? ? choice.icon, ? ? ? ? ? ? ? size: 120, ? ? ? ? ? ? ? color: Colors.white, ? ? ? ? ? ? ), ? ? ? ? ? ? Text(choice.title), ? ? ? ? ? ], ? ? ? ? )); ? } } //數(shù)據(jù)的類型 class Choice { ? const Choice({this.title, this.icon}); ? final String title; ? final IconData icon; } //模擬的數(shù)據(jù) const List choices = const [ ? const Choice(title: 'CAR', icon: Icons.directions_car), ? const Choice(title: 'BICYCLE', icon: Icons.directions_bike), ? const Choice(title: 'BOAT', icon: Icons.directions_boat), ? const Choice(title: 'BUS', icon: Icons.directions_bus), ? const Choice(title: 'TRAIN', icon: Icons.directions_railway), ? const Choice(title: 'WALK', icon: Icons.directions_walk), ];
原文鏈接:https://blog.csdn.net/qq_41614928/article/details/105334809
相關(guān)推薦
- 2022-08-23 python數(shù)據(jù)分析繪圖可視化_python
- 2024-03-14 SpringBoot中事務(wù)
- 2023-03-30 C/C++經(jīng)典楊輝三角問題解決方案_C 語言
- 2022-05-01 pytorch中的torch.nn.Conv2d()函數(shù)圖文詳解_python
- 2022-05-20 Spring-實(shí)現(xiàn)AOP的三種方式演示
- 2022-09-03 Go語言中的變量和常量_Golang
- 2022-06-10 python?PIL?Image?圖像處理基本操作實(shí)例_python
- 2021-12-16 C++?OpenCV實(shí)現(xiàn)圖像雙三次插值算法詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支