【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part61 Multi-child layout widgets

基礎 から 学ぶ Flutter 」という書籍で  学習 したことを ブログでアウトプットしていこうと思います。今回は ウィジェット編 ( part61 )です。

前回

【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part60 Multi-child layout widgets

引き続き、Multi-child layout widgetsについて学びます。

Multi-child layout widgets

Tableウィジェット

defaultVerticalAlignment

defaultVerticalAlignmentは行のセル要素を中央、上寄り、下寄りに配置することを選択できます。

中央寄せ
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Container(
        child: Table(
          // 下線を灰色で表示
          border: TableBorder(horizontalInside: BorderSide(color: Colors.grey)),
          // セルの要素が中央に配置されます。
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          children: <TableRow>[
            // 1行目
            TableRow(children: <Widget>[
              box(
                  height: 100.0,
                  color: Colors.black,
                  txt: "Row 1 \n Element 1"),
              box(height: 50.0, color: Colors.green, txt: "Row 1 \n Element 2"),
              box(height: 50.0, color: Colors.amber, txt: "Row 1 \n Element 3")
            ]),
            // 2行目
            TableRow(children: <Widget>[
              box(height: 48.0, color: Colors.blue, txt: "Row 2 \n Element 1"),
              box(
                  height: 48.0,
                  color: Colors.deepPurple,
                  txt: "Row 2 \n Element 2"),
              box(height: 100.0, color: Colors.red, txt: "Row 2 \n Element 3")
            ]),
          ],
        ),
      ),
    ));
  }

  Widget box(
      {double height = 120, Color color = Colors.green, String txt = "0"}) {
    return Container(
        margin: const EdgeInsets.all(2),
        height: height,
        color: color,
        child: Text(txt));
  }
}

 

上寄せ
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Container(
        child: Table(
          // 下線を灰色で表示
          border: TableBorder(horizontalInside: BorderSide(color: Colors.grey)),
          // セルの要素が上寄りに配置されます。
          defaultVerticalAlignment: TableCellVerticalAlignment.top,
          children: <TableRow>[
            // 1行目
            TableRow(children: <Widget>[
              box(
                  height: 100.0,
                  color: Colors.black,
                  txt: "Row 1 \n Element 1"),
              box(height: 50.0, color: Colors.green, txt: "Row 1 \n Element 2"),
              box(height: 50.0, color: Colors.amber, txt: "Row 1 \n Element 3")
            ]),
            // 2行目
            TableRow(children: <Widget>[
              box(height: 48.0, color: Colors.blue, txt: "Row 2 \n Element 1"),
              box(
                  height: 48.0,
                  color: Colors.deepPurple,
                  txt: "Row 2 \n Element 2"),
              box(height: 100.0, color: Colors.red, txt: "Row 2 \n Element 3")
            ]),
          ],
        ),
      ),
    ));
  }

  Widget box(
      {double height = 120, Color color = Colors.green, String txt = "0"}) {
    return Container(
        margin: const EdgeInsets.all(2),
        height: height,
        color: color,
        child: Text(txt));
  }
}

下寄り
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Container(
        child: Table(
          // 下線を灰色で表示
          border: TableBorder(horizontalInside: BorderSide(color: Colors.grey)),
          // セルの要素が上寄りに配置されます。
          defaultVerticalAlignment: TableCellVerticalAlignment.bottom,
          children: <TableRow>[
            // 1行目
            TableRow(children: <Widget>[
              box(
                  height: 100.0,
                  color: Colors.black,
                  txt: "Row 1 \n Element 1"),
              box(height: 50.0, color: Colors.green, txt: "Row 1 \n Element 2"),
              box(height: 50.0, color: Colors.amber, txt: "Row 1 \n Element 3")
            ]),
            // 2行目
            TableRow(children: <Widget>[
              box(height: 48.0, color: Colors.blue, txt: "Row 2 \n Element 1"),
              box(
                  height: 48.0,
                  color: Colors.deepPurple,
                  txt: "Row 2 \n Element 2"),
              box(height: 100.0, color: Colors.red, txt: "Row 2 \n Element 3")
            ]),
          ],
        ),
      ),
    ));
  }

  Widget box(
      {double height = 120, Color color = Colors.green, String txt = "0"}) {
    return Container(
        margin: const EdgeInsets.all(2),
        height: height,
        color: color,
        child: Text(txt));
  }
}

最後に

今回のはわかりやすくて助かりますね。

今日はここまで!

 

参考

Flutterウィジェットガイド—5分以内のテーブルウィジェット。 (ichi.pro)