smartlinks_ad 1.7.0
smartlinks_ad: ^1.7.0 copied to clipboard
A Flutter package for Linklytics SmartLinks Ads.
example/lib/main.dart
import 'package:example/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:smartlinks_ad/smartlinks_ad.dart';
import 'package:linklytics_flutter/linklytics_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('SmartLinks Ad Example')),
body: const AdExample(),
),
);
}
}
class AdExample extends StatefulWidget {
const AdExample({super.key});
@override
State<AdExample> createState() => _AdExampleState();
}
class _AdExampleState extends State<AdExample> {
final _adService = SmartLinksAd();
bool _initialized = false;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
// Replace with your API Key
// Initialize linklytics to get app user id
String? appUserId;
try {
final linklytics = LinklyticsFlutter();
await linklytics.initialize(
apiKey:
'sk_94fc18841c03f29ecf212d4b6844f796dcde6c701a343c1bc15085153aef',
);
appUserId = await linklytics.getAppUserId();
debugPrint('[AdExample] Retrieved appUserId: $appUserId');
} catch (e) {
debugPrint('[AdExample] Error getting appUserId from linklytics: $e');
}
// Initialize push notifications/device registration
try {
await _adService.initialize(
'sk_94fc18841c03f29ecf212d4b6844f796dcde6c701a343c1bc15085153aef',
appUserId: appUserId,
);
await SmartLinksPushHandler.initialize(
apiKey:
'sk_94fc18841c03f29ecf212d4b6844f796dcde6c701a343c1bc15085153aef',
appUserId: appUserId,
);
} catch (e) {
debugPrint('[AdExample] Error initializing push handler: $e');
}
setState(() {
_initialized = true;
});
}
@override
Widget build(BuildContext context) {
if (!_initialized) {
return const Center(child: CircularProgressIndicator());
}
return Column(
children: [
const Text('Banner Ad Below:'),
const SizedBox(height: 10),
SmartLinkBannerAd(adService: _adService, height: 100),
const Spacer(),
const Text('End of screen'),
],
);
}
}