FlutterのAppBar

Flutter/Dart

この記事では、FlutterのAppBarについて書いていきます。

AppBarとは

アプリバーは画面上部に表示される画面タイトルや検索アイコン・メニュー・戻るボタンなどのナビゲーションを担っています。

よく使用されるプロパティ

プロパティ名説明
titleWidget?タイトル部分に表示するウィジェット(主に Text ウィジェット)。
leadingWidget?左端(または標準の位置)に表示するウィジェット(戻るボタンやメニューアイコンなど)。
actionsList<Widget>?右端に並べるアクションウィジェットのリスト(検索や設定アイコンなど)。
bottomPreferredSizeWidget?AppBarの最下部に表示するウィジェット(主に TabBar など)。
flexibleSpaceWidget?AppBarの背景領域に配置するウィジェット(背景画像やグラデーションなど)。
backgroundColorColor?AppBarの背景色。
foregroundColorColor?アイコンやテキストのデフォルト表示色。
elevationdouble?AppBarの影の深さ(影の濃さ)。
shadowColorColor?elevation によって投影される影の色。
centerTitlebool?タイトルを中央揃えにするかどうか(true で中央寄せ)。
titleSpacingdouble?leading と title の間の余白サイズ。
automaticallyImplyLeadingboolleading が未指定の場合、自動で戻るボタンやドロワーアイコンを表示するかどうか(デフォルトは true)。
iconThemeIconThemeData?AppBar内のアイコンスタイル(色やサイズなど)を一括指定。
actionsIconThemeIconThemeData?actions エリア内専用のアイコンスタイル指定。
titleTextStyleTextStyle?title ウィジェットに適用するテキストスタイル。
toolbarHeightdouble?ツールバー本体の高さ(デフォルトは kToolbarHeight = 56.0)。
toolbarOpacitydoubleツールバー要素の不透明度(0.0 〜 1.0)。
bottomOpacitydoublebottom ウィジェットの不透明度(0.0 〜 1.0)。
systemOverlayStyleSystemUiOverlayStyle?ステータスバー(画面最上部のバッテリーや時間等)の表示スタイル指定。

サンプル

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const CustomAppBarExample(),
    );
  }
}

class CustomAppBarExample extends StatelessWidget {
  const CustomAppBarExample({super.key});

  @override
  Widget build(BuildContext context) {
    // TabBar を使うために DefaultTabController で囲みます
    return DefaultTabController(
      length: 2, // タブの数
      child: Scaffold(
        appBar: AppBar(
          // 1. 自動戻るボタンを無効化(明示的に leading をカスタムするため)
          automaticallyImplyLeading: false,

          // 2. 左端のウィジェット(ドロワーや戻るボタンなど)
          leading: IconButton(
            icon: const Icon(Icons.menu),
            tooltip: 'メニュー',
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('メニューがタップされました')),
              );
            },
          ),

          // 3. メインタイトル
          title: const Text(
            'AppBar デモ',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 20,
            ),
          ),

          // 4. タイトルの中央揃え(iOS風レイアウト)
          centerTitle: true,

          // 5. テキストやアイコンの基本カラー
          foregroundColor: Colors.white,

          // 6. 右端のアクションボタン群
          actions: [
            IconButton(
              icon: const Icon(Icons.search),
              tooltip: '検索',
              onPressed: () {},
            ),
            IconButton(
              icon: const Icon(Icons.notifications),
              tooltip: '通知',
              onPressed: () {},
            ),
            PopupMenuButton<String>(
              onSelected: (value) {},
              itemBuilder: (BuildContext context) => [
                const PopupMenuItem(value: 'setting', child: Text('設定')),
                const PopupMenuItem(value: 'help', child: Text('ヘルプ')),
              ],
            ),
          ],

          // 7. アプリバー背面のカスタムデザイン(グラデーション背景)
          flexibleSpace: Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.blue, Colors.indigo],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
          ),

          // 8. 影・立体感の設定
          elevation: 4.0,
          shadowColor: Colors.black54,

          // 9. 下部に固定表示する TabBar
          bottom: const TabBar(
            indicatorColor: Colors.amber,
            indicatorWeight: 3.0,
            labelColor: Colors.amber,
            unselectedLabelColor: Colors.white70,
            tabs: [
              Tab(icon: Icon(Icons.home), text: 'ホーム'),
              Tab(icon: Icon(Icons.person), text: 'マイページ'),
            ],
          ),
        ),
        body: const TabBarView(
          children: [
            Center(child: Text('ホーム画面のコンテンツ')),
            Center(child: Text('マイページ画面のコンテンツ')),
          ],
        ),
      ),
    );
  }
}
タイトルとURLをコピーしました