【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part70 Painting and effects

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

前回

【 Flutter 】Flutter を 基礎 から 学習 ( ウィジェット編 ) part69 Painting and effects

引き続き、Painting and effectsについて学びます。

Painting and effects

Opacityウィジェット

Opacityウィジェットは透明度を変えるウィジェットです。
画像関係で「透過度」でお世話になるやつですね。

0.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> {
  int _index = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Container(
          alignment: Alignment.topLeft,
          child: Row(children: <Widget>[
            box(color: Colors.blue),
            Opacity(opacity: 0.0, child: box(txt:"show!")),
            box(color: Colors.red)
          ])),
    ));
  }

  Widget box(
      {double height = 50,
      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));
  }
}

0.5(うっすら見える)にしてみました。

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> {
  int _index = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: Container(
          alignment: Alignment.topLeft,
          child: Row(children: <Widget>[
            box(color: Colors.blue),
            Opacity(opacity: 0.5, child: box(txt: "show!")),
            box(color: Colors.red)
          ])),
    ));
  }

  Widget box(
      {double height = 50,
      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));
  }
}

 

最後に

「レイアウトを保ったまま」ウィジェットを非表示にしたい場合に使えるようです。
「レイアウトを保たなくてもよい!」という人はウィジェットに備わっている非表示機能を使えばよさそう(ここは未確認です)。

今日はここまで!