【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part65 Multi-child layout widgets
「基礎 から 学ぶ Flutter 」という書籍で 学習 したことを ブログでアウトプットしていこうと思います。今回は ウィジェット編 ( part65 )です。
前回
【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part64 Multi-child layout widgets
引き続き、Multi-child layout widgetsについて学びます。
Multi-child layout widgets
Tableウィジェット
defaultColumnWidthプロパティ
MaxColumnWidth
MaxColumnWidth
は2つの幅(引数)を比べ、大きい方を採用する方式となります。
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(
// 100.0が採用されています。
defaultColumnWidth: MaxColumnWidth(
const FixedColumnWidth(70.0), const FixedColumnWidth(100.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,
double width = 50,
Color color = Colors.green,
String txt = "0"}) {
return Container(
margin: const EdgeInsets.all(2),
height: height,
width: width,
color: color,
child: Text(txt));
}
}
2つ目の引数の値が採用されました。
MinColumnWidth
MinColumnWidth
は2つの幅(引数)を比べ、最小の幅を設定します。
ただし、最小には閾値があり、「最小」として採択されなかったもう一方の幅が閾値の役割を担います。
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(
// 50.0が採用されています。
defaultColumnWidth: MinColumnWidth(
const FixedColumnWidth(50.0), const FixedColumnWidth(90.0)),
border: TableBorder.all(),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: <TableRow>[
// 1行目
TableRow(children: <Widget>[
box(
height: 100.0,
width: 200.0, // MinColumnWidthにて90を越えられないのでこの値は無効となります。
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,
double width = 50,
Color color = Colors.green,
String txt = "0"}) {
return Container(
margin: const EdgeInsets.all(2),
height: height,
width: width,
color: color,
child: Text(txt));
}
}
最小の値として50.0が採択され、幅の最大は90.0の値が採択されました。
最後に
しかしこれってどんな場面で使用されるのでしょうかね?
今日はここまで!
ディスカッション
コメント一覧
まだ、コメントがありません