loyalty_core_flutter 0.2.0-alpha.3 loyalty_core_flutter: ^0.2.0-alpha.3 copied to clipboard
A flutter plug-in for the native Loyalty Core library (Android and iOS).
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(RegisterMemberRequest(
phone: "0374264444",
name: "name",
address: "address",
dateOfBirth: null,
email: null,
gender: null,
idCardNumber: null,
idCitizenNumber: null,
passportNumber: 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(
merchantCode: null,
fromTime: null,
toTime: null,
txnType: RequestTransactionType.all,
status: null,
page: 1,
pageSize: 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)));
}
}