[Flutter]DropdownButtonFormFieldに影をつけるには?

Flutter

どうも、ちょげ(@chogetarou)です。

DropdownButtonFormFieldに影をつける方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

DropdownButtonFormFieldに影をつけるには、Containerを使います。

まず、DropdownButtonFormFieldをContainerでラップします。

次に、Containerの引数「decoration」にBoxDecorationを指定します。

そして、BoxDecorationの引数「shadow」に影の指定をします。

Container(
  decoration: BoxDecoration(
    boxShadow: [
      BoxShadow(・・・),
    ],
  ),
  child: DropdownButton(
      value: _text,
      items: [
        ・・・
      ],
      onChanged: (String? value) {
        ・・・
      },
  ),
),

Containerを使えば、DropdownButtonFormFieldに影をつけることが出来ます。

背景が透明なので、デフォルトでは影が透けて見えます。

なので、DropdownButtonFormFieldの背景色を設定する必要があります。

使用例

以下は、使用例です。

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _text = 'Hello';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            decoration: BoxDecoration(boxShadow: [
              BoxShadow(
                color: Colors.grey,
                offset: Offset(1.0, 1.0),
                blurRadius: 0.8,
                spreadRadius: 0.8,
              ),
            ]),
            child: DropdownButtonFormField(
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
              ),
              value: _text,
              items: [
                DropdownMenuItem(
                  child: Text('Hello'),
                  value: 'Hello',
                ),
                DropdownMenuItem(
                  child: Text('Hola'),
                  value: 'Hola',
                ),
                DropdownMenuItem(
                  child: Text('こんにちは'),
                  value: 'こんにちは',
                ),
              ],
              onChanged: (String? value) {
                setState(() {
                  _text = value ?? 'Hello';
                });
              },
            ),
          ),
        ),
      ),
    );
  }
}

コメント

タイトルとURLをコピーしました