Skip to content

Commit b164474

Browse files
committed
♻️ 迁移至 plugin_platform_interface
1 parent 5577e61 commit b164474

File tree

7 files changed

+154
-70
lines changed

7 files changed

+154
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* 适配新版本 Flutter,兼容 Android namespace
44
* 升级 iOS SDK 到 `v5.9.1.5`
55
* 升级 Android SDK 到 `v5.9.2.8`
6+
* 插件迁移 `plugin_platform_interface` 方便更多平台的后续适配,比如鸿蒙
67
* [🏆 Pro版](https://flutterads.top/) 支持【信息流、激励视频、测试助手、实时收益、预加载】
78

89
## 3.1.0

example/lib/main.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_gromore_ads/flutter_gromore_ads.dart';
23

34
import 'pro_page.dart';
45

@@ -7,6 +8,9 @@ void main() {
78
WidgetsFlutterBinding.ensureInitialized();
89
// 启动
910
runApp(const MyApp());
11+
12+
// 初始化
13+
init();
1014
}
1115

1216
class MyApp extends StatefulWidget {
@@ -24,3 +28,20 @@ class _MyAppState extends State<MyApp> {
2428
);
2529
}
2630
}
31+
32+
/// 初始化
33+
Future<void> init() async {
34+
// 设置监听
35+
FlutterGromoreAds.onEventListener((event) {
36+
print('🎉🎉🎉 FlutterAds ==> $event');
37+
});
38+
39+
// 初始化
40+
await FlutterGromoreAds.initAd(
41+
'1234567890',
42+
config: 'config.json',
43+
);
44+
45+
// 展示开屏广告
46+
await FlutterGromoreAds.showSplashAd('1234567890');
47+
}

example/test/widget_test.dart

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/flutter_gromore_ads.dart

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,33 @@
1-
import 'dart:async';
21
import 'dart:io';
3-
4-
import 'package:flutter/services.dart';
5-
2+
import 'flutter_gromore_ads_platform_interface.dart';
63
import 'event/ad_event_handler.dart';
74

85
export 'event/ad_event_handler.dart';
96
export 'view/ad_banner_widget.dart';
107

118
/// GroMore 广告插件
129
class FlutterGromoreAds {
13-
// 方法通道
14-
static const MethodChannel _methodChannel =
15-
MethodChannel('flutter_gromore_ads');
16-
// 事件通道
17-
static const EventChannel _eventChannel =
18-
EventChannel('flutter_gromore_ads_event');
19-
2010
///事件回调
2111
///@params onData 事件回调
2212
static Future<void> onEventListener(
2313
OnAdEventListener onAdEventListener) async {
24-
_eventChannel.receiveBroadcastStream().listen((data) {
14+
FlutterGromoreAdsPlatform.instance.getEventStream().listen((data) {
2515
hanleAdEvent(data, onAdEventListener);
2616
});
2717
}
2818

2919
/// 请求应用跟踪透明度授权(仅 iOS)
3020
static Future<bool> get requestIDFA async {
3121
if (Platform.isIOS) {
32-
final bool result = await _methodChannel.invokeMethod('requestIDFA');
33-
return result;
22+
return FlutterGromoreAdsPlatform.instance.requestIDFA();
3423
}
3524
return true;
3625
}
3726

3827
/// 动态请求相关权限(仅 Android)
3928
static Future<bool> get requestPermissionIfNecessary async {
4029
if (Platform.isAndroid) {
41-
final bool result =
42-
await _methodChannel.invokeMethod('requestPermissionIfNecessary');
43-
return result;
30+
return FlutterGromoreAdsPlatform.instance.requestPermissionIfNecessary();
4431
}
4532
return true;
4633
}
@@ -50,44 +37,30 @@ class FlutterGromoreAds {
5037
/// [config] 配置文件名称
5138
/// [limitPersonalAds] 是否限制个性化广告,0:不限制 1:限制
5239
static Future<bool> initAd(String appId,
53-
{String? config, int limitPersonalAds = 0}) async {
54-
final bool result = await _methodChannel.invokeMethod(
55-
'initAd',
56-
{
57-
'appId': appId,
58-
'config': config,
59-
'limitPersonalAds': limitPersonalAds,
60-
},
40+
{String? config, int limitPersonalAds = 0}) {
41+
return FlutterGromoreAdsPlatform.instance.initAd(
42+
appId,
43+
config: config,
44+
limitPersonalAds: limitPersonalAds,
6145
);
62-
print(
63-
"🎉🎉🎉 FlutterAds ==> 初始化完成,推荐使用 GroMore Pro 版本,获得更高的收益:https://flutterads.top/");
64-
return result;
6546
}
6647

6748
/// 展示开屏广告
6849
/// [posId] 广告位 id
6950
/// [logo] 如果传值则展示底部logo,不传不展示,则全屏展示
7051
/// [timeout] 加载超时时间
7152
static Future<bool> showSplashAd(String posId,
72-
{String? logo, double timeout = 3.5}) async {
73-
final bool result = await _methodChannel.invokeMethod(
74-
'showSplashAd',
75-
{
76-
'posId': posId,
77-
'logo': logo,
78-
'timeout': timeout,
79-
},
53+
{String? logo, double timeout = 3.5}) {
54+
return FlutterGromoreAdsPlatform.instance.showSplashAd(
55+
posId,
56+
logo: logo,
57+
timeout: timeout,
8058
);
81-
return result;
8259
}
8360

8461
/// 展示插屏广告
8562
/// [posId] 广告位 id
86-
static Future<bool> showInterstitialAd(String posId) async {
87-
final bool result = await _methodChannel.invokeMethod(
88-
'showInterstitialAd',
89-
{'posId': posId},
90-
);
91-
return result;
63+
static Future<bool> showInterstitialAd(String posId) {
64+
return FlutterGromoreAdsPlatform.instance.showInterstitialAd(posId);
9265
}
9366
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/services.dart';
3+
import 'flutter_gromore_ads_platform_interface.dart';
4+
5+
class MethodChannelFlutterGromoreAds extends FlutterGromoreAdsPlatform {
6+
@visibleForTesting
7+
final methodChannel = const MethodChannel('flutter_gromore_ads');
8+
9+
@visibleForTesting
10+
final eventChannel = const EventChannel('flutter_gromore_ads_event');
11+
12+
@override
13+
Future<bool> requestIDFA() async {
14+
final bool result = await methodChannel.invokeMethod('requestIDFA');
15+
return result;
16+
}
17+
18+
@override
19+
Future<bool> requestPermissionIfNecessary() async {
20+
final bool result =
21+
await methodChannel.invokeMethod('requestPermissionIfNecessary');
22+
return result;
23+
}
24+
25+
@override
26+
Future<bool> initAd(String appId,
27+
{String? config, int limitPersonalAds = 0}) async {
28+
final bool result = await methodChannel.invokeMethod(
29+
'initAd',
30+
{
31+
'appId': appId,
32+
'config': config,
33+
'limitPersonalAds': limitPersonalAds,
34+
},
35+
);
36+
print(
37+
"🎉🎉🎉 FlutterAds ==> 初始化完成,推荐使用 GroMore Pro 版本,获得更高的收益:https://flutterads.top/");
38+
//打印购买正版
39+
print(
40+
"🎉🎉🎉 FlutterAds ==> 请购买支持正版,否则可能出现上线后广告不加载的情况,避免收益下降、不结算等情况,请访问:https://flutterads.top/");
41+
return result;
42+
}
43+
44+
@override
45+
Future<bool> showSplashAd(String posId,
46+
{String? logo, double timeout = 3.5}) async {
47+
final bool result = await methodChannel.invokeMethod(
48+
'showSplashAd',
49+
{
50+
'posId': posId,
51+
'logo': logo,
52+
'timeout': timeout,
53+
},
54+
);
55+
return result;
56+
}
57+
58+
@override
59+
Future<bool> showInterstitialAd(String posId) async {
60+
final bool result = await methodChannel.invokeMethod(
61+
'showInterstitialAd',
62+
{'posId': posId},
63+
);
64+
return result;
65+
}
66+
67+
@override
68+
Stream<dynamic> getEventStream() {
69+
return eventChannel.receiveBroadcastStream();
70+
}
71+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
2+
import 'flutter_gromore_ads_method_channel.dart';
3+
4+
abstract class FlutterGromoreAdsPlatform extends PlatformInterface {
5+
FlutterGromoreAdsPlatform() : super(token: _token);
6+
7+
static final Object _token = Object();
8+
9+
static FlutterGromoreAdsPlatform _instance = MethodChannelFlutterGromoreAds();
10+
11+
static FlutterGromoreAdsPlatform get instance => _instance;
12+
13+
static set instance(FlutterGromoreAdsPlatform instance) {
14+
PlatformInterface.verifyToken(instance, _token);
15+
_instance = instance;
16+
}
17+
18+
Future<bool> requestIDFA() {
19+
throw UnimplementedError('requestIDFA() has not been implemented.');
20+
}
21+
22+
Future<bool> requestPermissionIfNecessary() {
23+
throw UnimplementedError(
24+
'requestPermissionIfNecessary() has not been implemented.');
25+
}
26+
27+
Future<bool> initAd(String appId,
28+
{String? config, int limitPersonalAds = 0}) {
29+
throw UnimplementedError('initAd() has not been implemented.');
30+
}
31+
32+
Future<bool> showSplashAd(String posId,
33+
{String? logo, double timeout = 3.5}) {
34+
throw UnimplementedError('showSplashAd() has not been implemented.');
35+
}
36+
37+
Future<bool> showInterstitialAd(String posId) {
38+
throw UnimplementedError('showInterstitialAd() has not been implemented.');
39+
}
40+
41+
Stream<dynamic> getEventStream() {
42+
throw UnimplementedError('getEventStream() has not been implemented.');
43+
}
44+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ environment:
1515
dependencies:
1616
flutter:
1717
sdk: flutter
18+
plugin_platform_interface: ^2.0.2
1819

1920
dev_dependencies:
2021
flutter_test:

0 commit comments

Comments
 (0)