ipdsdk_android 1.0.120
ipdsdk_android: ^1.0.120 copied to clipboard
IPDSDK Android plugin. Support SplashAd\RewardVideo\InterstitialAd and other contents. Required Flutter version >=4.0.0 and SDK>=2.17.0.
example/lib/main.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ipd_android_example/ad/privacy_settings.dart';
import 'package:ipd_android_example/data/data_store.dart';
import 'package:ipdsdk_android/ipd_android.dart';
import 'package:ipdsdk_android/event/event_action.dart';
import 'data/test_pos_id.dart';
import 'widget/main_list.dart';
import 'widget/navigator_util.dart';
void main() {
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
/// 当前IPD版本
String _sdkVersion = "";
/// 隐私政策配置
final DataStore _dataStore = DataStore.getInstance();
@override
void initState() {
super.initState();
// 初始化SDK
SchedulerBinding.instance.addPostFrameCallback((_) async {
// 获取SDK版本号
await initSdkVersion();
await _dataStore.init();
initSdk();
});
}
/// 获取当前的SDK版本
Future<void> initSdkVersion() async {
String ret;
try {
ret = await IPDAndroid.sdkVersion ?? 'Unknown';
} on PlatformException {
ret = 'Failed';
}
setState(() {
_sdkVersion = ret;
});
}
/// 初始化SDK
void initSdk() {
IPDAndroid.initWithoutStart(TestPosId.appId,
customController: _dataStore.controller);
// 判断用户是否同意了隐私政策
bool _canStart = _dataStore.isAgreeUserPrivacy;
if (_canStart) {
// 用于已同意隐私政策,调用启动方法,并在成功回调后调用广告
start();
} else {
showPrivacyDialog(context);
}
}
/// 启动SDK
void start() {
IPDAndroid.start(onStartListener: (ret) {
if (ret.action == IPDEventAction.startSuccess) {
Fluttertoast.showToast(msg: "初始化成功");
} else {
Fluttertoast.showToast(msg: "初始化失败:" + ret.msg!);
if (kDebugMode) {
print("${ret.action}:${ret.code}-${ret.extra}");
}
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
appBarTheme: const AppBarTheme(
backgroundColor: Color(0xFFB22222),
centerTitle: false,
titleTextStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20),
),
colorScheme: const ColorScheme.light(primary: Color(0xFFB22222))),
home: Scaffold(
appBar: AppBar(
title: Text("IPDFlutter:$_sdkVersion"),
centerTitle: false,
actions: [
Container(
padding: const EdgeInsets.only(right: 10),
child: PopupMenuButton(
child: const Icon(Icons.more_vert, weight: 25, color: Colors.white,),
itemBuilder: (BuildContext context) {
return <PopupMenuItem<String>>[
PopupMenuItem(child: _buildPopupMenuItem("隐私配置", 0)),
PopupMenuItem(child: _buildPopupMenuItem("个性化推荐", 1)),
];
},
),
)
],
),
body: Container(
color: Colors.white,
child: const Column(
children: [
SizedBox(height: 4),
Expanded(child: MainList()),
SizedBox(height: 4),
],
),
),
),
);
}
Widget _buildPopupMenuItem(String title, int index) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return InkWell(
onTap: () {
Navigator.of(context).pop();
if (index == 0) {
NavigatorUtil.pushNames(
context, const PrivacySettings(), "", "");
} else if (index == 1) {
onPersonalRecommendChanged(setState);
}
},
child: Row(
children: [
Container(
height: 50,
alignment: Alignment.center,
child: Text(title,
style: const TextStyle(color: Colors.black))),
if (index == 1)
Checkbox(
value: _dataStore.canUsePersonalRecommend,
onChanged: (bool? newValue) {
onPersonalRecommendChanged(setState);
},
activeColor: const Color(0xFFB22222),
),
],
));
},
);
}
void onPersonalRecommendChanged(StateSetter setState) {
setState(() {
_dataStore.onPersonalRecommendChanged();
IPDAndroid.setPersonalRecommend(_dataStore.canUsePersonalRecommend);
});
}
void showPrivacyDialog(BuildContext context) {
showDialog(
barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
context: context,
builder: (context) {
return PopScope(
canPop: false,
child: AlertDialog(
title: const Text("APP隐私政策与用户协议"),
content: const Text("模拟隐私政策与用户协议的对话框"),
actions: [
TextButton(
onPressed: () {
Fluttertoast.showToast(msg: "未同意隐私政策,广告无法正常加载");
Navigator.of(context).pop();
},
child: const Text("不同意")),
TextButton(
onPressed: () {
start();
_dataStore.onAgreeUserPrivacy();
Navigator.of(context).pop();
},
child: const Text("同意"))
],
));
});
}
}