0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Flutter】BottomNavigationBar に3つ目のタブを足す

0
Posted at

個人で使用するアプリの続きです。

今回はホームと買い物のあとにTODOタブを足しています。

bottomNavigationBar: BottomNavigationBar(
  currentIndex: _currentIndex,
  onTap: _onTabTapped,
  type: BottomNavigationBarType.fixed,
  items: const [
    BottomNavigationBarItem(
      icon: Icon(Icons.home_outlined),
      activeIcon: Icon(Icons.home),
      label: 'ホーム',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.shopping_cart_outlined),
      activeIcon: Icon(Icons.shopping_cart),
      label: '買い物',
    ),
+    BottomNavigationBarItem(
+      icon: Icon(Icons.check_box_outlined),
+      activeIcon: Icon(Icons.check_box),
+      label: 'TODO',
+    ),
  ],
),

BottomNavigationBarType.fixedを指定すると、すべてのタブのラベルが常に表示されます。

shiftingでは選択中のタブのみラベルが表示されるため、すべてのラベルを表示したい場合はfixedを指定します。

今回のようにTODOタブを追加した場合、BottomNavigationBarだけでなく、IndexedStackのchildrenにも画面を追加する必要があります。

また、BottomNavigationBarのitemsとIndexedStackのchildrenは、同じ順番にする必要があります。

children: [
  HomeTabPage(onOpenShopping: () => _onTabTapped(1)),
  const ShoppingPage(),
+  const TodoPage(),
],

BottomNavigationBarにTODOタブを追加しました。

タブを追加する際は、BottomNavigationBarのitemsだけでなく、IndexedStackのchildrenにも対応する画面を追加する必要があります。

また、それぞれの項目の順番を一致させることで、選択したタブと表示する画面を正しく対応させることができます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?