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

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

前回

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

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

Painting and effects

DecoratedBoxウィジェット

positionプロパティ

positionプロパティは装飾する位置を指定するプロパティです。

childrenウィジェットの前に描画するのか、後ろに描画するのかを指定します。

foreground
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: DecoratedBox(
        position: DecorationPosition.foreground, //★
        decoration: BoxDecoration(
          color: const Color(0xFFFFFFFF),
          border: Border.all(
            color: const Color(0xFF000000),
            style: BorderStyle.solid,
            width: 4.0,
          ),
          borderRadius: BorderRadius.zero,
          shape: BoxShape.rectangle,
          boxShadow: const <BoxShadow>[
            BoxShadow(
              color: Color(0x66000000),
              blurRadius: 10.0,
              spreadRadius: 4.0,
            )
          ],
        ),
        child: Container(
          child: Container(
            width: 200,
            height: 200,
            padding: EdgeInsets.all(20),
            child: box(),
          ),
        ),
      ),
    ));
  }

  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));
  }
}

 

黒い線の四角が表示されています。
(背後には緑のウィジェットが隠れています。)

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

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

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

class _State extends State {
  int _index = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('テスト中です。'),
      ),
      body: DecoratedBox(
        position: DecorationPosition.background,//★
        decoration: BoxDecoration(
          color: const Color(0xFFFFFFFF),
          border: Border.all(
            color: const Color(0xFF000000),
            style: BorderStyle.solid,
            width: 4.0,
          ),
          borderRadius: BorderRadius.zero,
          shape: BoxShape.rectangle,
          boxShadow: const [
            BoxShadow(
              color: Color(0x66000000),
              blurRadius: 10.0,
              spreadRadius: 4.0,
            )
          ],
        ),
        child: Container(
          child: Container(
            width: 200,
            height: 200,
            padding: EdgeInsets.all(20),
            child: box(),
          ),
        ),
      ),
    ));
  }

  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));
  }
}

DecoratedBoxウィジェットが緑のウィジェットの背後にまわりました。

最後に

コンテンツと装飾は分けて実装するほうが良いのですね。

今日はここまで!