LoginSignup
7
3

More than 3 years have passed since last update.

Flutter AlertDialogの幅を画面いっぱいに引き伸ばす

Posted at

AlertDialogの幅を画面いっぱいに引き伸ばしたい

もともとは通常の画面だったものを、ダイアログ化してほしいと言われ、
ダイアログ化したのだけど、どうにも窮屈な感じになってしまっていたので、
ダイアログのパディングを減らしてほしといわれたが、ちょっと詰まった話。

通常のAlertDialog

通常Dialogを表示する場合は下記のようなコードになると思います。

showDialog(
  context: context,
  builder: (context) {
    return AlertDialog(
      title: Text('タイトル'),
      content: Text('メッセージ'),
      actions: [
        ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: Text('キャンセル'),
        ),
        ElevatedButton(
          onPressed: () => Navigator.pop(context, 'OK'),
          child: Text('OK'),
        ),
      ],
    );
  },
);

実行時の画像
通常のAlertDialog

上記コードの content部分を次のように変更

content: Container(
  width: 400,
  child: Text('メッセージ'),
),

実行時の画像
content,width400

何も変わっていない!!
原因はでAlertDialogにはデフォルトでマージンが設定されていることだった。
下記のようにinsetPaddingを設定することで問題なく幅を広げることができた。

showDialog(
  context: context,
  builder: (context) {
    return AlertDialog(
      insetPadding: EdgeInsets.all(8),
      title: Text('タイトル'),
      content: Container(
        width: 400,
        child: Text('メッセージ'),
      ),
      actions: [
        ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: Text('キャンセル'),
        ),
        ElevatedButton(
          onPressed: () => Navigator.pop(context, 'OK'),
          child: Text('OK'),
        ),
      ],
    );
  },
);

実行時の画像
insetPadding_override

無事ダイアログの幅を広げることができた。

7
3
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
7
3