Skip to content

Commit

Permalink
♻️ 迁移至 plugin_platform_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesZero committed Jan 20, 2025
1 parent 5577e61 commit b164474
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_gromore_ads/flutter_gromore_ads.dart';

import 'pro_page.dart';

Expand All @@ -7,6 +8,9 @@ void main() {
WidgetsFlutterBinding.ensureInitialized();
// 启动
runApp(const MyApp());

// 初始化
init();
}

class MyApp extends StatefulWidget {
Expand All @@ -24,3 +28,20 @@ class _MyAppState extends State<MyApp> {
);
}
}

/// 初始化
Future<void> init() async {
// 设置监听
FlutterGromoreAds.onEventListener((event) {
print('🎉🎉🎉 FlutterAds ==> $event');
});

// 初始化
await FlutterGromoreAds.initAd(
'1234567890',
config: 'config.json',
);

// 展示开屏广告
await FlutterGromoreAds.showSplashAd('1234567890');
}
27 changes: 0 additions & 27 deletions example/test/widget_test.dart

This file was deleted.

59 changes: 16 additions & 43 deletions lib/flutter_gromore_ads.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
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';
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<void> onEventListener(
OnAdEventListener onAdEventListener) async {
_eventChannel.receiveBroadcastStream().listen((data) {
FlutterGromoreAdsPlatform.instance.getEventStream().listen((data) {
hanleAdEvent(data, onAdEventListener);
});
}

/// 请求应用跟踪透明度授权(仅 iOS)
static Future<bool> get requestIDFA async {
if (Platform.isIOS) {
final bool result = await _methodChannel.invokeMethod('requestIDFA');
return result;
return FlutterGromoreAdsPlatform.instance.requestIDFA();
}
return true;
}

/// 动态请求相关权限(仅 Android)
static Future<bool> get requestPermissionIfNecessary async {
if (Platform.isAndroid) {
final bool result =
await _methodChannel.invokeMethod('requestPermissionIfNecessary');
return result;
return FlutterGromoreAdsPlatform.instance.requestPermissionIfNecessary();
}
return true;
}
Expand All @@ -50,44 +37,30 @@ class FlutterGromoreAds {
/// [config] 配置文件名称
/// [limitPersonalAds] 是否限制个性化广告,0:不限制 1:限制
static Future<bool> 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;
}

/// 展示开屏广告
/// [posId] 广告位 id
/// [logo] 如果传值则展示底部logo,不传不展示,则全屏展示
/// [timeout] 加载超时时间
static Future<bool> 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<bool> showInterstitialAd(String posId) async {
final bool result = await _methodChannel.invokeMethod(
'showInterstitialAd',
{'posId': posId},
);
return result;
static Future<bool> showInterstitialAd(String posId) {
return FlutterGromoreAdsPlatform.instance.showInterstitialAd(posId);
}
}
71 changes: 71 additions & 0 deletions lib/flutter_gromore_ads_method_channel.dart
Original file line number Diff line number Diff line change
@@ -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<bool> requestIDFA() async {
final bool result = await methodChannel.invokeMethod('requestIDFA');
return result;
}

@override
Future<bool> requestPermissionIfNecessary() async {
final bool result =
await methodChannel.invokeMethod('requestPermissionIfNecessary');
return result;
}

@override
Future<bool> 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<bool> 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<bool> showInterstitialAd(String posId) async {
final bool result = await methodChannel.invokeMethod(
'showInterstitialAd',
{'posId': posId},
);
return result;
}

@override
Stream<dynamic> getEventStream() {
return eventChannel.receiveBroadcastStream();
}
}
44 changes: 44 additions & 0 deletions lib/flutter_gromore_ads_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -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<bool> requestIDFA() {
throw UnimplementedError('requestIDFA() has not been implemented.');
}

Future<bool> requestPermissionIfNecessary() {
throw UnimplementedError(
'requestPermissionIfNecessary() has not been implemented.');
}

Future<bool> initAd(String appId,
{String? config, int limitPersonalAds = 0}) {
throw UnimplementedError('initAd() has not been implemented.');
}

Future<bool> showSplashAd(String posId,
{String? logo, double timeout = 3.5}) {
throw UnimplementedError('showSplashAd() has not been implemented.');
}

Future<bool> showInterstitialAd(String posId) {
throw UnimplementedError('showInterstitialAd() has not been implemented.');
}

Stream<dynamic> getEventStream() {
throw UnimplementedError('getEventStream() has not been implemented.');
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ environment:
dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit b164474

Please sign in to comment.