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

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

前回

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

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

Multi-child layout widgets

Flexibleウィジェット

Flexibleウィジェットは親ウィジェットのサイズ変更に自身も従う従順なウィジェットです。

flexプロパティ

fitプロパティは前回のfitプロパティ同様に重みづけです。

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(
          alignment: Alignment.center,
          width: 100,
          child: Column(
            children: <Widget>[
              box(),
              Flexible(flex: 1, child: Container(color: Colors.cyan)),
              Flexible(flex: 2, child: Container(color: Colors.red))
            ],
          ),
        ),
      ),
    );
  }

  Widget box({
    Color color = Colors.green,
    Alignment alignment = Alignment.center,
  }) {
    return Align(
        alignment: alignment,
        child: SizedBox(
            width: 100,
            height: 100,
            child: DecoratedBox(
              decoration: BoxDecoration(
                color: color,
              ),
            )));
  }
}

いい感じの割合で表示されています。

最後に

師走でばたばたです😅

今日はここまで!