20Flutter通過TabController定義頂部tab切換,介紹生命周期函數


基本使用:

import 'package:flutter/material.dart';

class TabBarControllerPage extends StatefulWidget {
  TabBarControllerPage({Key key}) : super(key: key);

  _TabBarControllerPageState createState() => _TabBarControllerPageState();
}

class _TabBarControllerPageState extends State<TabBarControllerPage> with SingleTickerProviderStateMixin{
  TabController _tabController;
  @override
  void dispose(){ //生命周期函數:
  super.dispose();
  _tabController.dispose();
  }
  @override
  void initState(){  //生命周期函數:
    super.initState();
    _tabController=new TabController(
      vsync: this,
      length: 2
    );
    _tabController.addListener((){
      print(_tabController.index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TabBarControllerPage'),
        bottom: TabBar(
          controller:this._tabController,
          tabs: <Widget>[
            Tab(text: '熱銷'),
            Tab(text: '推薦')
          ],
        ),
      ),
      body: TabBarView(
        controller: this._tabController,
        children: <Widget>[
          Center(child: Text('熱銷')),
          Center(child: Text('推薦'))
        ],
      )

    );
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM