native_test_2 0.0.1
native_test_2: ^0.0.1 copied to clipboard
广告插件
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:native_test_2/native_test_2.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _nativeTest_2Plugin = NativeTest_2();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await _nativeTest_2Plugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
bool registerAdResult = false;
showAd() async {
NativeTestStream.initAdStream(
natuveTestRewardAdCallBack: NatuveTestRewardAdCallBack(
onShow: () {
print('flutter-onShow函数');
// print("$info");
},
onClick: () {
print('flutter-onClick函数');
},
onFail: (message) {
print('flutter-onFail函数-----$message');
},
onClose: () {
print('flutter-onClose函数');
},
onReward: () {
print('flutter-onReward函数,奖励发放函数');
},
adInfo: (info) {
print('flutter-info函数,${info}');
print(info);
},
onCache: () {
print('flutter-onCache函数');
Future.delayed(const Duration(seconds: 2), () async {
print('延迟执行');
await _nativeTest_2Plugin.showAd();
});
},
onUnReady: () {
print('flutter-onUnReady函数');
},
));
await _nativeTest_2Plugin.loadAd(adCodeId: "2323541");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
children: [
Center(
child: Text('Running on: $_platformVersion\n'),
),
ElevatedButton(
onPressed: () async {
registerAdResult = await _nativeTest_2Plugin.registerAd(
appId: "767881",
appName: "广告demo",
tanxAppKey: "",
channelName: "demoTest");
setState(() {});
},
child: const Text('初始化')),
Text('初始化结果${registerAdResult ? "成功" : "失败"}'),
ElevatedButton(
onPressed: () async {
await _nativeTest_2Plugin.loadAd(adCodeId: "2323541");
},
child: const Text('预加载广告')),
ElevatedButton(
onPressed: () async {
await _nativeTest_2Plugin.showAd();
},
child: const Text('展示广告')),
ElevatedButton(
onPressed: () async {
showAd();
},
child: const Text('测试完整流程')),
],
)),
);
}
}