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

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

前回

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

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

Multi-child layout widgets

Tableウィジェット

defaultColumnWidthプロパティ

FractionColumnWidth

FractionColumnWidth親の幅の割合で指定するプロパティです。

1.0を指定した場合。

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(
        width: 150,
        child: Table(
          defaultColumnWidth: FractionColumnWidth(1.0), // 親の幅の割合
          border: TableBorder.all(),
          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));
  }
}

表示はされましたが何やらエラーが発生しました。

0.25の場合

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(
        width: 150,
        child: Table(
          defaultColumnWidth: FractionColumnWidth(0.25), // 親の幅の割合
          border: TableBorder.all(),
          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));
  }
}

今度は特にエラーはでませんでした。

IntrinsicColumnWidth

最後に

なんのエラーなのか?幅が足りなかったのでしょうかね?

今日はここまで!