debug狀態(tài)下可能有點卡,release情況下很完美
import 'dart:io';
import 'package:flutter/services.dart';
void main() {
runApp(new MyApp());
if (Platform.isAndroid) {
// 以下兩行 設(shè)置android狀態(tài)欄為透明的沉浸。寫在組件渲染之后,是為了在渲染后進行set賦值,覆蓋狀態(tài)欄,寫在渲染之前MaterialApp組件會覆蓋掉這個值。
SystemUiOverlayStyle systemUiOverlayStyle =
SystemUiOverlayStyle(statusBarColor: Colors.transparent);
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColorBrightness: Brightness.dark,
platform: TargetPlatform.iOS),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => HomeState();
}
class TabTitle {
String title;
int id;
TabTitle(this.title, this.id);
}
class HomeState extends State<MyHomePage> with SingleTickerProviderStateMixin {
TabController mTabController;
PageController mPageController = PageController(initialPage: 0);
List<TabTitle> tabList;
var currentPage = 0;
var isPageCanChanged = true;
@override
void initState() {
super.initState();
initTabData();
mTabController = TabController(
length: tabList.length,
vsync: this,
);
mTabController.addListener(() {//TabBar的監(jiān)聽
if (mTabController.indexIsChanging) {//判斷TabBar是否切換
print(mTabController.index);
onPageChange(mTabController.index, p: mPageController);
}
});
}
initTabData() {
tabList = [
new TabTitle('推薦', 10),
new TabTitle('熱點', 0),
new TabTitle('社會', 1),
new TabTitle('娛樂', 2),
new TabTitle('體育', 3),
new TabTitle('美文', 4),
new TabTitle('科技', 5),
new TabTitle('財經(jīng)', 6),
new TabTitle('時尚', 7)
];
}
onPageChange(int index, {PageController p, TabController t}) async {
if (p != null) {//判斷是哪一個切換
isPageCanChanged = false;
await mPageController.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.ease);//等待pageview切換完畢,再釋放pageivew監(jiān)聽
isPageCanChanged = true;
} else {
mTabController.animateTo(index);//切換Tabbar
}
}
@override
void dispose() {
super.dispose();
mTabController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("首頁"),
backgroundColor: Color(0xffd43d3d),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.all_inclusive),
backgroundColor: Color(0xffd43d3d),
elevation: 2.0,
highlightElevation: 2.0,
onPressed: () {},
),
body: Column(
children: <Widget>[
Container(
color: new Color(0xfff4f5f6),
height: 38.0,
child: TabBar(
isScrollable: true,
//是否可以滾動
controller: mTabController,
labelColor: Colors.red,
unselectedLabelColor: Color(0xff666666),
labelStyle: TextStyle(fontSize: 16.0),
tabs: tabList.map((item) {
return Tab(
text: item.title,
);
}).toList(),
),
),
Expanded(
child: PageView.builder(
itemCount: tabList.length,
onPageChanged: (index) {
if (isPageCanChanged) {//由于pageview切換是會回調(diào)這個方法,又會觸發(fā)切換tabbar的操作,所以定義一個flag,控制pageview的回調(diào)
onPageChange(index);
}
},
controller: mPageController,
itemBuilder: (BuildContext context, int index) {
return Text(tabList[index].title);
},
),
)
],
),
);
}
}