knt_ad_monetized 1.4.1 copy "knt_ad_monetized: ^1.4.1" to clipboard
knt_ad_monetized: ^1.4.1 copied to clipboard

PlatformiOS

Helper blocs for handling common admob flow.

example/lib/main.dart

import 'package:example/ad_manager.dart';
import 'package:example/page/book_page.dart';
import 'package:flutter/material.dart';
import 'package:knt_ad_monetized/knt_ad_monetized.dart';
import 'package:knt_bloc/knt_bloc.dart';

import 'flavors.dart';
import 'page/student_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Flavors.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Knt Ad Monetized Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Knt Ad Monetized Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AppOpenAdBloc? _appOpenAdBloc;
  InterstitialAdBloc? _interstitialAdBloc;
  BannerAdBloc? _bannerAdBloc;
  NativeAdBloc? _nativeAdBloc;
  bool _blocLoaded = false;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _init();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: couldShowView
            ? MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (_) => _appOpenAdBloc!,
                  ),
                  BlocProvider(
                    create: (_) => _interstitialAdBloc!,
                  ),
                  BlocProvider(
                    create: (_) => _bannerAdBloc!,
                  ),
                  BlocProvider(
                    create: (_) => _nativeAdBloc!,
                  ),
                ],
                child: const _ContentPage(),
              )
            : const CircularProgressIndicator(
                color: Colors.orange,
              ),
      ),
    );
  }

  void _init() async {
    _blocLoaded = false;
    _appOpenAdBloc = await appOpenAdBloc;
    _interstitialAdBloc = await interstitialAdBloc;
    _bannerAdBloc = await bannerAdBloc;
    _nativeAdBloc = await nativeAdBloc;

    setState(() {
      _blocLoaded = true;
    });
    _appOpenAdBloc!.add(PrepareAdWithoutViewEvent());
    _interstitialAdBloc!.add(PrepareAdWithoutViewEvent());
  }

  bool get couldShowView =>
      _blocLoaded &&
      _appOpenAdBloc != null &&
      _interstitialAdBloc != null &&
      _bannerAdBloc != null &&
      _nativeAdBloc != null;
}

class _ContentPage extends StatelessWidget {
  const _ContentPage({Key? key}) : super(key: key);

  final _adTag = 'MyHomePage';

  @override
  Widget build(BuildContext context) {
    return MultiBlocListener(
      listeners: [
        BlocListener<AppOpenAdBloc, BaseState>(
          listener: (context, state) {
            if (state is ClosedAdWithoutViewState) {
              context.showInSnackBar('AppOpenAd closed!');
            }
          },
        ),
        BlocListener<InterstitialAdBloc, BaseState>(
          listener: (context, state) {
            if (state is ClosedAdWithoutViewState) {
              context.showInSnackBar('InterstitialAd closed!');
            }
          },
        ),
      ],
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                context.loadAppOpenAd(_adTag);
              },
              child: const Text('Show App Open Ads'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                context.loadInterstitialAd(_adTag);
              },
              child: const Text('Show Interstitial Ads'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) {
                      return const BookPage();
                    },
                  ),
                );
              },
              child: const Text('Show Native Ads in BOOK list'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) {
                      return const StudentPage();
                    },
                  ),
                );
              },
              child: const Text('Show Native Ads in STUDENT list'),
            ),
            const SizedBox(height: 16),
            const KntBannerAd(
              tag: 'banner_ad_home',
            ),
            const SizedBox(height: 16),
            KntNativeAd(
              tag: 'native_ad_home_small',
              nativeTemplateStyle: NativeTemplateStyle(
                templateType: TemplateType.small,
              ),
            ),
            const SizedBox(height: 16),
            KntNativeAd(
              tag: 'native_ad_home',
              nativeTemplateStyle: NativeTemplateStyle(
                // Required: Choose a template.
                templateType: TemplateType.medium,
                // Optional: Customize the ad's style.
                cornerRadius: 10.0,
                callToActionTextStyle: NativeTemplateTextStyle(
                    textColor: Colors.white,
                    backgroundColor: Colors.green,
                    style: NativeTemplateFontStyle.monospace,
                    size: 16.0),
                primaryTextStyle: NativeTemplateTextStyle(
                    textColor: Colors.red,
                    backgroundColor: Colors.cyan,
                    style: NativeTemplateFontStyle.italic,
                    size: 16.0),
                secondaryTextStyle: NativeTemplateTextStyle(
                    textColor: Colors.green,
                    backgroundColor: Colors.transparent,
                    style: NativeTemplateFontStyle.bold,
                    size: 16.0),
                tertiaryTextStyle: NativeTemplateTextStyle(
                    textColor: Colors.brown,
                    backgroundColor: Colors.amber,
                    style: NativeTemplateFontStyle.normal,
                    size: 16.0),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

extension on BuildContext {
  void showInSnackBar(String message) {
    ScaffoldMessenger.of(this).removeCurrentSnackBar();
    ScaffoldMessenger.of(this).showSnackBar(SnackBar(
      duration: const Duration(milliseconds: 3000),
      content: Text(message),
    ));
  }
}