bluestack_sdk_flutter 1.0.2 copy "bluestack_sdk_flutter: ^1.0.2" to clipboard
bluestack_sdk_flutter: ^1.0.2 copied to clipboard

BlueStack Flutter plugin for interstitial, rewarded, and banner ad integration on 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();
  final GlobalKey<BannerViewState> bannerViewKey = GlobalKey<BannerViewState>();
  InterstitialAd? _interstitialAd;
  RewardedAd? _rewardedVideoAd;
  bool _isSDKInitialized = false;
  bool _isBannerVisible = false;
  bool _isBannerRefreshing = false;

  @override
  void initState() {
    super.initState();
    _initializeSDK();
    _setupOptions();
  }

  @override
  void dispose() {
    // Clean up ads
    _interstitialAd?.dispose();
    _rewardedVideoAd?.dispose();
    super.dispose();
  }

  /// Initializes the BlueStack SDK and sets up event listeners for initialization events.
  /// Updates the UI state based on success or failure.
  Future<void> _initializeSDK() async {
    // BlueStack Initializer callbacks
    BlueStackInitializer.setEventListener(InitializationEventListener(
      onInitializationSuccess: (adapterStatus) {
        setState(() {
          _isSDKInitialized = true;
        });
        debugPrint('$_tag BlueStack SDK initialized with App ID: $_bsAppId');
      },
      onInitializationFail: (error) {
        setState(() {
          _isSDKInitialized = false;
        });
        debugPrint(
            '$_tag BlueStack SDK failed to initialize: ${error.toString()}');
      },
    ));

    BlueStackInitializer.initialize(
      appId: _bsAppId,
      enableDebug: true,
    );
  }

  /// Configures the [RequestOptions] with user and content targeting parameters.
  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/');
  }

  /// Creates and loads an interstitial ad, and sets up event listeners for load events.
  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);
  }

  /// Shows the interstitial ad if loaded, and sets up event listeners for show events.
  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();
  }

  /// Creates and loads a rewarded ad, and sets up event listeners for load events.
  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);
  }

  /// Shows the rewarded ad if loaded, and sets up event listeners for show and reward events.
  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();
  }

  /// Loads the banner ad using the current [RequestOptions].
  void _loadBannerAd() {
    bannerViewKey.currentState?.load(options: _options);
  }

  /// Toggles the visibility of the banner ad.
  void _toggleBannerAdVisibility() {
    bannerViewKey.currentState?.toggleVisibility(!_isBannerVisible);
    setState(() {
      _isBannerVisible = !_isBannerVisible;
    });
  }

  /// Starts or stops the automatic refresh of the banner ad.
  void _toggleBannerAdRefresh() {
    bannerViewKey.currentState?.toggleRefresh(!_isBannerRefreshing);
    setState(() {
      _isBannerRefreshing = !_isBannerRefreshing;
    });
  }

  /// Destroys the banner ad and releases its resources.
  void _destroyBannerAd() {
    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>[
              const Text(
                'Banner Ad:',
                style: TextStyle(fontSize: 18),
              ),
              const SizedBox(height: 20),
              if (_isSDKInitialized)
                // Banner ad with GlobalKey for manual control
                BannerView(
                  key: bannerViewKey,
                  type: BannerAdSize.banner,
                  placementId: '/$_bsAppId/banner',
                  shouldLoadWhenReady: false,
                  options: _options,
                  onAdLoaded: (size) {
                    debugPrint('$_tag Banner ad loaded with size: $size');
                    setState(() {
                      _isBannerVisible = true;
                    });
                  },
                  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}');
                  },
                ),

              // Banner Ad Buttons
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: _loadBannerAd,
                    child: const Text('Load banner Ad'),
                  ),
                  const SizedBox(width: 10),
                  ElevatedButton(
                    onPressed: _toggleBannerAdRefresh,
                    child: Text(_isBannerRefreshing
                        ? 'Stop banner Refresh'
                        : 'Start banner Refresh'),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: _toggleBannerAdVisibility,
                    child: Text(
                        _isBannerVisible ? 'Hide banner Ad' : 'Show banner Ad'),
                  ),
                  const SizedBox(width: 10),
                  ElevatedButton(
                    onPressed: _destroyBannerAd,
                    child: const Text('Destroy banner Ad'),
                  ),
                ],
              ),

              const SizedBox(height: 20),
              if (_isSDKInitialized)
                // MREC Banner Ad with auto load
                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'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
150
points
55
downloads
screenshot

Publisher

unverified uploader

Weekly Downloads

BlueStack Flutter plugin for interstitial, rewarded, and banner ad integration on Android and iOS apps.

Homepage

Topics

#bluestack #ads #interstitial #rewarded #banner

Documentation

Documentation
API reference

License

Apache-2.0 (license)

Dependencies

flutter, meta

More

Packages that depend on bluestack_sdk_flutter

Packages that implement bluestack_sdk_flutter