Flutter-function

 

在Dart中不存在内部类,所以很多时候我们需要回调无法直接使用内部类。

所以我们引入function,function有参数的我们定义dynamic主要是为了实现泛型调用原理。

老规矩,先上代码:

import 'package:yxk_app/net/request_util.dart';

/// 方法定义类型(用于接口回调中使用)

/// 无参数
typedef ActionNoParam = void Function();

/// 一个参数
typedef ActionOneParam = void Function(dynamic t);

///两个参数
typedef ActionTwoParam = void Function(dynamic o, dynamic t);

/// 请求成功
typedef ResponceSuccess = void Function(dynamic t);

/// 请求失败
typedef ResponceError = void Function(LogicError error);

使用说明:

PermissionUtils.showDialog(cxt, "权限提醒",
        "使用易小康之前,需要开启\"电话权限\"、\"短信权限\"、\"存储权限\",以确保账号登录安全和信息安全。\n\n请在设置-应用-易小康-权限中开启相关权限。",
        () {
      isShowDialog = false;
      Navigator.pop(cxt);
      PermissionHandler().openAppSettings();
    }, () {
      SystemChannels.platform.invokeMethod('SystemNavigator.pop');
    });
/// 权限提示对话款
static showDialog(BuildContext cxt, String title, String content,
      ActionNoParam ok, ActionNoParam cancel) {
    showCupertinoDialog<int>(
        context: cxt,
        builder: (cxt) {
          return CupertinoAlertDialog(
            title: Text(title),
            content: Text(content),
            actions: <Widget>[
              CupertinoDialogAction(
                child: Text("去开启"),
                onPressed: () {
                  ok();
                },
              ),
              CupertinoDialogAction(
                child: Text("取消"),
                onPressed: () {
                  cancel();
                },
              )
            ],
          );
        });
  }
posted @ 2020-08-24 16:57  sundaysandroid  阅读(704)  评论(0编辑  收藏  举报