検索
[Flutter/dart] ModalBottomSheet内でスクロールする
- M.R

- 2021年5月4日
- 読了時間: 1分
概要
スマホアプリでは、メニューを選択してもらう時にメニューが画面下から現れる場合がありますが、これはFlutterではshowModalBottomSheetで作成できます。
しかし、表示したいメニューが多いと、ModalBottomSheetの中に入りきりません。このような場合、ModalBottomSheetの中でスクロールする方法をご紹介します。
方法
DraggableScrollableSheetを使います。
以下のように、showModalBottomSheetのbuilderでDraggableScrollableSheetを作成し、その子に表示したいWidgetを指定します。
@override
Widget build(BuildContext context) {
List<String> menues=List.generate(20, (index) => "メニュー${index}");
return Scaffold(
appBar: AppBar(
title: Text('demo'),
),
body: Container(
padding: const EdgeInsets.all(16),
child: RaisedButton(
child: Text('show menu'),
onPressed: ()async{
int t=await showModalBottomSheet(
context: context,
builder: (context)=> DraggableScrollableSheet( //これ!
initialChildSize: 1,
builder: (BuildContext context, ScrollController
scrollController)=>
Container(
child: ListView(
shrinkWrap: true,
children: menues.map((e) =>
ListTile(
title: Text(
e,
style: TextStyle(
fontSize: 18
),
),
)
).toList(),
),
)
)
);
},
)
)
);
}initialChildSizeは親Widgetに占める割合を設定します。
以下の動画のようにスクロールできるようになります!






コメント