fluent_adflow_widget 1.0.4-beta.2 fluent_adflow_widget: ^1.0.4-beta.2 copied to clipboard
Fluent AdFlow Widget
import 'package:flutter/material.dart';
import 'package:fluent_adflow_widget/FluentAdFlowWidget.dart';
void main() {
runApp(const MyApp());
}
class PopupModule extends StatefulWidget {
final Function(bool)? onAdShow;
Map<String, String>? params;
PopupModule({this.onAdShow, this.params});
@override
State<PopupModule> createState() => PopupModuleState();
}
class PopupModuleState extends State<PopupModule> {
bool isDialogOpen = false;
@override
Widget build(BuildContext context) {
return Container(
color: isDialogOpen
? const Color.fromARGB(100, 0, 0, 0)
: const Color(0x01000000),
constraints: const BoxConstraints.expand(),
child: Center(
widthFactor: 0.8,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 20),
child: FluentAdFlowWidgetView(
onAdShow: (isAdShown) {
//for showing backdrop when ad loads
setState(() {
isDialogOpen = isAdShown;
});
//close dialogue when ad is hidden
if (!isAdShown) {
if (Navigator.canPop(context)) {
Navigator.of(context, rootNavigator: true).pop(context);
}
}
//call onAdShow callback of parent widget
if (widget.onAdShow != null) {
widget.onAdShow!(isAdShown);
}
},
params: widget.params,
),
),
),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AdFlow Flutter App',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// late Widget content;
bool isDialogOpen = false;
bool showEmbededAd1 = false;
bool showEmbededAd2 = false;
bool showPopUpAd = false;
//user details
var userDetails = {
"email": "jsmith@gmail.com",
"firstname": "John",
"lastname": "Smith",
"orderId": "123abc456def",
"transactionValue": "99.99",
"zip": "11211",
};
@override
void initState() {
super.initState();
FluentAdFlowWidget.init("api-key", "referer");
}
void onAdShownEmbeded1(bool isShown) {
print("^^^^^^^^^ onAdShown ^^^^^^^^^" + (isShown ? "true" : "false"));
setState(() {
if (!isShown) showEmbededAd1 = false;
});
}
void onAdShownEmbeded2(bool isShown) {
print("^^^^^^^^^ onAdShown ^^^^^^^^^" + (isShown ? "true" : "false"));
setState(() {
if (!isShown) showEmbededAd2 = false;
});
}
void onAdShownPopup(bool isShown) {
print("^^^^^^^^^ onAdShown ^^^^^^^^^" + (isShown ? "true" : "false"));
setState(() {
showPopUpAd = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Stack(
children: [
ListView(children: [
showEmbededAd1 ? FluentAdFlowWidgetView(
onAdShow: onAdShownEmbeded1,
params: userDetails,
) : Container(),
Container(
height: 50,
color: Colors.amber[600],
child: const Center(child: Text('Upper View')),
),
//embedded ad view
Container(
height: 50,
color: Colors.amber[600],
child: const Center(child: Text('Lower View')),
),
showEmbededAd2 ? FluentAdFlowWidgetView(
onAdShow: onAdShownEmbeded2,
params: userDetails,
) : Container(),
ElevatedButton(
onPressed: () =>
{
//open ad view in popup dialog
showDialog(
context: context,
barrierColor: Color(0x01000000),
builder: (context) =>
PopupModule(onAdShow: onAdShownPopup, params: userDetails)
),
},
child: const Text('Popup View'),
),
ElevatedButton(
onPressed: () => {
setState(() {
showEmbededAd1 = true;
})
},
child: const Text('Embedded View 1'),
),
ElevatedButton(
onPressed: () => {
setState(() {
showEmbededAd2 = true;
})
},
child: const Text('Embedded View 2'),
),
]),
]));
}
}