# 常用包

# flutter screenutil

# 安装

flutter pub add flutter_screenutil
1

# 使用

return ScreenUtilInit(
    designSize: const Size(
      375,
      667.0,
    ),
    builder: (BuildContext context, Widget? child) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      );
    },
  );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# flutter intl

# fluro

# provider

# dio

# fluwx

# 打开微信小程序

fluwx.launchWeChatMiniProgram(
  username: GlobalConfig.wecharMinPromgram, // 小程序原始ID
  path: "/", //小程序起始路径
);
1
2
3
4

# cached network image

# 使用方法

CachedNetworkImage(
      imageUrl: "http://via.placeholder.com/350x150",
      progressIndicatorBuilder: (context, url, downloadProgress) => 
              CircularProgressIndicator(value: downloadProgress.progress),
      errorWidget: (context, url, error) => Icon(Icons.error),
    ),
1
2
3
4
5
6

# flutter svg

# pie_chart

# platform device id

# 安装

flutter pub add platform_device_id
1

# 使用

String? deviceId;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
  deviceId = await PlatformDeviceId.getDeviceId;
} on PlatformException {
  deviceId = 'Failed to get deviceId.';
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
  _deviceId = deviceId;
  print("deviceId->$_deviceId");
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# url launcher

# json 序列化

# 安装

flutter pub add dev:build_runner dev:json_serializable json_annotation

1
2

# 建立 model 类

import 'package:json_annotation/json_annotation.dart';

part 'result.g.dart';

()
class Result {
  //{"code": 0, "method": "GET", "requestPrams": "shenlong"}
  int code;
  String method;
  String requestPrams;

  Result(this.code, this.method, this.requestPrams);

  // 固定格式,不同的类使用不同的mixin即可
  // 这里声明一个工厂构造方法
  factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);

  Map<String, dynamic> toJson() => _$ResultToJson(this);
}
// 1)@JsonSerializable() :表示当前类需要被 json_serializable 处理;
// 2)part 'result.g.dart':这里的 part 表示当前文件(result.dart)关联 result.g.dart,其中 result.g.dart 命名规范是:文件名称.g.dart;
// 3) _$ResultFromJson :是 json_serializable 帮我们生成的将 Map 转为实体类的方法,这个方法的命名规则:_$+当前类名+FromJson;
// 4)_$ResultToJson :是 json_serializable 帮我们生成的将实体类转为 Map 的方法,这个方法的命名规则:_$+当前类名+ToJson;
// 5)factory Result.fromJson : 这里是声明了工厂构造函数 fromJson ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 生对成应的代码

flutter pub run build_runner build  --delete-conflicting-outputs  
1

# json生成模型

json2dart (opens new window)

# permission_handler 检查权限

# 安装

flutter pub add permission_handler
1

# Ios设置

// ios/Runner/Info.plist
// ...
<dict>
    // ...其它设置
 
    <!-- 通知推送 -->
    <key>PermissionGroupNotification</key>
    <string>Notification</string>
 
    // ...其它设置
</dict>
1
2
3
4
5
6
7
8
9
10
11

在 ios/Podfile 文件中加入想要获取的权限列表

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
 
    # 以上为flutter自动生成的一些配置
 
    # 加入以下权限申请列表代码
    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
 
        ## 通知推送
        'PERMISSION_NOTIFICATIONS=1',
      ]
 
    end 
    # 权限申请列表结束
 
  end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

如果是“通知推送”权限,需要在 AppDelegate.swift 文件中进行如下操作,其它权限不需要

@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // ....
    
    // **加入以下内容
    // This is required to make any communication available in the action isolate.
    FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
        GeneratedPluginRegistrant.register(with: registry)
    }
    // 根据设计,iOS应用程序在应用程序处于前台时不显示通知,除非配置为这样。
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
    }
    // **加入以上内容
    
    // ...
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 操作

# 获取权限状态


var status = await Permission.notification.status;
print(status);
1
2
3

status有以下几种值:

  • isDenied:
    • 在 iOS 上代表还未申请权限;
    • 在android上代表还未申请权限或之前拒绝了权限。
  • isGranted:拥有全部权限。
  • isLimited:拥有部分权限。
  • isRestricted:拥有部分权限(仅限iOS)。
  • isPermanentlyDenied:权限已被永久拒绝。

# 申请权限

await Permission.notification.request();
 
// 同时申请多个权限:
await [
  Permission.notification,
  Permission.photos,
].request();
1
2
3
4
5
6
7

android:申请权限时, 如果用户选择"拒绝",status将保持为isDenied,以后可以再次调用request()来取得权限; 如果用户选择"拒绝并不再询问",status将变为isPermanentlyDenied,即以后无法再调用request()来取得权限,必须去应用设置界面手动开启权限。

iOS:首次申请权限时,如果用户选择了"拒绝",status将变为isPermanentlyDenied,即以后无法再调用request()来取得权限,必须去应用设置界面手动开启权限。

跳转到应用设置界面

openAppSettings();
1