FlutterでDropdownButton
を親の横幅いっぱいに広げたい!
概要
今回の記事では、FlutterでDropdownButton
を親の横幅いっぱいに広げる手順を掲載する。
仕様書
環境
- Android Studio Giraffe | 2023.2.1 Patch 2
- Flutter 3.19.6
手順書
TextButton
の場合は、width: double.infinity
したSizedBox
を使って親の横幅いっぱいに広げることができたが、DropdownButton
ではこの方法でサイズを変えることはできなかった。
DropdownButton
の場合は、isExpanded
プロパティをtrue
にするだけで親の横幅いっぱいに広げることができる。
import 'package:flutter/material.dart';
class DropdownButtonExpandSample1 extends StatelessWidget {
const DropdownButtonExpandSample1({super.key});
@override
Widget build(BuildContext context) {
return SafeArea(
child: Center(
child: DropdownButton<String>(
value: '1',
isExpanded: true,
onChanged: () => {},
items: const [
DropdownMenuItem(
value: '1',
child: Text('One'),
),
DropdownMenuItem(
value: '2',
child: Text('Two'),
),
DropdownMenuItem(
value: '3',
child: Text('Three'),
)
],
),
),
);
}
}
まとめ(感想文)
もう全部のWedgetにこのプロパティを付けてくれないかな!