loyalty_core_flutter 0.1.0 copy "loyalty_core_flutter: ^0.1.0" to clipboard
loyalty_core_flutter: ^0.1.0 copied to clipboard

A flutter plug-in for the native Loyalty Core library (Android and iOS).

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:loyalty_core_flutter/model/member_info_request.dart';
import 'package:loyalty_core_flutter/model/register_member_request.dart';
import 'package:loyalty_core_flutter/model/transaction_history_request.dart';
import 'package:loyalty_core_flutter/model/transaction_type.dart';
import 'package:loyalty_core_flutter/terra_loyalty.dart';
import 'package:loyalty_flutter_example/login_screen.dart';
import 'package:terra_flutter/terra_app.dart';

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

const terraAppName = "loyalty-flutter";

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  late Terraloyalty _terraloyalty;
  final _messangerKey = GlobalKey<ScaffoldMessengerState>();

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

    _initTerraLibs();
  }

  void _initTerraLibs() async {
    try {
      await TerraApp.initTerraApp(appName: terraAppName);
      _terraloyalty = await Terraloyalty.getInstance(terraAppName);

      print("Initialized Terra succeeded");
    } catch (error) {
      print("Initialized Terra failed: $error");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      scaffoldMessengerKey: _messangerKey,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: Column(
              children: [
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getClientId();
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get Loyalty Configuration"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.registerMember(RegisterMemeberRequest(
                        "0374264444", "name", "address", null, null, null, null, null, null));
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Register Member"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getMembershipQr();
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get MembershipQR"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getMemberInfo(MemberInfoRequest());
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get MemberInfo"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getTransactionHistory(
                        TransactionHistoryRequest(
                            null, null, null, [TransactionType.grant], null, 1, 20));
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get Transactions"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getNetworkConfig();
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get Network Config"),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final result = await _terraloyalty.getClientId();
                    if (result.isFailure) {
                      showToast("Failed");
                    } else {
                      showToast(result.get().toString());
                    }
                  },
                  child: const Text("Get Client ID"),
                ),

                Builder(
                  builder: (context) {
                    return ElevatedButton(
                      onPressed: () async {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => const LoginScreen()),
                        );
                      },
                      child: const Text("Handle Authentication"),
                    );
                  }
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void showToast(String message) {
    _messangerKey.currentState!.showSnackBar(SnackBar(content: Text(message)));
  }
}
0
likes
110
pub points
0%
popularity

Publisher

unverified uploader

A flutter plug-in for the native Loyalty Core library (Android and iOS).

Repository

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on loyalty_core_flutter