今回はshowModalBottomSheet
の基本的な使い方を紹介します。
ボタンのタップイベントなどにshowModalBottomSheet
メソッドを使うことで画面下部からボトムシートを表示できます。
目次
showModalBottomSheetの使い方
ElevatedButton(
child: Text('showModalBottomSheet'),
onPressed: () => showModalBottomSheet(
context: context,
builder: (context) {
return Container();
},
),
),
showModalBottomSheet
メソッドは上記コードのように書きます。
builder
の引数にコールバック関数でボトムシートに表示したいWidgetを渡します。
ボトムシートを非表示にするメソッド
ElevatedButton(
child: Text('showModalBottomSheet'),
onPressed: () => showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: 200,
child: TextButton(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
);
},
),
),
Navigator.of(context).pop()
でボトムシートを非表示にできます。
サンプルコード
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('FlutterZero'),
),
body: MyCustomApp(),
),
);
}
}
class MyCustomApp extends StatefulWidget {
const MyCustomApp({Key? key}) : super(key: key);
@override
State<MyCustomApp> createState() => _MyCustomAppState();
}
class _MyCustomAppState extends State<MyCustomApp> {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text('showModalBottomSheet'),
onPressed: () => showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: 200,
child: TextButton(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
);
},
),
),
);
}
}
一緒に読みたい
あわせて読みたい
【Flutter】DraggableScrollableSheet|画面下部からドラッグ可能なスクロールシートを表示
今回はDraggableScrollableSheetの基本的な使い方を紹介します。 DraggableScrollableSheetを使えば画面下部からListViewなどのスクルール可能なWidgetをドラッグして表…
あわせて読みたい
【Flutter】ListView|スクロール可能なリストを表示
今回はListViewの基本的な使い方を紹介します。 ListViewを使えば複数のWidgetをリスト表示できます。 【ListViewの使い方】 ListView( children: [ Padding( padding: …
参考