0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter】 showModalBottomSheetの高さを比率で指定する

Last updated at Posted at 2022-01-16

はじめに

showModalBottomSheetはデフォルトで50%くらいの高さで表示されます。

スクリーンショット 2022-01-16 20.51.49.png

それを👇のように80%の高さで表示させます!
タイトルなし.gif

前提

画面サイズに対する比率で高さを指定するには👆の記事を参考にsize_config.dartを作成しておいてください:bow:

showModalBottomSheetを用意

import '../../widgets/comment/view_comments.dart';

// 省略

         Padding(
              padding: EdgeInsets.symmetric(vertical: 20),
              child: Center(
                child: IconButton(
                  icon: Icon(
                    Icons.comment,
                    size: 40,
                  ),
                  onPressed: () => showModalBottomSheet(
                    isScrollControlled: true,
                    shape: RoundedRectangleBorder(
                        borderRadius:
                            BorderRadius.vertical(top: Radius.circular(20))),
                    context: context,
                     // showModalBottomSheetで表示される中身
                    builder: (context) => ViewComments(),
                  ),
                ),
              ),
            ),

showModalBottomSheetの中身

view_comments.dart
import '../../config/size_config.dart';

class ViewComments extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Container(
       // 80%の高さで表示させる
      height: SizeConfig.blockSizeVertical * 80,
       // 省略
   );
 }
}

最後に

showModalBottomSheetの高さを変更する方法は👇のDraggableScrollableSheetを使う場合が一般的かもしれません。

しかしながら、DraggableScrollableSheetを使うと角を丸くしたりmodal外をタップでdismissさせたりと言った処理が効かなくなり、ちょっと一工夫が必要になるっぽいのでめんどくさくてやめました・・・w

DraggableScrollableSheetを使う場合は👇が参考になるかと!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?