3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TabBarのTabを動的に増やす

Last updated at Posted at 2020-05-10

きっかけ

Tabの数をイベントにより動的に増やしたい場合はどうしたら良いのか実装中に疑問が生じたので、調べました。

結論

イベントごとに、Tabのサイズを増やしたTabControllerを生成し、コントローラの状態を更新する。

実装

動作

  • add Tabボタンを押すとTabが追加される
  • remove Tabボタンを押すと最後尾のTabが削除される。

画像

dynamic_tab.gif

コード

main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  TabController _tabController;

  List<String> _titles = [
    "tab",
    "tab",
    "tab",
    "tab",
  ];

  bool _isStop = false;

  void _addTab() {
    setState(() {
      _titles.add("additional tab");
      _tabController = _createNewTabController();
    });
  }

  void _removeTab() {
    setState(() {
      _titles.removeLast();
      _tabController = _createNewTabController();
    });
  }

  TabController _createNewTabController() => TabController(
        vsync: this,
        length: _titles.length,
      );

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(vsync: this, length: _titles.length);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        bottom: TabBar(
          controller: _tabController,
          isScrollable: true,
          tabs: _titles
              .map(
                (t) => Tab(
                  text: t,
                ),
              )
              .toList(),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              color: Colors.blue,
              onPressed: () {
                _addTab();
              },
              child: Text(
                "add tab",
                style: TextStyle(color: Colors.white),
              ),
            ),
            RaisedButton(
              color: Colors.blue,
              onPressed: () {
                print('pressed');
                setState(() {
                  _removeTab();
                });
              },
              child: Text(
                "remove tab",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?