flutter TabBar+TabView的使用

第一步 添加數(shù)據(jù)

List<TabTitle> 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)
];

第二步 完成布局

class HomeState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  TabController mController;
  List<TabTitle> tabList;
  @override
  void initState() {
    super.initState();
    initTabData();
    mController = TabController(
      length: tabList.length,
      vsync: this,
    );
  }
  
  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }
  
  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)
    ];
  }
  
  @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: mController,
              labelColor: Colors.red,
              unselectedLabelColor: Color(0xff666666),
              labelStyle: TextStyle(fontSize: 16.0),
              tabs: tabList.map((item) {
                return Tab(
                  text: item.title,
                );
              }).toList(),
            ),
          ),
          Expanded(
            child: TabBarView(
              controller: mController,
              children: tabList.map((item) {
                return Stack(children: <Widget>[
                  Align(alignment:Alignment.topCenter,child: Text(item.title),),
                ],);
              }).toList(),
            ),
          )
        ],
      ),
    );
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么事聽多了就煩
    啦啦啦cfy閱讀 165評論 3 0
  • 何必呢 何必去假裝 餓了就吃 困了便睡 你是獨立的個體 抱歉 抱歉 我不是情感專家 并不會理性分析 或是軟言細(xì)雨安...
    scumalapert閱讀 336評論 6 4
  • 生活告訴我們,唯一能證明做事兒的就是結(jié)果。所以,一切表象和外在,都在行為舉止中。
    呂明超閱讀 163評論 0 0
  • 要盡可能用簡單的的詞匯描述自己的想法,要向身邊的人說,像新人說,看別人是否能聽懂。還有就是洞察力,先是用戶面臨那些...
    倪希嘎閱讀 608評論 0 0

友情鏈接更多精彩內(nèi)容