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】ホームタブ画面を作る(グラデーション・ショートカット)

0
Posted at

買い物リスト以外の機能も今後の開発に向けてホーム画面を作成

  1. ログインユーザー名で挨拶する
  2. AppGradients.home で背景をグラデーションにする
  3. 買い物リストへのショートカットカードを置く

ログインユーザー名の挨拶

Firebase Auth の displayName を使います。未設定の場合はゲストにします。

final displayName =
    FirebaseAuth.instance.currentUser?.displayName ?? 'ゲスト';
Text(
  'こんにちは、$displayName さん',
  style: textTheme.headlineSmall?.copyWith(
    color: Colors.white,
    fontWeight: FontWeight.bold,
  ),
)

新規登録時に updateDisplayName しているので、ホーム画面では名前をそのまま表示できます。

グラデーション背景

色はテーマ用ファイルに切り出しています。

// theme/app_colors.dart
class AppGradients {
  static const home = LinearGradient(
    colors: [Color(0xFFFFCC80), Color(0xFFFF8A65)],
    begin: Alignment.topCenter,
    end: Alignment.bottomCenter,
  );
}

ScaffoldのbodyをContainerで包み、BoxDecorationに渡します。

return Scaffold(
  body: Container(
    decoration: const BoxDecoration(
      gradient: AppGradients.home,
    ),
    child: SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 挨拶・カード
          ],
        ),
      ),
    ),
  ),
);

SafeArea を入れると、ノッチやステータスバーに文字が被りません。

買い物リストへのショートカットカード

Card + ListTile でタップしやすいショートカットにしています。処理は親(HomePage)に VoidCallback で渡します。

class HomeTabPage extends StatelessWidget {
  final VoidCallback? onOpenShopping;
  const HomeTabPage({
    super.key,
    this.onOpenShopping,
  });
}
Card(
  elevation: 2,
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(16),
  ),
  child: ListTile(
    leading: Icon(
      Icons.shopping_cart_outlined,
      color: colorScheme.primary,
    ),
    title: const Text(
      '買い物リスト',
      style: TextStyle(fontWeight: FontWeight.bold),
    ),
    subtitle: const Text('共有リストを確認・編集'),
    trailing: const Icon(Icons.chevron_right),
    onTap: onOpenShopping,
  ),
)

親側では、買い物タブ(index = 1)に切り替えます。

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

ホーム画面は UI だけ、画面切り替えは親が担当する形です。

まとめ

ホームタブは StatelessWidget のまま、挨拶・背景・ショートカットの3点で構成できます。画面遷移の責務は親に渡し、ホーム画面は表示に集中させるとシンプルです。

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?