knt_ad_monetized 1.0.0 knt_ad_monetized: ^1.0.0 copied to clipboard
Helper blocs for handling common admob flow.
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: 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',
nativeTemplateStyle: NativeTemplateStyle(
templateType: TemplateType.small,
),
),
],
),
);
}
}
extension on BuildContext {
void showInSnackBar(String message) {
ScaffoldMessenger.of(this).removeCurrentSnackBar();
ScaffoldMessenger.of(this).showSnackBar(SnackBar(
duration: const Duration(milliseconds: 3000),
content: Text(message),
));
}
}