LoginSignup
1
0

More than 1 year has passed since last update.

【Flutter/Dart】Cannot provide both a color and a decoration のエラーの解決策

Last updated at Posted at 2021-12-08

環境

-> %  sw_vers
ProductName:    macOS
ProductVersion: 12.0.1
BuildVersion:   21A559

-> % flutter --version
Flutter 2.5.3  channel stable  https://github.com/flutter/flutter.git
Tools  Dart 2.14.4

実装したかったこと

スクリーンショット 2021-12-08 21.57.06.png

このような角丸で背景色のあるWidgetを作成したい🔵

エラー文

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Practice(dirty):
Cannot provide both a color and a decoration
To provide both, use "decoration: BoxDecoration(color: color)".
'package:flutter/src/widgets/container.dart':
package:flutter//widgets/container.dart:1
Failed assertion: line 274 pos 15: 'color == null || decoration == null'

エラーが発生した際のコード

class Practice extends StatelessWidget {
  const Practice12({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: 
          Container(
            color: Colors.blue,
            width: 100,
            height: 100,
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(16)),
          ),
    );
  }
}

修正したコード

To provide both, use "decoration: BoxDecoration(color: color)".
'package:flutter/src/widgets/container.dart':

ということで、答えはエラー文に書いてありました☺️

ContainerクラスのcolorプロパティとBoxDecorationを併用するのはうまくいかなそうなので、
代わりにBoxDecorationクラスのcolorプロパティを使用するように変更することで意図したWidgetが実装できました!


class Practice extends StatelessWidget {
  const Practice({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child:  Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16), color: Colors.blue),
          ),
    );
  }
}

1
0
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
1
0