feat: add useSafeAreaPadding option for a default padding at the bottom of the dialogs

This commit is contained in:
Freek van de Ven 2024-06-06 09:57:55 +02:00 committed by Freek van de Ven
parent be6784a3db
commit c8c38b202f
2 changed files with 37 additions and 1 deletions

View file

@ -1,5 +1,6 @@
## 1.0.0 ## 1.0.0
- Flutter_dialogs and flutter_bottom_alert_dialogs combined into one package - Flutter_dialogs and flutter_bottom_alert_dialogs combined into one package
- Add default padding to the bottom alert dialog that is the size of the safe area of a device. You can disable this by setting `useSafeAreaPadding` to false.
## 0.0.2 ## 0.0.2

View file

@ -12,8 +12,14 @@ class BottomAlertDialogAction extends StatelessWidget {
this.buttonType = ButtonType.tertiary, this.buttonType = ButtonType.tertiary,
super.key, super.key,
}); });
/// The text to be displayed on the button.
final String text; final String text;
/// The type of button to be displayed.
final ButtonType buttonType; final ButtonType buttonType;
/// The callback to be called when the button is pressed.
final VoidCallback onPressed; final VoidCallback onPressed;
@override @override
@ -41,11 +47,13 @@ class BottomAlertDialog extends StatelessWidget {
required List<Widget> buttons, required List<Widget> buttons,
List<BottomAlertDialogAction>? actions, List<BottomAlertDialogAction>? actions,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog._( BottomAlertDialog._(
closeButton: closeButton, closeButton: closeButton,
buttons: buttons, buttons: buttons,
actions: actions, actions: actions,
useSafeAreaPadding: useSafeAreaPadding ?? true,
body: (_) => body, body: (_) => body,
); );
@ -57,12 +65,14 @@ class BottomAlertDialog extends StatelessWidget {
required VoidCallback onPressed, required VoidCallback onPressed,
ButtonType buttonType = ButtonType.tertiary, ButtonType buttonType = ButtonType.tertiary,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog.icon( BottomAlertDialog.icon(
closeButton: closeButton, closeButton: closeButton,
title: title, title: title,
icon: icon, icon: icon,
body: body, body: body,
useSafeAreaPadding: useSafeAreaPadding,
buttons: [ buttons: [
BottomAlertDialogAction( BottomAlertDialogAction(
text: buttonText, text: buttonText,
@ -81,12 +91,14 @@ class BottomAlertDialog extends StatelessWidget {
bool focusYes = true, bool focusYes = true,
bool otherSecondary = false, bool otherSecondary = false,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog.icon( BottomAlertDialog.icon(
closeButton: closeButton, closeButton: closeButton,
title: title, title: title,
body: body, body: body,
icon: icon, icon: icon,
useSafeAreaPadding: useSafeAreaPadding,
buttons: _getYesNoDialogButtons(focusYes, otherSecondary, onYes, onNo), buttons: _getYesNoDialogButtons(focusYes, otherSecondary, onYes, onNo),
); );
@ -98,12 +110,14 @@ class BottomAlertDialog extends StatelessWidget {
bool focusYes = true, bool focusYes = true,
bool otherSecondary = false, bool otherSecondary = false,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog.multiButton( BottomAlertDialog.multiButton(
closeButton: closeButton, closeButton: closeButton,
title: title, title: title,
body: body, body: body,
buttons: const [], buttons: const [],
useSafeAreaPadding: useSafeAreaPadding,
actions: _getYesNoDialogButtons(focusYes, otherSecondary, onYes, onNo), actions: _getYesNoDialogButtons(focusYes, otherSecondary, onYes, onNo),
); );
@ -114,11 +128,13 @@ class BottomAlertDialog extends StatelessWidget {
required List<Widget> buttons, required List<Widget> buttons,
List<BottomAlertDialogAction>? actions, List<BottomAlertDialogAction>? actions,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog._( BottomAlertDialog._(
closeButton: closeButton, closeButton: closeButton,
buttons: buttons, buttons: buttons,
actions: actions, actions: actions,
useSafeAreaPadding: useSafeAreaPadding ?? true,
body: (context) => Column( body: (context) => Column(
children: [ children: [
icon, icon,
@ -140,11 +156,13 @@ class BottomAlertDialog extends StatelessWidget {
required List<BottomAlertDialogAction> buttons, required List<BottomAlertDialogAction> buttons,
List<BottomAlertDialogAction>? actions, List<BottomAlertDialogAction>? actions,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog._( BottomAlertDialog._(
closeButton: closeButton, closeButton: closeButton,
buttons: buttons, buttons: buttons,
actions: actions, actions: actions,
useSafeAreaPadding: useSafeAreaPadding ?? true,
body: (context) => Column( body: (context) => Column(
children: [ children: [
Padding( Padding(
@ -167,11 +185,13 @@ class BottomAlertDialog extends StatelessWidget {
required VoidCallback onPressed, required VoidCallback onPressed,
ButtonType buttonType = ButtonType.tertiary, ButtonType buttonType = ButtonType.tertiary,
bool? closeButton, bool? closeButton,
bool? useSafeAreaPadding,
}) => }) =>
BottomAlertDialog.multiButton( BottomAlertDialog.multiButton(
closeButton: closeButton, closeButton: closeButton,
title: title, title: title,
body: body, body: body,
useSafeAreaPadding: useSafeAreaPadding,
buttons: [ buttons: [
BottomAlertDialogAction( BottomAlertDialogAction(
text: buttonText, text: buttonText,
@ -180,17 +200,29 @@ class BottomAlertDialog extends StatelessWidget {
), ),
], ],
); );
const BottomAlertDialog._({ const BottomAlertDialog._({
required this.buttons, required this.buttons,
required this.body, required this.body,
this.useSafeAreaPadding = true,
this.actions, this.actions,
this.closeButton = false, this.closeButton = false,
}); });
final List<Widget> buttons; final List<Widget> buttons;
/// The body of the dialog.
final WidgetBuilder body; final WidgetBuilder body;
/// Whether to show a close button in the top right corner of the dialog.
final bool? closeButton; final bool? closeButton;
final List<BottomAlertDialogAction>? actions; final List<BottomAlertDialogAction>? actions;
/// This adds a padding at the bottom of the dialog to account for the
/// safearea padding. Set this to false if you don't want the extra padding,
/// it is enabled by default.
final bool useSafeAreaPadding;
static List<BottomAlertDialogAction> _getYesNoDialogButtons( static List<BottomAlertDialogAction> _getYesNoDialogButtons(
bool focusYes, bool focusYes,
bool otherSecondary, bool otherSecondary,
@ -233,7 +265,10 @@ class BottomAlertDialog extends StatelessWidget {
const Spacer(), const Spacer(),
AlertDialog( AlertDialog(
insetPadding: EdgeInsets.zero, insetPadding: EdgeInsets.zero,
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.only(
bottom:
useSafeAreaPadding ? MediaQuery.of(context).padding.bottom : 0,
),
backgroundColor: backgroundColor:
config.backgroundColor ?? Theme.of(context).cardColor, config.backgroundColor ?? Theme.of(context).cardColor,
shape: const RoundedRectangleBorder( shape: const RoundedRectangleBorder(