Remove the Flutter Native Splash Screen after AppOpenAd is closed

1.2k views Asked by At

I'm trying to implement the Flutter Native Splash new feature of preserving the splash screen until the remove() function is called. I want the splash to be removed when a user dismisses or closes the AppOpenAd. And I also want the splash screen to be preserved till the ad is loaded and shown to the user and removed when dismissed.

I'm using a secondary splash screen, Animated Splash Screen to hide the black screen when the Open App Ad is dismissed.

I tried to run the

loadOpenAppAd

on the main function, but it was a disaster. On dismiss, the black screen couldn't come off.

import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';

AppOpenAd? myAppOpenAd;

loadAppOpenAd() {
  AppOpenAd.load(
      adUnitId: ids.appOpenAdUnitId,
      request: const AdRequest(),
      adLoadCallback: AppOpenAdLoadCallback(onAdLoaded: (ad) {
        myAppOpenAd = ad;
        myAppOpenAd!.show();
        myAppOpenAd!.fullScreenContentCallback= FullScreenContentCallback(
          onAdDismissedFullScreenContent: (AppOpenAd ad){
            ad.dispose();
            FlutterNativeSplash.remove();
          }
        );
        FlutterNativeSplash.remove();
      }, onAdFailedToLoad: (error) {
        FlutterNativeSplash.remove();
      }),
      orientation: AppOpenAd.orientationPortrait);
}

Future<void> main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  await MobileAds.instance.initialize();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.

 
  @override
  void initState() {
    super.initState();
    loadAppOpenAd();
  }
  
  @override
  Widget build(BuildContext context) => OverlaySupport.global(
        child: MyBackground(
          child: MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'Bojack',
            theme: ThemeData(
                scaffoldBackgroundColor: backgroundColor,
                colorScheme: const ColorScheme.dark(
                    onBackground: whiteShade, 
                    onSurface: whiteShade),
             ),
            home: AnimatedSplashScreen(
              duration: 2500,
              splash: '..icon.png',
              splashIconSize: 120,
              nextScreen: isSeenOnboard == true
                  ? const DynamicPage()
                  : const OnBoardingPage(),
              splashTransition: SplashTransition.fadeTransition,
              pageTransitionType: PageTransitionType.fade,
              backgroundColor:backgroundColor,
            ),
          ),
        ),
      );
}
0

There are 0 answers