knt_monetize_bloc 3.0.0 knt_monetize_bloc: ^3.0.0 copied to clipboard
Helper blocs for handling in-app-purchase & admob flow
import 'package:flutter/material.dart';
import 'package:knt_bloc/knt_bloc.dart';
import 'package:knt_monetize_bloc/knt_monetize_bloc.dart';
import 'di.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp(bloc: await adsBloc));
}
class MyApp extends StatelessWidget {
final FrequentlyAdsBloc bloc;
const MyApp({
Key? key,
required this.bloc,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => bloc,
child: MaterialApp(
title: 'Knt Monetized Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const PageOne(),
),
);
}
}
class PageOne extends StatefulWidget {
const PageOne({Key? key}) : super(key: key);
@override
State<PageOne> createState() => _PageOneState();
}
class _PageOneState extends State<PageOne> {
bool _loading = false;
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
context.read<FrequentlyAdsBloc>().add(PrepareFrequentlyAdEvent());
});
}
void _onPressed() {
setState(() {
_loading = true;
});
Future.delayed(
const Duration(milliseconds: 1500),
() {
if (!mounted) {
return;
}
context
.read<FrequentlyAdsBloc>()
.add(const ShowFrequentlyAdEvent('PageOne'));
},
);
}
@override
Widget build(BuildContext context) {
return BlocListener<FrequentlyAdsBloc, BaseState>(
listener: (context, state) {
if (!mounted) {
return;
}
if (state is ClosedFrequentlyAdState) {
setState(() {
_loading = false;
});
PageTwo.push(context);
}
},
child: Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.orange,
child: Center(
child: _loading
? const CircularProgressIndicator(
color: Colors.white,
)
: ElevatedButton(
onPressed: _onPressed,
child: const Text(
'Page 1, move to Page 2',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
);
}
}
class PageTwo extends StatefulWidget {
static void push(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const PageTwo()),
);
}
const PageTwo({Key? key}) : super(key: key);
@override
State<PageTwo> createState() => _PageTwoState();
}
class _PageTwoState extends State<PageTwo> {
bool _loading = false;
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
context.read<FrequentlyAdsBloc>().add(PrepareFrequentlyAdEvent());
});
}
void _onPressed() {
setState(() {
_loading = true;
});
Future.delayed(
const Duration(milliseconds: 1500),
() {
if (!mounted) {
return;
}
context
.read<FrequentlyAdsBloc>()
.add(const ShowFrequentlyAdEvent('PageTwo'));
},
);
}
@override
Widget build(BuildContext context) {
return BlocListener<FrequentlyAdsBloc, BaseState>(
listener: (context, state) {
if (!mounted) {
return;
}
if (state is ClosedFrequentlyAdState) {
setState(() {
_loading = false;
});
PageThree.push(context);
}
},
child: Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.blue,
child: Center(
child: _loading
? const CircularProgressIndicator(
color: Colors.white,
)
: ElevatedButton(
onPressed: _onPressed,
child: const Text(
'Page 2, move to Page 3',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
);
}
}
class PageThree extends StatelessWidget {
static void push(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const PageThree()),
);
}
const PageThree({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.purple,
child: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).popUntil((route) => route.isFirst);
},
child: const Text(
'Page 3, Pop Till Page 1',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
),
),
),
);
}
}