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 + IndexedStack でタブ切り替えと状態保持

0
Posted at

個人で使用する買い物リストアプリの続きです。

やりたいこと

  • 下部タブでホームと買い物を切り替える
  • ホームのカードタップでも買い物タブへ遷移する
  • タブを切り替えても、買い物リストのフィルター・カテゴリタブ・スクロール位置を残す
    if で画面を差し替えると Widget が破棄されるため、IndexedStack を使います。

BottomNavigationBar で2タブ構成

class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  void _onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: [
          HomeTabPage(
            onOpenShopping: () => _onTabTapped(1),
          ),
          const ShoppingPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: _onTabTapped,
        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: '買い物',
          ),
        ],
      ),
    );
  }
}

activeIcon を指定すると、選択中だけ塗りつぶしアイコンにできます。

_currentIndex で画面切り替え

タブの選択状態は_currentIndexで管理します。onTapで更新すると、IndexedStackの表示とBottomNavigationBarの選択が同時に変わります。

void _onTabTapped(int index) {
  setState(() {
    _currentIndex = index;
  });
}

ホーム画面のカードタップも、同じメソッドで買い物タブへ遷移します。

HomeTabPage(
  onOpenShopping: () => _onTabTapped(1),
)

下部タブもカードも、切り替え処理は親のHomePageに集約しています。

IndexedStack で状態を保持する

よくある書き方はこうです。

body: _currentIndex == 0
    ? const HomeTabPage()
    : const ShoppingPage(),

この場合、非表示になった側の Widget は破棄されます。買い物リストに戻ると、フィルターやスクロール位置がリセットされます。

IndexedStack は 全画面をツリーに残したまま、表示する index だけ切り替える Widget です。

IndexedStack(
  index: _currentIndex,
  children: [
    HomeTabPage(onOpenShopping: () => _onTabTapped(1)),
    const ShoppingPage(),
  ],
)

これで以下が残ります。

  • フィルター(全て / 未購入 / 購入済)
  • カテゴリタブ(食品 / 日用品)
  • リストのスクロール位置

ShoppingPage は StatefulWidget なので、破棄されなければ TabController や filter もそのままです。

ポイント

  • タブ状態は _currentIndex の1つで管理する
  • 下部タブとカードタップは同じ _onTabTapped を使う
  • 状態を残したいなら IndexedStack、破棄してよいなら if

まとめ

BottomNavigationBar でタブを切り替え、IndexedStack で各画面を破棄しない。これだけで「戻ったら状態が消える」問題を避けられます。

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?