dialog function Null safety

dynamic dialog(
  1. {required BuildContext context,
  2. required dynamic item,
  3. required Function statusMsg,
  4. Function? onSubmit,
  5. bool bodyIsWidget = false,
  6. Widget bodyAsWidget = const Text(''),
  7. String title = '',
  8. String body = '',
  9. String submitText = '',
  10. ButtonStyle submitStyle = const ButtonStyle(),
  11. bool submitInput = true,
  12. bool centerText = false,
  13. String cancelText = '',
  14. Function? onCancel}
)

Implementation

dialog({
  required BuildContext context,
  required item,
  required Function statusMsg,
  Function? onSubmit,
  bool bodyIsWidget = false,
  Widget bodyAsWidget = const Text(''),
  String title = '',
  String body = '',
  String submitText = '',
  ButtonStyle submitStyle = const ButtonStyle(),
  bool submitInput = true,
  bool centerText = false,
  String cancelText = '',
  Function? onCancel,
}) {
  final controller = TextEditingController();
  plausible.event(page: title.split(' ')[0].toLowerCase());
  showDialog(
    context: context,
    builder: (context) {
      return ContentDialog(
        constraints: const BoxConstraints(maxHeight: 300.0, maxWidth: 400.0),
        title: centerText ? Center(child: Text(title)) : Text(title),
        content: Column(
          children: [
            !bodyIsWidget
                ? centerText
                    ? Center(child: Text(body))
                    : Text(body)
                : SizedBox(
                    width: double.infinity,
                    height: 120.0,
                    child: SingleChildScrollView(child: bodyAsWidget)),
            submitInput
                ? Padding(
                    padding: const EdgeInsets.only(top: 10.0),
                    child: TextBox(
                      controller: controller,
                      placeholder: item,
                    ),
                  )
                : const Text(''),
          ],
        ),
        actions: [
          submitText != ''
              ? Button(
                  style: submitStyle,
                  onPressed: () {
                    Navigator.pop(context);
                    if (onSubmit != null) {
                      onSubmit(controller.text);
                    }
                  },
                  child: Text(submitText))
              : Container(),
          Button(
              child: Text(cancelText == '' ? 'cancel-text'.i18n() : cancelText),
              onPressed: () {
                if (onCancel != null) {
                  onCancel();
                }
                Navigator.pop(context);
              }),
        ],
      );
    },
  );
}