Flutter 네이티브 광고
Last updated
Last updated
Widget nativeAdView(BuildContext context) {
if (!isLoaded) return Container();
return Container(
margin: const EdgeInsets.symmetric(vertical: 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
width: MediaQuery.of(context).size.width,
child: AdropNativeAdView(
ad: nativeAd,
child: Container(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.network(
nativeAd?.properties.profile?.displayLogo ?? '',
width: 24,
height: 24,
),
const SizedBox(
width: 4,
),
Text(
nativeAd?.properties.profile?.displayName ?? '',
)
],
),
const SizedBox(
height: 8,
),
if (nativeAd?.properties.headline != null)
Text(
nativeAd?.properties.headline ?? '',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
if (nativeAd?.properties.body != null)
Text(
nativeAd?.properties.body ?? '',
style: const TextStyle(
fontSize: 14,
),
),
if (nativeAd?.properties.asset != null)
Column(children: [
const SizedBox(
height: 4,
),
Image.network(
nativeAd?.properties.asset ?? '',
loadingBuilder: (context, child, loadingProgress) {
if (loadingProgress == null) return child;
return const CupertinoActivityIndicator();
},
errorBuilder: (context, error, stackTrace) {
return const Icon(Icons.error);
},
width: MediaQuery.of(context).size.width,
height: 300,
)
]),
if (nativeAd?.properties.extra['t1'] != null)
Column(
children: [
const SizedBox(
height: 8,
),
Text(
nativeAd?.properties.extra['t2'] ?? '',
style: const TextStyle(
fontSize: 14,
),
),
Text(
nativeAd?.properties.extra['t3'] ?? '',
style: const TextStyle(
fontSize: 14,
),
),
],
)
],
),
),
),
);
}
final AdropNativeListener listener = AdropNativeListener(
onAdReceived: (ad) =>
debugPrint('Adrop Native Ad loaded with unitId ${ad.unitId}!'),
onAdFailedToReceive: (ad, errorCode) =>
debugPrint('error in ${ad.unitId} while loading: $errorCode'),
...
);
class YourComponent extends StatefulWidget {
const YourComponent({super.key});
@override
State<StatefulWidget> createState() => _YourComponentState();
}
class _YourComponentState extends State<YourComponent> {
final AdropNativeAd nativeAd = AdropNativeAd(
unitId: 'YOUR_UNIT_ID',
listener: listener,
);
bool isLoaded = false;
@override
void initState() {
super.initState();
nativeAd.load();
}
Widget nativeAdView(BuildContext context) {
if (!isLoaded) return Container();
return AdropNativeView(
ad: nativeAd,
child: Column(
children: [
if (nativeAd.properties.profile?.displayLogo)
Image.network(
nativeAd.properties.profile?.displayLogo,
width: 24,
height: 24,
),
if (nativeAd.properties.profile?.displayName)
Text(
nativeAd.properties.profile?.displayName,
),
if (nativeAd.properties.headline != null)
Text(nativeAd.properties.headline),
if (nativeAd.properties.body != null)
Text(nativeAd.properties.body),
if (nativeAd.properties.asset != null)
Image.network(nativeAd.properties.asset, width: {{yourWidth}}, height: {{yourHeight}}),
if (nativeAd.properties.extra['sampleExtraId'] != null)
Text(nativeAd.properties.extra['sampleExtraId']),
],
)
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: nativeAdView(context),
),
);
}
}