AppBarの外観をカスタマイズする(画像設置や背景にグラデーション)|flutter
AppBarウィジェットのflexibleSpaceプロパティを利用します。
下記は背景にグラデーションが適用されます。
AppBar(
title: Text('Flexible Space Demo'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.green],
),
),
),
);
flexibleSpaceに設定できるウィジェットは非常に多岐にわたります。
例えば、以下のようなものがあります:
- Container
- Image
- Stack (複数のウィジェットを重ねて配置する場合)
- ListView (スクロール可能な要素を配置する場合)
- 他のカスタムウィジェットや複合ウィジェット
これらを組み合わせることで、さまざまなデザインや挙動を持つAppBarを作成することができます。
AppBarに画像を入れたいことは多いかもしれません。
例えば下記のようなコーディングをご紹介します。
appBar: AppBar(
flexibleSpace: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/hoge.png'), // AppBar画像
fit: BoxFit.fitWidth, // 画像を全体に広げる
),
),
),

