From b164474995aa1502c9d37eac8f853c3c8af8f840 Mon Sep 17 00:00:00 2001 From: Zero <1300326388@qq.com> Date: Mon, 20 Jan 2025 17:18:51 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E8=87=B3=20plugin=5Fplatform=5Finterface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + example/lib/main.dart | 21 ++++++ example/test/widget_test.dart | 27 ------- lib/flutter_gromore_ads.dart | 59 +++++---------- lib/flutter_gromore_ads_method_channel.dart | 71 +++++++++++++++++++ ...lutter_gromore_ads_platform_interface.dart | 44 ++++++++++++ pubspec.yaml | 1 + 7 files changed, 154 insertions(+), 70 deletions(-) delete mode 100644 example/test/widget_test.dart create mode 100644 lib/flutter_gromore_ads_method_channel.dart create mode 100644 lib/flutter_gromore_ads_platform_interface.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f17ccb..81420f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * 适配新版本 Flutter,兼容 Android namespace * 升级 iOS SDK 到 `v5.9.1.5` * 升级 Android SDK 到 `v5.9.2.8` +* 插件迁移 `plugin_platform_interface` 方便更多平台的后续适配,比如鸿蒙 * [🏆 Pro版](https://flutterads.top/) 支持【信息流、激励视频、测试助手、实时收益、预加载】 ## 3.1.0 diff --git a/example/lib/main.dart b/example/lib/main.dart index 9426166..cc06c8b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_gromore_ads/flutter_gromore_ads.dart'; import 'pro_page.dart'; @@ -7,6 +8,9 @@ void main() { WidgetsFlutterBinding.ensureInitialized(); // 启动 runApp(const MyApp()); + + // 初始化 + init(); } class MyApp extends StatefulWidget { @@ -24,3 +28,20 @@ class _MyAppState extends State { ); } } + +/// 初始化 +Future init() async { + // 设置监听 + FlutterGromoreAds.onEventListener((event) { + print('🎉🎉🎉 FlutterAds ==> $event'); + }); + + // 初始化 + await FlutterGromoreAds.initAd( + '1234567890', + config: 'config.json', + ); + + // 展示开屏广告 + await FlutterGromoreAds.showSplashAd('1234567890'); +} diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart deleted file mode 100644 index 6e19b2c..0000000 --- a/example/test/widget_test.dart +++ /dev/null @@ -1,27 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'package:flutter_gromore_ads_example/main.dart'; - -void main() { - testWidgets('Verify Platform version', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(const MyApp()); - - // Verify that platform version is retrieved. - expect( - find.byWidgetPredicate( - (Widget widget) => - widget is Text && widget.data!.startsWith('Running on:'), - ), - findsOneWidget, - ); - }); -} diff --git a/lib/flutter_gromore_ads.dart b/lib/flutter_gromore_ads.dart index 75455b1..4cd6818 100644 --- a/lib/flutter_gromore_ads.dart +++ b/lib/flutter_gromore_ads.dart @@ -1,8 +1,5 @@ -import 'dart:async'; import 'dart:io'; - -import 'package:flutter/services.dart'; - +import 'flutter_gromore_ads_platform_interface.dart'; import 'event/ad_event_handler.dart'; export 'event/ad_event_handler.dart'; @@ -10,18 +7,11 @@ export 'view/ad_banner_widget.dart'; /// GroMore 广告插件 class FlutterGromoreAds { - // 方法通道 - static const MethodChannel _methodChannel = - MethodChannel('flutter_gromore_ads'); - // 事件通道 - static const EventChannel _eventChannel = - EventChannel('flutter_gromore_ads_event'); - ///事件回调 ///@params onData 事件回调 static Future onEventListener( OnAdEventListener onAdEventListener) async { - _eventChannel.receiveBroadcastStream().listen((data) { + FlutterGromoreAdsPlatform.instance.getEventStream().listen((data) { hanleAdEvent(data, onAdEventListener); }); } @@ -29,8 +19,7 @@ class FlutterGromoreAds { /// 请求应用跟踪透明度授权(仅 iOS) static Future get requestIDFA async { if (Platform.isIOS) { - final bool result = await _methodChannel.invokeMethod('requestIDFA'); - return result; + return FlutterGromoreAdsPlatform.instance.requestIDFA(); } return true; } @@ -38,9 +27,7 @@ class FlutterGromoreAds { /// 动态请求相关权限(仅 Android) static Future get requestPermissionIfNecessary async { if (Platform.isAndroid) { - final bool result = - await _methodChannel.invokeMethod('requestPermissionIfNecessary'); - return result; + return FlutterGromoreAdsPlatform.instance.requestPermissionIfNecessary(); } return true; } @@ -50,18 +37,12 @@ class FlutterGromoreAds { /// [config] 配置文件名称 /// [limitPersonalAds] 是否限制个性化广告,0:不限制 1:限制 static Future initAd(String appId, - {String? config, int limitPersonalAds = 0}) async { - final bool result = await _methodChannel.invokeMethod( - 'initAd', - { - 'appId': appId, - 'config': config, - 'limitPersonalAds': limitPersonalAds, - }, + {String? config, int limitPersonalAds = 0}) { + return FlutterGromoreAdsPlatform.instance.initAd( + appId, + config: config, + limitPersonalAds: limitPersonalAds, ); - print( - "🎉🎉🎉 FlutterAds ==> 初始化完成,推荐使用 GroMore Pro 版本,获得更高的收益:https://flutterads.top/"); - return result; } /// 展示开屏广告 @@ -69,25 +50,17 @@ class FlutterGromoreAds { /// [logo] 如果传值则展示底部logo,不传不展示,则全屏展示 /// [timeout] 加载超时时间 static Future showSplashAd(String posId, - {String? logo, double timeout = 3.5}) async { - final bool result = await _methodChannel.invokeMethod( - 'showSplashAd', - { - 'posId': posId, - 'logo': logo, - 'timeout': timeout, - }, + {String? logo, double timeout = 3.5}) { + return FlutterGromoreAdsPlatform.instance.showSplashAd( + posId, + logo: logo, + timeout: timeout, ); - return result; } /// 展示插屏广告 /// [posId] 广告位 id - static Future showInterstitialAd(String posId) async { - final bool result = await _methodChannel.invokeMethod( - 'showInterstitialAd', - {'posId': posId}, - ); - return result; + static Future showInterstitialAd(String posId) { + return FlutterGromoreAdsPlatform.instance.showInterstitialAd(posId); } } diff --git a/lib/flutter_gromore_ads_method_channel.dart b/lib/flutter_gromore_ads_method_channel.dart new file mode 100644 index 0000000..b4a7ad9 --- /dev/null +++ b/lib/flutter_gromore_ads_method_channel.dart @@ -0,0 +1,71 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'flutter_gromore_ads_platform_interface.dart'; + +class MethodChannelFlutterGromoreAds extends FlutterGromoreAdsPlatform { + @visibleForTesting + final methodChannel = const MethodChannel('flutter_gromore_ads'); + + @visibleForTesting + final eventChannel = const EventChannel('flutter_gromore_ads_event'); + + @override + Future requestIDFA() async { + final bool result = await methodChannel.invokeMethod('requestIDFA'); + return result; + } + + @override + Future requestPermissionIfNecessary() async { + final bool result = + await methodChannel.invokeMethod('requestPermissionIfNecessary'); + return result; + } + + @override + Future initAd(String appId, + {String? config, int limitPersonalAds = 0}) async { + final bool result = await methodChannel.invokeMethod( + 'initAd', + { + 'appId': appId, + 'config': config, + 'limitPersonalAds': limitPersonalAds, + }, + ); + print( + "🎉🎉🎉 FlutterAds ==> 初始化完成,推荐使用 GroMore Pro 版本,获得更高的收益:https://flutterads.top/"); + //打印购买正版 + print( + "🎉🎉🎉 FlutterAds ==> 请购买支持正版,否则可能出现上线后广告不加载的情况,避免收益下降、不结算等情况,请访问:https://flutterads.top/"); + return result; + } + + @override + Future showSplashAd(String posId, + {String? logo, double timeout = 3.5}) async { + final bool result = await methodChannel.invokeMethod( + 'showSplashAd', + { + 'posId': posId, + 'logo': logo, + 'timeout': timeout, + }, + ); + return result; + } + + @override + Future showInterstitialAd(String posId) async { + final bool result = await methodChannel.invokeMethod( + 'showInterstitialAd', + {'posId': posId}, + ); + return result; + } + + @override + Stream getEventStream() { + return eventChannel.receiveBroadcastStream(); + } +} diff --git a/lib/flutter_gromore_ads_platform_interface.dart b/lib/flutter_gromore_ads_platform_interface.dart new file mode 100644 index 0000000..c3c5046 --- /dev/null +++ b/lib/flutter_gromore_ads_platform_interface.dart @@ -0,0 +1,44 @@ +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'flutter_gromore_ads_method_channel.dart'; + +abstract class FlutterGromoreAdsPlatform extends PlatformInterface { + FlutterGromoreAdsPlatform() : super(token: _token); + + static final Object _token = Object(); + + static FlutterGromoreAdsPlatform _instance = MethodChannelFlutterGromoreAds(); + + static FlutterGromoreAdsPlatform get instance => _instance; + + static set instance(FlutterGromoreAdsPlatform instance) { + PlatformInterface.verifyToken(instance, _token); + _instance = instance; + } + + Future requestIDFA() { + throw UnimplementedError('requestIDFA() has not been implemented.'); + } + + Future requestPermissionIfNecessary() { + throw UnimplementedError( + 'requestPermissionIfNecessary() has not been implemented.'); + } + + Future initAd(String appId, + {String? config, int limitPersonalAds = 0}) { + throw UnimplementedError('initAd() has not been implemented.'); + } + + Future showSplashAd(String posId, + {String? logo, double timeout = 3.5}) { + throw UnimplementedError('showSplashAd() has not been implemented.'); + } + + Future showInterstitialAd(String posId) { + throw UnimplementedError('showInterstitialAd() has not been implemented.'); + } + + Stream getEventStream() { + throw UnimplementedError('getEventStream() has not been implemented.'); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 4633241..a6e831e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ environment: dependencies: flutter: sdk: flutter + plugin_platform_interface: ^2.0.2 dev_dependencies: flutter_test: