barterads_flutter 0.1.0
barterads_flutter: ^0.1.0 copied to clipboard
BarterAds display advertising SDK for Flutter applications.
example/lib/main.dart
import 'package:barterads_flutter/barterads_flutter.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const ExampleApp());
}
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
static const appId = 'REPLACE_WITH_APP_ID';
static const inventoryId = 'REPLACE_WITH_INVENTORY_ID';
final List<String> _events = <String>[];
bool _initialized = false;
@override
void initState() {
super.initState();
_initialize();
}
Future<void> _initialize() async {
try {
await BarterAds.initialize(
appId: appId,
storageAllowed: true,
personalizationAllowed: true,
measurementAllowed: true,
debug: true,
);
_log('initialized');
if (mounted) {
setState(() => _initialized = true);
}
} catch (error) {
_log('initialize failed: $error');
}
}
void _log(String message) {
if (!mounted) {
return;
}
setState(() {
_events.insert(0, '${DateTime.now().toIso8601String()} $message');
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('BarterAds Flutter Example')),
body: ListView(
padding: const EdgeInsets.all(16),
children: <Widget>[
const Text(
'Replace appId and inventoryId in example/lib/main.dart before '
'running against production inventory.',
),
const SizedBox(height: 16),
if (_initialized)
BarterBanner(
inventoryId: inventoryId,
size: BarterAdSize.responsive,
onFill: (requestId, creativeId) {
_log('fill request=$requestId creative=$creativeId');
},
onImpression: () => _log('impression'),
onClick: () => _log('click'),
onNoFill: () => _log('no fill'),
onFailed: (code) => _log('failed ${code.nativeValue}'),
)
else
const SizedBox(
height: 50,
child: Center(child: CircularProgressIndicator()),
),
const SizedBox(height: 24),
const Text('Events'),
const SizedBox(height: 8),
for (final event in _events) Text(event),
],
),
),
);
}
}