linklytics_flutter 1.1.4 copy "linklytics_flutter: ^1.1.4" to clipboard
linklytics_flutter: ^1.1.4 copied to clipboard

A Flutter plugin for integrating Linklytics analytics into your Flutter applications.

example/lib/main.dart

import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:linklytics_flutter/deep_link_route.dart';
import 'package:linklytics_flutter/l10n/app_localizations.dart';
import 'package:linklytics_flutter/linklytics_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize GetX and Dio
  Get.put(Dio());
  LinklyticsNavigator.initializeControllers();

  final linklyticsFlutterPlugin = LinklyticsFlutter();
  await linklyticsFlutterPlugin.initialize(apiKey: 'api-key');
  linklyticsFlutterPlugin.getLink((DeepLinkRoute route) {});

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _linklyticsFlutterPlugin = LinklyticsFlutter();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 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 _linklyticsFlutterPlugin.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.
    if (!mounted) return;

    // setState(() {
    // _platformVersion = platformVersion;
    // });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Linklytics Flutter Example',
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: AppLocalizations.supportedLocales,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Linklytics Flutter')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () => LinklyticsNavigator.openCommunities(),
              child: const Text('Open Communities'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => LinklyticsNavigator.openBlogs(),
              child: const Text('Open Blogs'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => LinklyticsNavigator.openBlogs(),
              child: const Text('Open App 1 Blogs'),
            ),
          ],
        ),
      ),
    );
  }
}