代码:
import 'package:flutter/material.dart';
class KeepAliveDemo extends StatefulWidget {
@override
_KeepAliveDemoState createState() => _KeepAliveDemoState();
}
//with类似多重继承
class _KeepAliveDemoState extends State<KeepAliveDemo> with SingleTickerProviderStateMixin {
TabController _controller;
@override
void initState(){
super.initState();
_controller = TabController(length: 3,vsync: this);//vsync 垂直
}
@override
void dispose(){
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('keepAlive'),
elevation: 8.0,
bottom: TabBar(
controller: _controller,
tabs: <Widget>[
Tab(icon: Icon(Icons.directions_bike)),
Tab(icon: Icon(Icons.directions_boat)),
Tab(icon: Icon(Icons.directions_car)),
],
),
),
body: TabBarView(
controller: _controller,
children: <Widget>[
Text('111'),
Text('222'),
Text('333')
],
),
);
}
}
总结:
导航栏下嵌套tabController
final TabController _controller
多重继承 用with 关键字
初始化
_controller = TabController(length:xx,vsync:this)//vsync 继承于 SingleTickerProviderStateMixin 垂直布局
flutter 的tabController 是个模型 里面可以直接放数据
appBar 下 放 tabController
appBar(
bottom:Tabbar(
controller : _controller
tabs:<widget>[//次数个数要和上面的length 个数对应
Tab(icon:xxx)//
Tab(icon:xxx)//
Tab(icon:xxx)//
]
)
)