bigo_ads 1.0.0 bigo_ads: ^1.0.0 copied to clipboard
BigoADS Flutter Plugin for Android and iOS - with support for interstitial ads, rewarded ads, splash ads, banners, and native ads.
import 'dart:async';
import 'dart:io';
import 'package:bigo_ads/bigo_ads.dart';
import 'package:bigo_ads_example/interstitial_ad_page.dart';
import 'package:bigo_ads_example/native_ad_page.dart';
import 'package:bigo_ads_example/popup_ad_page.dart';
import 'package:bigo_ads_example/reward_ad_page.dart';
import 'package:bigo_ads_example/splash_ad_app_show_page.dart';
import 'package:bigo_ads_example/splash_ad_sdk_show_page.dart';
import 'package:flutter/material.dart';
import 'banner_ad_page.dart';
void main() {
runApp(const MaterialApp(
title: 'BigoAds example app',
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}):super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _bigoAdSdkVersionName = 'Unknown';
String _bigoAdSdkVersionCode = 'Unknown';
BigoAdConsentOptions _options = BigoAdConsentOptions.gdpr;
bool _consent = true;
final countryController = TextEditingController();
final hostController = TextEditingController();
bool _bigoAdSdkInited = false;
@override
void initState() {
super.initState();
getBigoAdSdkVersion();
}
Future<void> getBigoAdSdkVersion() async {
String versionName = await BigoAdSdk.instance.getSDKVersionName() ??
'Unknown sdk version name';
String versionCode =
await BigoAdSdk.instance.getSDKVersion() ?? 'Unknown sdk version code';
setState(() {
_bigoAdSdkVersionName = versionName;
_bigoAdSdkVersionCode = versionCode;
});
}
Future<void> initBigoAdSdk() async {
BigoAdConfig adConfig = BigoAdConfig(
appKey: Platform.isAndroid ? '10182906' : '10048891',
channel: 'flutter',
isDebug: true,
age: 20,
gender: BigoAdGender.male,
activatedTime: DateTime(2023).millisecondsSinceEpoch);
adConfig.addExtra(
key: 'test_init_extra_key', value: 'test_init_extra_value');
print('BigoAdSdk, initBigoAdSdk adConfig=${adConfig.toMap()}');
await BigoAdSdk.instance.initialize(adConfig);
if (!mounted) return;
setState(() {
_bigoAdSdkInited = true;
});
}
Future<void> setUserConsent(
{required BigoAdConsentOptions options, required bool consent}) async {
print('BigoAdSdk, setUserConsent options=$_options, consent=$_consent.');
BigoAdSdk.instance.setUserConsent(_options, _consent);
}
Future<void> addExtraHost(
{required String country, required String host}) async {
String country = countryController.text;
String host = hostController.text;
if (country.isEmpty || host.isEmpty) {
print('BigoAdSdk, addExtraHost error, country=$country, host=$host.');
return;
}
print('BigoAdSdk, addExtraHost country=$country, host=$host.');
BigoAdSdk.instance.addExtraHost(country, host);
}
Future<void> getBidderToken() async {
String? token = await BigoAdSdk.instance.getBidderToken();
print('BigoAdSdk, getBidderToken length=${token?.length}, token=$token.');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BigoAds example app'),
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 16, 8, 8),
child: Row(
children: [
Text(
'Bigo Ad Sdk \nPlatform: ${Platform.isAndroid ? 'Android' : (Platform.isIOS ? 'iOS' : 'Unknown')}\nVerName: $_bigoAdSdkVersionName\nVerCode: $_bigoAdSdkVersionCode')
],
),
),
const Padding(
padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
child: Divider(
height: 1,
color: Color.fromARGB(0xFF, 0x88, 0x88, 0x88),
),
),
Row(
children: [
const Padding(
padding: EdgeInsets.fromLTRB(8, 0, 10, 0),
child: Text('OPTIONS:'),
),
DropdownButton(
value: _options,
items: <BigoAdConsentOptions>[
BigoAdConsentOptions.gdpr,
BigoAdConsentOptions.ccpa,
BigoAdConsentOptions.coppa,
BigoAdConsentOptions.lgpd
]
.map((e) => DropdownMenuItem(
value: e, child: Text(e.options.toUpperCase())))
.toList(),
onChanged: (BigoAdConsentOptions? newValue) {
setState(() {
_options = newValue ?? BigoAdConsentOptions.gdpr;
});
},
),
const Padding(
padding: EdgeInsets.fromLTRB(28, 0, 10, 0),
child: Text('CONSENT:'),
),
DropdownButton(
value: _consent,
items: <bool>[true, false]
.map((e) => DropdownMenuItem(
value: e, child: Text(e.toString())))
.toList(),
onChanged: (bool? newValue) {
setState(() {
_consent = newValue ?? true;
});
},
),
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: ElevatedButton(
onPressed: () {
setUserConsent(options: _options, consent: _consent);
},
child: const Text('UPDATE USE CONSENT')),
),
],
),
const Padding(
padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
child: Divider(
height: 1,
color: Color.fromARGB(0xFF, 0x88, 0x88, 0x88),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: Row(
children: [
SizedBox(
width: 120,
height: 50,
child: TextField(
textAlignVertical: TextAlignVertical.center,
decoration: const InputDecoration(
border: OutlineInputBorder(), hintText: 'country'),
controller: countryController,
enabled: true,
),
),
const SizedBox(
width: 10,
height: 50,
),
SizedBox(
width: 120,
height: 50,
child: TextField(
textAlignVertical: TextAlignVertical.center,
decoration: const InputDecoration(
border: OutlineInputBorder(), hintText: 'host'),
controller: hostController,
enabled: true,
),
),
],
),
),
Row(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: ElevatedButton(
onPressed: () {
addExtraHost(
country: countryController.text,
host: hostController.text);
},
child: const Text('ADD EXTRA COUNTRY HOST')),
),
],
),
const Padding(
padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
child: Divider(
height: 1,
color: Color.fromARGB(0xFF, 0x88, 0x88, 0x88),
),
),
Row(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: ElevatedButton(
onPressed: initBigoAdSdk,
child: const Text('INIT BIGO AD SDK')),
),
if (_bigoAdSdkInited)
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: ElevatedButton(
onPressed: getBidderToken,
child: const Text('GET BIDDER TOKEN')),
),
],
),
const Padding(
padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
child: Divider(
height: 1,
color: Color.fromARGB(0xFF, 0x88, 0x88, 0x88),
),
),
if (_bigoAdSdkInited)
Column(
children: [
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BannerAdPage()),
);
},
child: const Text('Banner Ad example'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NativeAdPage()),
);
},
child: const Text('Native Ad example'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const InterstitialAdPage()),
);
},
child: const Text('Interstitial Ad example'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RewardAdPage()),
);
},
child: const Text('Reward Ad example'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const SplashAdAppShowPage()),
);
},
child: const Text('Splash Ad example(app show)'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const SplashAdSdkShowPage()),
);
},
child: const Text('Splash Ad example(sdk show)'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const PopupAdPage()),
);
},
child: const Text('Popup Ad example'),
),
],
),
],
),
),
),
);
}
}