bluestack_sdk_flutter 1.0.1
bluestack_sdk_flutter: ^1.0.1 copied to clipboard
A comprehensive Flutter plugin for integrating BlueStack's ad monetization platform, supporting interstitial, rewarded, and banner ads with seamless implementation for Android and iOS apps.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:bluestack_sdk_flutter/bluestack_sdk.dart';
import 'dart:io';
final String _bsAppId = Platform.isIOS ? '1259479' : '1259479';
const String _tag = '[BlueStackExample]';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BlueStack SDK Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const MyHomePage(title: 'BlueStack SDK Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final RequestOptions _options = RequestOptions();
bool _isSDKInitialized = false;
InterstitialAd? _interstitialAd;
RewardedAd? _rewardedVideoAd;
final GlobalKey<BannerViewState> bannerViewKey = GlobalKey<BannerViewState>();
final GlobalKey<BannerViewState> mrecViewKey = GlobalKey<BannerViewState>();
bool _isBannerVisible = true;
bool _isBannerRefreshing = false;
String? _errorMessage;
@override
void initState() {
super.initState();
_initializeSDK();
_setupOptions();
}
@override
void dispose() {
// Clean up ads
_interstitialAd?.dispose();
_rewardedVideoAd?.dispose();
super.dispose();
}
Future<void> _initializeSDK() async {
// Initialize the BlueStack SDK
debugPrint('Initializing BlueStack SDK!');
// BlueStack Initializer load callbacks
BlueStackInitializer.setEventListener(InitializationEventListener(
onInitializationSuccess: (adapterStatus) {
setState(() {
_isSDKInitialized = true;
_errorMessage = null;
});
debugPrint(
'$_tag BlueStack SDK initialized with App ID: $_bsAppId, adapter status: $adapterStatus');
},
onInitializationFail: (error) {
setState(() {
_isSDKInitialized = false;
_errorMessage = error.toString();
});
debugPrint(
'$_tag BlueStack SDK failed to initialize: ${error.toString()}');
},
));
try {
BlueStackInitializer.initialize(
appId: _bsAppId,
enableDebug: true,
);
} catch (e) {
setState(() {
_isSDKInitialized = false;
_errorMessage = 'Failed to initialize SDK: $e';
});
debugPrint('$_tag Error initializing BlueStack SDK: $e');
}
}
void _setupOptions() {
_options.setAge(25);
_options.setLanguage('en');
_options.setGender(Gender.unknown);
_options.setKeyword('testbrand=myBrand;category=sport');
_options.setLocation(
Location(
latitude: 37.7749,
longitude: -122.4194,
provider: LocationProvider.gps),
1);
_options.setContentUrl('https://developers.bluestack.app/');
}
void _createInterstitialAd() {
_interstitialAd = InterstitialAd('/$_bsAppId/interstitial');
_interstitialAd?.setLoadEventListener(InterstitialAdLoadEventListener(
onAdLoaded: () {
debugPrint('$_tag Interstitial ad loaded');
},
onAdFailedToLoad: (error) {
debugPrint('$_tag Interstitial ad failed to load: ${error.message}');
},
));
_interstitialAd?.load(options: _options);
}
void _showInterstitialAd() {
_interstitialAd?.setShowEventListener(InterstitialAdShowEventListener(
onAdDisplayed: () {
debugPrint('$_tag Interstitial ad showed');
},
onAdFailedToDisplay: (error) {
debugPrint('$_tag Interstitial ad failed to load: ${error.message}');
},
onAdDismissed: () {
debugPrint('$_tag Interstitial ad closed');
},
onAdClicked: () {
debugPrint('$_tag Interstitial ad clicked');
},
));
_interstitialAd?.show();
}
void _createRewardedAd() {
_rewardedVideoAd = RewardedAd('/$_bsAppId/rewardedVideo');
_rewardedVideoAd?.setLoadEventListener(RewardedAdLoadEventListener(
onAdLoaded: () {
debugPrint('$_tag Rewarded ad loaded');
},
onAdFailedToLoad: (error) {
debugPrint('$_tag Rewarded ad failed to load: ${error.message}');
},
));
_rewardedVideoAd?.load(options: _options);
}
void _showRewardedAd() {
_rewardedVideoAd?.setShowEventListener(RewardedAdShowEventListener(
onAdDisplayed: () {
debugPrint('$_tag Rewarded ad showed');
},
onAdFailedToDisplay: (error) {
debugPrint('$_tag Rewarded ad failed to load: ${error.message}');
},
onAdDismissed: () {
debugPrint('$_tag Rewarded ad closed');
},
onAdClicked: () {
debugPrint('$_tag Rewarded ad clicked');
},
onRewarded: (reward) {
debugPrint('$_tag Rewarded: ${reward.type} - ${reward.amount}');
},
));
_rewardedVideoAd?.show();
}
void _loadBanner() {
bannerViewKey.currentState?.load(options: _options);
}
void _toggleAdVisibility() {
bannerViewKey.currentState?.toggleVisibility(!_isBannerVisible);
setState(() {
_isBannerVisible = !_isBannerVisible;
});
}
void _toggleBannerRefresh() {
bannerViewKey.currentState?.toggleRefresh(!_isBannerRefreshing);
}
void _destroyBanner() {
bannerViewKey.currentState?.destroy();
}
@override
Widget build(BuildContext context) {
debugPrint('$_tag Has SDK been initialized: $_isSDKInitialized');
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text(widget.title),
centerTitle: true,
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (_errorMessage != null)
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
_errorMessage!,
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center,
),
),
const Text(
'Banner Ad:',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 20),
if (_isSDKInitialized)
BannerView(
key: bannerViewKey,
type: BannerAdSize.banner,
placementId: '/$_bsAppId/banner',
shouldLoadWhenReady: false,
options: _options,
onAdLoaded: (size) {
debugPrint('$_tag Banner ad loaded with size: $size');
},
onAdFailedToLoad: (error) {
debugPrint(
'$_tag Banner ad failed to load: ${error.message}');
},
onAdClicked: () {
debugPrint('$_tag Banner ad clicked');
},
onAdRefreshed: () {
debugPrint('$_tag Banner ad refreshed');
setState(() {
_isBannerRefreshing = true;
});
},
onAdFailedToRefresh: (error) {
debugPrint(
'$_tag Banner ad failed to refresh: ${error.message}');
setState(() {
_isBannerRefreshing = false;
});
},
),
// Banner Ad Buttons
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _loadBanner,
child: const Text('Load banner Ad'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _toggleBannerRefresh,
child: Text(_isBannerRefreshing
? 'Stop banner Refresh'
: 'Start banner Refresh'),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _toggleAdVisibility,
child: Text(
_isBannerVisible ? 'Hide banner Ad' : 'Show banner Ad'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _destroyBanner,
child: const Text('Destroy banner Ad'),
),
],
),
// MREC Ad
const SizedBox(height: 20),
if (_isSDKInitialized)
BannerView(
type: BannerAdSize.mediumRectangle,
placementId: '/$_bsAppId/mrec',
shouldLoadWhenReady: true,
options: _options,
onAdLoaded: (size) {
debugPrint('$_tag MREC ad loaded with size: $size');
},
onAdFailedToLoad: (error) {
debugPrint(
'$_tag MREC ad failed to load: ${error.message}');
},
onAdClicked: () {
debugPrint('$_tag MREC ad clicked');
},
onAdRefreshed: () {
debugPrint('$_tag MREC ad refreshed');
},
onAdFailedToRefresh: (error) {
debugPrint(
'$_tag MREC ad failed to refresh: ${error.message}');
},
),
// Interstitial Ad
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _createInterstitialAd,
child: const Text('Load Interstitial Ad'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _showInterstitialAd,
child: const Text('Show Interstitial Ad'),
),
],
),
// Rewarded Ad
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _createRewardedAd,
child: const Text('Load rewarded Ad'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _showRewardedAd,
child: const Text('Show rewarded Ad'),
),
],
),
],
),
),
),
);
}
}