12
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?

More than 1 year has passed since last update.

[Flutter] Appbarを使用して、GoogleMap風なUIを作ろう

Last updated at Posted at 2023-06-20

スクリーンショット 2023-06-20 9.22.18.png

こんにちわ〜いせりゅーです。

最近、仕事がうまく行って結構幸せに働けています。いいね。

プロフィール

スクリーンショット 2023-06-13 7.33.21(2).png

本題 Appbarを使用して、GoogleMap風なUIを作ろう

まずは、アプリの画面を載せておきます。

背景

  • GoogleのMap風なUIにしたかった。
  • Appbarを載せるよりも写真の右側に戻るボタンを作りたかった。

コード詳細

簡単に解説すると

  • Stackを使用して、画像とAppBarを重ねる
  • AppBarで使用するものは戻るアイコンのみでいい。
  • AppBarのカラーをColors.transparentにする。

注意

  • 正直なところAppbarを使用しなくても実装はできます。
  • もしかしたらパフォーマンス関連で影響が出てしまうかもしれません。
  • ただ、今回はたった1行で実装できる便利さを取りました。

↓コードはこちら

Stack(
  children: [
    Container(
      width: MediaQuery.of(context).size.width,
      height: 300,
      color: Colors.grey.shade300,
      child: Image.network(
        'https://assets.renoveru.jp/journal/wp-content/uploads/2021/11/10125410/16187_01.jpg',
        fit: BoxFit.cover,
      ),
    ),
    AppBar(backgroundColor: Colors.transparent),
  ],
),

ソースコード

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _incrementCounter() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const AppBarScreen(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class AppBarScreen extends StatelessWidget {
  const AppBarScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                height: 300,
                color: Colors.grey.shade300,
                child: Image.network(
                  'https://assets.renoveru.jp/journal/wp-content/uploads/2021/11/10125410/16187_01.jpg',
                  fit: BoxFit.cover,
                ),
              ),
              AppBar(backgroundColor: Colors.transparent),
            ],
          ),
          const Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 10,
            ),
            child: Text(
              '名前:〇〇〇〇ホテル',
              textAlign: TextAlign.start,
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 10,
            ),
            child: Text(
              '評価:4.5/5',
              textAlign: TextAlign.start,
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 10,
            ),
            child: Text(
              '説明:とても綺麗でおすすめの場所',
              textAlign: TextAlign.start,
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

最後に

天気が不安定ですが、頑張っていきましょうぅぅぅ

12
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
12
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?