LoginSignup
6
2

More than 3 years have passed since last update.

Flutter で Button の背景に画像を設定する

Last updated at Posted at 2020-09-13

やりたいこと

Flutter の Button コンポーネントに 背景画像を設定して、画像ボタンを作りたい。

Group 1.png

この画像を背景にした Button を作る。

image.png

(なんで じゃんけん なのかは ひみつ)

どうする

Background のようなオプションは見当たらなかったので、child に画像を設定することで実装する。

実装

import 'package:flutter/material.dart';
import 'package:janken/gen/assets.gen.dart';

typedef void IncrementCallback();

class IncrementButton extends StatelessWidget {
  const IncrementButton(this._increment);

  final IncrementCallback _increment;

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: this._increment,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.0),
      ),
      color: Colors.orange,
      child: Container(
        width: 180,
        height: 180,
        decoration: BoxDecoration(
          image: DecorationImage(
        // Assets.images.janken.trianglePng.path には 画像Path が入る
              image: AssetImage(Assets.images.janken.trianglePng.path), 
              fit: BoxFit.scaleDown),
        ),
      ),
    );
  }
}

ひとこと

理解してれば「そりゃそう」なのかもだけど、理解するまでは方向性がつかめずに苦労しそう。

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2