[Flutter]CupertinoPickerに枠線をつけるには?

Flutter

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

CupertinoPickerに枠線をつける方法を紹介します。

スポンサーリンク

方法

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

CupertinoPickerに枠線をつけるには、Containerを使います。

まず、CupertinoPickerをContainerでラップし、Containerの引数「decoration」にBoxDecorationを指定します。

そして、BoxDecorationの引数「border」にBorder.all()を指定します。

Container(
  decoration: BoxDecoration(
    border: Border.all(),
  ),
  child: CupertinoPicker(・・・),
),

Containerを使えば、CupertinoPickerに枠線をつけることが出来ます。

使用例

以下は、使用例です。

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

class _MyHomePageState extends State<MyHomePage> {
  var _selectedValue = 0;
  final _fruits = [
    "Apple",
    "Banana",
    "StrawBerry",
    "Orange",
    "Watermelon",
  ];
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: CupertinoPageScaffold(
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('${_fruits[_selectedValue]}'),
              Container(
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Colors.pink,
                    width: 2.0,
                  ),
                ),
                height: 200,
                child: CupertinoPicker(
                  itemExtent: 30.0,
                  children: _fruits.map((e) => Text(e)).toList(),
                  onSelectedItemChanged: (newValue) {
                    setState(() {
                      _selectedValue = newValue;
                    });
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

コメント

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