iOS 技巧

Flutter对话框实践 2022-06-10 周五

2022-06-10  本文已影响0人  勇往直前888

简介

根据UI设计图,我们的目标是要实现如下形式的弹窗提示。

目标弹窗样式

Get.defaultDialog

GetX默认对话框 配置了参数后的对话框
Get.defaultDialog(
      title: '注册成功',
      titleStyle: TextStyle(
        color: const Color(0xFF000000),
        fontSize: 17.sp,
      ),
      middleText: '去登录邮箱激活账号,然后登录',
      middleTextStyle: TextStyle(
        color: const Color(0xFF000000),
        fontSize: 13.sp,
      ),
      radius: 14.sp,
      textConfirm: 'OK',
      confirmTextColor: const Color(0xFF007AFF),
      backgroundColor: const Color(0xD1F8F8F8),
      buttonColor: Colors.transparent,
      onConfirm: () {
        /// 确认按钮的代码
        ///  让对话框消失需要动用路由方法
        Get.back();
      },
      barrierDismissible: false, /// 既然是Alert,iOS定义的空白处不能退出更合理
    );
Widget baseAlertDialog = AlertDialog(
      titlePadding: titlePadding ?? const EdgeInsets.all(8),
      contentPadding: contentPadding ?? const EdgeInsets.all(8),

      backgroundColor: backgroundColor ?? theme.dialogBackgroundColor,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(radius))),
      title: Text(title, textAlign: TextAlign.center, style: titleStyle),
      content: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          content ??
              Text(middleText,
                  textAlign: TextAlign.center, style: middleTextStyle),
          const SizedBox(height: 16),
          ButtonTheme(
            minWidth: 78.0,
            height: 34.0,
            child: Wrap(
              alignment: WrapAlignment.center,
              spacing: 8,
              runSpacing: 8,
              children: actions,
            ),
          )
        ],
      ),
      // actions: actions, // ?? <Widget>[cancelButton, confirmButton],
      buttonPadding: EdgeInsets.zero,
    );

    return dialog<T>(
      onWillPop != null
          ? WillPopScope(
              onWillPop: onWillPop,
              child: baseAlertDialog,  /// 这个baseAlertDialog就是用AlertDialog来的
            )
          : baseAlertDialog,
      barrierDismissible: barrierDismissible,
      navigatorKey: navigatorKey,
    );
  }

对Get.defaultDialog的封装

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

typedef VoidCallback = void Function();

class PandaDialog {
  static void showAlert({
    String title = 'title',
    String content = 'content',
    String textConfirm = 'OK',
    String? textCancel,
    VoidCallback? onConfirm,
    VoidCallback? onCancel,
  }) {
    Get.defaultDialog(
      title: title,
      titleStyle: TextStyle(
        color: const Color(0xFF000000),
        fontSize: 17.sp,
      ),
      middleText: content,  // 自动支持多行文本,没有必要给content Widget
      middleTextStyle: TextStyle(
        color: const Color(0xFF000000),
        fontSize: 13.sp,
      ),
      radius: 14.sp,
      textConfirm: textConfirm,
      textCancel: textCancel,
      confirmTextColor: const Color(0xFF007AFF),
      backgroundColor: const Color(0xD1F8F8F8),
      buttonColor: Colors.transparent,
      onConfirm: () {
        if (onConfirm != null) {
          onConfirm();
        }
        Get.back(); // 让OK也能自动退出
      },
      onCancel: onCancel,  // cancel是自带退出的,不需要多此一举
      barrierDismissible: false, // 既然是Alert,当然是iOS定义的空白处不能退出更合理
    );
  }
}

简单测试

PandaDialog.showAlert();
默认调用
    PandaDialog.showAlert(
      title: '这是标题',
      content: '这是具体的描述',
      textConfirm: '确定',
      onConfirm: () {
        debugPrint('点击了确定按钮');
      },
    );
一个按钮
PandaDialog.showAlert(
      title: '这是标题',
      content: '这是具体的描述',
      textConfirm: '确定',
      onConfirm: () {
        debugPrint('点击了确定按钮');
      },
      textCancel: '取消',
      onCancel: () {
        debugPrint('点击了取消按钮');
      },
    );
两个按钮
    PandaDialog.showAlert(
      title: '这是标题',
      content: '''
这是具体的描述。这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.
这是多行文本。
这是多行文本。
这是多行文本。
这是多行文本。
// 这里还有个注释

这是多行文本。这是多行文本。
这是多行文本。
这是多行文本。
这是多行文本。
          ''',
      textConfirm: '确定',
      onConfirm: () {
        debugPrint('点击了确定按钮');
      },
      textCancel: '取消',
      onCancel: () {
        debugPrint('点击了取消按钮');
      },
    );
很长的内容
PandaDialog.showAlert(
      title: '''很长的标题。很长的标题。很长的标题。很长的标题。很长的标题。很长的标题。
      多行的标题
      多行的标题
      多行的标题
      多行的标题
      ''',
      content: '''
这是具体的描述。这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.这是很长的描述111.
这是多行文本。
这是多行文本。
这是多行文本。
这是多行文本。
// 这里还有个注释

这是多行文本。这是多行文本。
这是多行文本。
这是多行文本。
这是多行文本。
          ''',
      textConfirm: '''很长的确定。很长的确定。很长的确定。很长的确定。很长的确定。很长的确定。
      多行的确定。
      多行的确定。
      多行的确定。

      多行的确定。
      ''',
      onConfirm: () {
        debugPrint('点击了确定按钮');
      },
      textCancel: '''很长的取消。很长的取消。很长的取消。很长的取消。很长的取消。
      多行的取消。


      多行的取消。
      多行的取消。多行的取消。
      多行的取消。
      ''',
      onCancel: () {
        debugPrint('点击了取消按钮');
      },
    );
过长的布局导致报错

测试小结

参考文章

Dialogs

提示窗口(AlertDialog、CupertinoAlertDialog、SimpleDialog、Dialog)

上一篇下一篇

猜你喜欢

热点阅读