import 'package:flutter/material.dart';
class TabControllerPage extends StatefulWidget {
TabControllerPage({Key key}) : super(key: key);
_TabControllerPageState createState() => _TabControllerPageState();
}
//用with来实现
class _TabControllerPageState extends State<TabControllerPage>
with SingleTickerProviderStateMixin {
TabController _tabController;
@override
//初始化状态
void initState() {
// TODO: implement initState
super.initState();
_tabController = new TabController(vsync: this, length: 2);
//监听索引变化
_tabController.addListener(() {
print(_tabController.index);
});
}
//生命周期函数,组件销毁时调用
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_tabController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("TabControllerPage"),
bottom: TabBar(
controller: this._tabController,
tabs: <Widget>[
Tab(text: "热销"),
Tab(text: "推荐"),
],
),
),
body: TabBarView(
controller: this._tabController,
children: <Widget>[
Center(child: Text("热销")),
Center(child: Text("推荐")),
],
),
);
}
}
实现效果: