很少逼逼緩存
先看Tabbar的參數bash
const TabBar({
Key key,
@required this.tabs, // item
this.controller, //控制器
this.isScrollable = false, //是否能夠滑動
this.indicatorColor, //指示器的顏色
this.indicatorWeight = 2.0,
this.indicatorPadding = EdgeInsets.zero,
this.indicator,//自定義指示器
this.indicatorSize,
this.labelColor, // 選中文字顏色
this.labelStyle, //
this.labelPadding, // 文字的內邊距
this.unselectedLabelColor, // 未選中文字顏色
this.unselectedLabelStyle,
this.dragStartBehavior = DragStartBehavior.start,
this.onTap,// 點擊事件
})
複製代碼
const TabBarView({
Key key,
@required this.children, // 每頁的view
this.controller, // 控制器
this.physics,
this.dragStartBehavior = DragStartBehavior.start,
})
複製代碼
好了,這兩個必須關聯起來用. 否則會報錯app
這個是一個無狀態的控制器,很簡單.可是要作成動態Tabbar的話,不能用這個.async
也很簡單.ide
TabController({ int initialIndex = 0, @required this.length, @required TickerProvider vsync })
複製代碼
主要是這個TickerProvider
.做用是獲取每一幀刷新的通知.工具
直接 實現TickerProviderStateMixin就行了.ui
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin{
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(length: _spList.length, vsync: this);// 直接傳this
}
}
複製代碼
SharedPreferences
這裏我使用了本地緩存來作的this
添加插件spa
寫個工具插件
class SpUtils {
static Future<List<String>> getSpList(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getStringList(key) ?? [];
}
static setSpList(String key, List<String> list) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList(key, list);
}
}
複製代碼
這樣使用就簡單不少了
import 'package:flutter/material.dart';
import '../util/SpUtils.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
var _bottomIndex = 0;
var _spList = [];
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(length: _spList.length, vsync: this);
}
@override
Widget build(BuildContext context) {
SpUtils.getSpList('tabmenu').then((s) {
if (_spList == s) {
return;
}
setState(() {
_spList = s;
_tabController = new TabController(length: _spList.length, vsync: this);
});
});
return Scaffold(
appBar: new AppBar(
titleSpacing: 0,
title: Center(
child: new TabBar(
controller: _tabController,
isScrollable: true,
labelColor: Colors.white,
tabs: _spList.map((f) {
return Center(
child: new Text(f),
);
}).toList(),
),
),
actions: <Widget>[
new Container(
child: new Icon(Icons.arrow_drop_down),
width: 48,
)
],
),
body: TabBarView(
controller: _tabController,
children: _spList.isEmpty
? []
: _spList.map((f) {
return Center(
child: new Text(f),
);
}).toList()),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
bottomNavigationBar: new BottomNavigationBar(
items: [
new BottomNavigationBarItem(
icon: Icon(Icons.home),
title: new Container(),
),
new BottomNavigationBarItem(
icon: Icon(Icons.whatshot),
title: new Container(),
),
new BottomNavigationBarItem(
icon: Icon(Icons.search),
title: new Container(),
),
new BottomNavigationBarItem(
icon: Icon(Icons.book),
title: new Container(),
),
new BottomNavigationBarItem(
icon: Icon(Icons.person),
title: new Container(),
),
],
currentIndex: _bottomIndex,
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.white,
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.grey,
onTap: (i) {
setState(() {
_bottomIndex = i;
});
},
),
);
}
}
複製代碼
SpUtils.getSpList('tabmenu').then((s) { //獲取緩存
if (_spList == s) {//防止遞歸刷新
return;
}
setState(() {
_spList = s;
_tabController = new TabController(length: _spList.length, vsync: this);
});
});
複製代碼
TabBar須要使用center包裝.否則默認高度是不會佔滿的,固然也能夠用其它辦法
isScrollable
須要設置爲true.才能滑動.
appBar: new AppBar(
titleSpacing: 0,
title: Center(
child: new TabBar(
controller: _tabController,
isScrollable: true,
labelColor: Colors.white,
tabs: _spList.map((f) {
return Center(
child: new Text(f),
);
}).toList(),
),
),
actions: <Widget>[
new Container(
child: new Icon(Icons.arrow_drop_down),
width: 48,
)
],
),
複製代碼
很簡單.只要更新SharedPreferences的緩存值.就能夠了.像 掘金的效果是跨頁面開關的.因此使用SharedPreferences的緩存來作
Flutter:782978118
Android:493180098