【 Flutter 】Flutter を 基礎 から 学習 ( Material ComponentsとiOS-Style ) part127 Material Components

基礎 から 学ぶ Flutter 」という書籍で  学習 したことを ブログでアウトプットしていこうと思います。今回は Material ComponentsとiOS-Style ( part127 )です。

前回

【 Flutter 】Flutter を 基礎 から 学習 ( Material ComponentsとiOS-Style ) part126 Material Components

引き続き、Material Componentsについて学びます。

Drawer

マテリアルデザインのDrawerです。
Drawerとは何か?以下のサイトより、画像をお借りしました。

【WPF】MaterialDesign の Drawer を試す | のい太ろぐ (noitalog.tokyo)

 

とても分かりやすいですね!

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: new MyHomePage()));
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  bool dark = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: dark ? ThemeData.dark() : ThemeData.light(),
      home: Scaffold(
        // Drawerはたったこれだけです!
        drawer: Drawer(
          child: ListView(
            children: <Widget>[
              DrawerHeader(
                child: Text('ヘッダー'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text("ボタン1"),
                trailing: Icon(Icons.arrow_forward),
              ),
              ListTile(
                title: Text("ボタン2"),
                trailing: Icon(Icons.arrow_forward),
              ),
              ListTile(
                title: Text("ボタン3"),
                trailing: Icon(Icons.arrow_forward),
              ),
            ],
          ),
        ),

        appBar: AppBar(
          centerTitle: true, // タイトルが中央になります。
          title: Text('Toggle theme sample'),
          // AppBarの左側にボタンが配置されます。
          // leadingプロパティを設定しているとDrawerが表示されないようです。(みえてないだけ?)
          // leading: IconButton(
          //   icon: Icon(Icons.add),
          //   onPressed: () {},
          // ),
          // AppBarの右側にボタンが配置されます。
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.group),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => NextPage(),
                  ),
                );
              },
            )
          ],
          // AppBarの下の部分のを担当します。
          // PreferredSizeウィジェットはchildの高さの影響を受けません。
          bottom: PreferredSize(
            preferredSize: Size.fromHeight(50),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                'Bottom',
                style: TextStyle(fontSize: 32),
              ),
            ),
          ),
          // AppBarとbody部分との影の深さ(z軸)を指定します。
          elevation: 20,
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              setState(() => dark = !dark);
            },
            child: Text('Toggle theme'),
          ),
        ),
      ),
    );
  }
}

class NextPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false, // 戻るアイコンが表示されなくなります。
        title: Text("次のページ"),
      ),
      body: Container(
        height: double.infinity,
        color: Colors.red,
      ),
    );
  }
}

すいすいっとメニューがでてきます。

最後に

メニュー欄・・・みたいなイメージです。

今日はここまで!

参考

FlutterのWidgetをコードを動かしながら学ぶ: Drawer編 | DevelopersIO (classmethod.jp)