[Flutter]ModalBottomSheet(モーダルボトムシート)の高さを設定するには?

Flutter

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

ModalBottomSheetの高さを設定する方法を紹介します。

スポンサーリンク

方法

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

ModalBottomSheetの高さを設定するには、showModalBottomSheetの引数「isScrollControled」とCotainerを使います。

まず、showModalBottomSheetの引数「isScrollControled」に「true」を指定します。

showModalBottomSheet(
  isScrollControlled: true,
  context: context,
  builder: (BuildContext context) {
    return BottomSheet();
  },
);

次に、showModalBottomSheetの引数「builder」で表示するウェジェットをContainerのchildにします。

そして、Containerの引数「height」で高さを設定します。

showModalBottomSheet(
  isScrollControlled: true,
  context: context,
  builder: (BuildContext context) {
    return Container(
      height: /*高さ*/,
      child : BottomSheet()
    );
  },
);

これでModalBottomSheetの高さを設定することが出来ます。

showModalBottomSheetの引数「isScrollControled」は、高さが画面の半分以下なら使う必要がありません。

ですが、画面の半分以上の場合は、「isScrollControled」に「true」を指定します。

以下は、使用例です。

使用例
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {

            showModalBottomSheet(
              isScrollControlled: true,
              context: context,
              builder: (BuildContext context) {
                return Container(
                  height: 700,
                  width: double.infinity,
                  color: Colors.pink,
                );
              },
            );

          },
          child: Text('Show'),
        ),
      ),
    );
  }

コメント

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