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

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

前回

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

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

Multi-child layout widgets

Tableウィジェット

borderプロパティ

borderプロパティは枠線を表示することができます。上下左右指定でき、デフォルトは枠線無しになります。

前回はTableBorder.all()を指定したので上下左右の枠線が表示されたのでした。

ちょっとわかりづらいですが下線だけにしてみました。

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));
  }
}

こんな風に自由に指定できます。

最後に

まるでHTMLのテーブルを操作しているみたい!

今日はここまで!