Flutter 原生TabBar切换标签页示例

效果图:

代码如下:

import 'package:flutter/material.dart';

class TabsTestPage extends StatefulWidget {
  _TabsTestPageState createState() => _TabsTestPageState();
}

class _TabsTestPageState extends State<TabsTestPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 500.0,
      child: MyTabbedPage(),
    );
  }
}

class MyTabbedPage extends StatefulWidget {
  _MyTabbedPageState createState() => _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    Tab(text: 'LEFT'),
    Tab(text: 'RIGHT'),
  ];

  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: myTabs.length);
  }

 @override
 void dispose() {
   _tabController.dispose();
   super.dispose();
 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tabs'),
        bottom: TabBar(
          controller: _tabController,
          tabs: myTabs,
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: myTabs.map((Tab tab) {
          return Center(child: Text(tab.text));
        }).toList(),
      ),
    );
  }
}

 

posted on 2019-10-17 16:24  JoeYoung  阅读(1516)  评论(0编辑  收藏  举报