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

Flutter

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

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

スポンサーリンク

方法

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

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

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

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

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

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

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

使用例

以下は、使用例です。

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: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.grey,
                offset: Offset(1.0, 1.0),
                blurRadius: 0.8,
                spreadRadius: 0.8,
              ),
            ],
          ),
          child: DropdownButtonHideUnderline(
            child: DropdownButton(
              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をコピーしました