livehive_sdk_flutter 0.0.1 copy "livehive_sdk_flutter: ^0.0.1" to clipboard
livehive_sdk_flutter: ^0.0.1 copied to clipboard

The LiveHive Flutter SDK.

example/lib/main.dart

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:livehive_sdk_flutter/livehive_sdk_flutter.dart';

void main() {
  runApp(const MyApp());
}

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

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

class _MyAppState extends State<MyApp> {
  final _liveHiveSdk = LivehiveSdkFlutter();

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

  void _initialise() {
    _liveHiveSdk.initialize(key: "YOUR_KEY", isDailyPoints: true);
    _liveHiveSdk.events.listen((event) {
      if (event == "onInstructionsDoneClicked") {
        print("Instructions done! ----------------- ");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: MyHomePage(liveHiveSdk: _liveHiveSdk));
  }
}

class MyHomePage extends StatefulWidget {
  final LivehiveSdkFlutter liveHiveSdk;

  const MyHomePage({super.key, required this.liveHiveSdk});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _userIdController = TextEditingController();

  @override
  void dispose() {
    _userIdController.dispose();
    super.dispose();
  }

  void _onStartPressed() {
    widget.liveHiveSdk.start(userId: _userIdController.text);
  }

  void _getRewards() async {
    try {
      final rewards = await widget.liveHiveSdk.getRewards(userId: _userIdController.text);
      print("Rewards ------------ : $rewards");
    } catch (e) {
      print('Error fetching rewards ------------ : $e');
    }
  }

  void _award100Points() async {
    try {
      final source = _getRandomString();
      final key = _getRandomString();
      final award = await widget.liveHiveSdk.awardRewards(userId: _userIdController.text, points: 100, source: source, awardKey: key);
      print("Awards ------------ : $award");
    } catch (e) {
      print('Error awarding rewards ------------ : $e');
    }
  }

  void _redeem100Points() async {
    try {
      final redemption = await widget.liveHiveSdk.redeemRewards(userId: _userIdController.text, points: 100);
      print("Redemption ------------ : $redemption");
    } catch (e) {
      print('Error redeeming rewards ------------ : $e');
    }
  }

  void _testRefund() async {
    try {
      final refund = await widget.liveHiveSdk.refundRewards(userId: _userIdController.text, redemptionId: "some id");
      print("Refund ------------ : $refund");
    } catch (e) {
      print('Error refunding rewards ------------ : $e');
    }
  }

  void _onCancelPressed(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Exit'),
          content: const Text('Are you sure you want to close the app?'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('Cancel'),
            ),
            TextButton(
              onPressed: () {
                SystemNavigator.pop();
              },
              child: const Text('Close'),
            ),
          ],
        );
      },
    );
  }

  String _getRandomString() {
    const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
    final rnd = Random();

    return String.fromCharCodes(Iterable.generate(12, (_) => chars.codeUnitAt(rnd.nextInt(chars.length))));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('LiveHive Flutter Example')),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextField(
                controller: _userIdController,
                decoration: const InputDecoration(hintText: 'User ID'),
              ),
              const SizedBox(height: 20),
              ElevatedButton(onPressed: _onStartPressed, child: const Text('Start')),
              ElevatedButton(onPressed: _getRewards, child: const Text('Get Rewards')),
              ElevatedButton(onPressed: _award100Points, child: const Text('Award 100 points')),
              ElevatedButton(onPressed: _redeem100Points, child: const Text('Redeem 100 points')),
              ElevatedButton(onPressed: _testRefund, child: const Text('Refund fail')),
              ElevatedButton(onPressed: () => _onCancelPressed(context), child: const Text('Cancel')),
            ],
          ),
        ),
      ),
    );
  }
}