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

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

前回

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

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

Single-child layout widgets

FractionallySizedBoxウィジェット

FractionallySizedBoxウィジェットはchildプロパティで設定されたウィジェットに対して作用します。
表示できる幅・高さの割合を指定できます。

widthFactor, heightFactorプロパティ

childプロパティで設定されたウィジェットに対して、表示できる割合を指定できます。
childプロパティ内で指定したwidthheightの幅・高さは無効になります。

「親の言うことを聞きなさい!」ということですね😣

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

var ok = false;

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

class _State extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var w = size.width;

    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Align(
          alignment: Alignment.center,
          child: FractionallySizedBox(
              // 子に対して有効なプロパティです。
              widthFactor: 0.75,
              heightFactor: 1,
              child: SizedBox(
                  width: 1, // この値は無効です。
                  height: 1, // この値も無効です。
                  child: DecoratedBox(
                    decoration: BoxDecoration(color: Colors.greenAccent),
                  )))),
    ));
  }
}

SizedBoxwidth,height指定が無効になっているのがわかりますね。

最後に

子のウィジェットをリサイクルした際に親が幅・高さを制御できるのは良いですね。

今日はここまで!