bjmob_sdk 0.0.1
bjmob_sdk: ^0.0.1 copied to clipboard
A new Flutter plugin.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:bjmob_sdk/bjmob_flutter_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BJMOB SDK 示例',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'BJMOB SDK 示例'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isAdLoaded = false;
String _adStatus = '未加载';
bool _isLoading = false;
@override
void initState() {
super.initState();
print('MyHomePage initState 被调用');
// 使用 Future.microtask 确保在下一个微任务中执行初始化
Future.microtask(() async {
try {
print('开始调用 _initSDK...');
await _initSDK();
print('_initSDK 调用完成');
} catch (e) {
print('在 initState 中调用 _initSDK 时发生错误: $e');
}
});
}
Future<void> _initSDK() async {
try {
print('开始初始化SDK...');
setState(() {
_isLoading = true;
_adStatus = '正在初始化...';
});
print('调用 BjmobFlutterPlugin.init...');
try {
await BjmobFlutterPlugin.init(
appId: '69lSe9cvORWnd62o',
);
print('BjmobFlutterPlugin.init 调用完成');
} catch (e) {
print('BjmobFlutterPlugin.init 调用失败: $e');
throw e;
}
setState(() {
_adStatus = '初始化成功,等待SDK就绪...';
});
print('等待SDK就绪...');
try {
// 增加等待时间到3秒,确保SDK完全初始化
await Future.delayed(const Duration(seconds: 3));
print('SDK就绪,准备加载广告');
} catch (e) {
print('等待SDK就绪时发生错误: $e');
throw e;
}
setState(() {
_adStatus = 'SDK就绪,正在加载广告...';
});
print('开始调用 _loadBannerAd...');
try {
// 直接调用 _loadBannerAd,不使用 Future.microtask
await _loadBannerAd();
print('_loadBannerAd 调用完成');
} catch (e) {
print('_loadBannerAd 调用失败: $e');
throw e;
}
} catch (e, stackTrace) {
print('初始化失败: $e');
print('错误堆栈: $stackTrace');
setState(() {
_isLoading = false;
_adStatus = '初始化失败: $e';
});
}
}
Future<void> _loadBannerAd() async {
try {
print('开始加载横幅广告...');
setState(() {
_isLoading = true;
_adStatus = '正在加载广告...';
_isAdLoaded = false; // 重置广告加载状态
});
print('调用 loadBannerAd 方法...');
try {
await BjmobFlutterPlugin.loadBannerAd(
width: 320,
height: 50,
onAdLoaded: (info) {
print('广告加载成功: $info');
if (mounted) {
setState(() {
_isAdLoaded = true;
_isLoading = false;
_adStatus = '广告加载成功';
});
}
},
onAdFailedToLoad: (error) {
print('广告加载失败: $error');
if (mounted) {
setState(() {
_isAdLoaded = false;
_isLoading = false;
_adStatus = '广告加载失败: ${error['error']} (错误码: ${error['code']})';
});
}
},
onAdExposure: () {
print('广告已曝光');
if (mounted) {
setState(() {
_isLoading = false;
_adStatus = '广告已曝光';
});
}
},
onAdClicked: () {
print('广告被点击');
if (mounted) {
setState(() {
_isLoading = false;
_adStatus = '广告被点击';
});
}
},
onAdClosed: () {
print('广告已关闭');
if (mounted) {
setState(() {
_isLoading = false;
_adStatus = '广告已关闭';
});
}
},
);
print('loadBannerAd 方法调用完成');
} catch (e) {
print('loadBannerAd 方法调用失败: $e');
throw e;
}
} catch (e, stackTrace) {
print('加载广告时发生错误: $e');
print('错误堆栈: $stackTrace');
setState(() {
_isAdLoaded = false;
_isLoading = false;
_adStatus = '加载广告失败: $e';
});
}
}
@override
void dispose() {
BjmobFlutterPlugin.destroyBannerAd();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'广告状态:',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(_adStatus),
if (_isLoading) ...[
const SizedBox(height: 8),
const LinearProgressIndicator(),
],
],
),
),
),
const SizedBox(height: 16),
if (_isAdLoaded) ...[
ElevatedButton(
onPressed: () async {
try {
await BjmobFlutterPlugin.showBannerAd();
setState(() {
_adStatus = '广告已显示';
});
} catch (e) {
setState(() {
_adStatus = '显示广告失败: $e';
});
}
},
child: const Text('显示广告'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
try {
await BjmobFlutterPlugin.hideBannerAd();
setState(() {
_adStatus = '广告已隐藏';
});
} catch (e) {
setState(() {
_adStatus = '隐藏广告失败: $e';
});
}
},
child: const Text('隐藏广告'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
try {
await BjmobFlutterPlugin.destroyBannerAd();
setState(() {
_isAdLoaded = false;
_adStatus = '广告已销毁';
});
} catch (e) {
setState(() {
_adStatus = '销毁广告失败: $e';
});
}
},
child: const Text('销毁广告'),
),
] else ...[
ElevatedButton(
onPressed: _isLoading ? null : _loadBannerAd,
child: Text(_isLoading ? '加载中...' : '加载广告'),
),
],
],
),
),
);
}
}