contlo_plugin 1.2.0
contlo_plugin: ^1.2.0 copied to clipboard
The Contlo Flutter SDK for mobile Customer Engagement with Autonomous AI Marketing
example/lib/main.dart
// import 'package:contlo_plugin_example/dynamic_screen.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:async/async.dart';
import 'package:flutter/services.dart';
import 'package:contlo_plugin/contlo_plugin.dart';
import 'package:permission_handler/permission_handler.dart';
// import 'package:uni_links/uni_links.dart';
import 'package:app_links/app_links.dart';
import 'user_screen.dart';
import 'dummy_event_screen.dart';
import 'sign_up_screen.dart';
import 'product_screen.dart';
import 'event_screen.dart';
final API_KEY = "";
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
void main() {
runApp(const MyApp());
ContloPlugin.enableLogging(true);
requestNotificationPermission();
}
Future<void> requestNotificationPermission() async {
// Request the camera permission
final status = await Permission.notification.request();
if (status.isGranted) {
} else if (status.isDenied) {
} else if (status.isPermanentlyDenied) {
openAppSettings();
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
scaffoldMessengerKey: scaffoldMessengerKey;
return MaterialApp(
home: Scaffold(
body: HomePage(),
)
);
}
}
class HomePage extends StatelessWidget {
late final BuildContext context;
bool isRunning = false;
final debouncer = Debouncer(delay: Duration(milliseconds: 500));// Adjust the duration as needed
String _platformVersion = 'Unknown';
final _contloPlugin = ContloPlugin();
HomePage() {
scaffoldMessengerKey: scaffoldMessengerKey;
init();
initUniLinks();
}
void init() async {
String? dd = await ContloPlugin.init(API_KEY) as String?;
print("initialized contlo: $dd");
// Add your code here for the third button
}
void initUniLinks() async {
try {
Uri? initialUri = await AppLinks().getInitialLink();
// debouncer.run(() {
handleDeepLink(initialUri);
// Handle the deep link here
// });
} catch (e) {
// Handle exceptions
}
}
void handleDeepLink(Uri? uri) {
if (uri != null) {
// Extract data from the deep link
String path = uri.path;
Map<String, String> queryParams = uri.queryParameters;
print("deep linking: $path");
// isRunning = true;
// Navigator.replace(context, oldRoute: MaterialPageRoute(builder: (context) => HomePage()), newRoute: MaterialPageRoute(builder: (context) => DynamicScreen(displayText: path)));
// Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => DynamicScreen(displayText: path)), (route) => false);
// Navigator.push(context, MaterialPageRoute(builder: (context) => DynamicScreen(displayText: path)));
// scaffoldMessengerKey.currentState?.showSnackBar(
// SnackBar(
// content: Text('This is a global SnackBar!'),
// ),
// );
// Navigator.push(this.context, MaterialPageRoute(builder: (context) => DynamicScreen(displayText: queryParams.values.first)));
}
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await ContloPlugin.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
}
@override
Widget build(BuildContext context) {
this.context = context;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Contlo Plugin'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Align buttons vertically in the center
children: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => UserScreen()));
},
child: Text('Send User Detail'),
),
// ElevatedButton(
// onPressed: () {
// Navigator.push(context, MaterialPageRoute(builder: (context) => EventScreen()));
// },
// child: Text('Send Event'),
// ),
// ElevatedButton(
// onPressed: () async {
// String? dd = await ContloPlugin.sendAdvertisingId(true);
// print("Advertising ID: $dd");
// // Add your code here for the third button
// },
// child: Text('Send Advertising ID'),
// ),
ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductListScreen()));
},
child: Text('Dummy Events'),
),
],
),
),
),
);
}
}
class Debouncer {
final Duration delay;
Timer? _timer;
Debouncer({required this.delay});
void run(VoidCallback callback) {
if (_timer != null) {
_timer!.cancel();
}
_timer = Timer(delay, callback);
}
}