knotapi_flutter 2.0.0
knotapi_flutter: ^2.0.0 copied to clipboard
Change card on file information, retrieve transactions, and more via Knot.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:knotapi_flutter/events.dart';
import 'package:knotapi_flutter/knotapi_configuration.dart';
import 'package:knotapi_flutter/knotapi_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 _knotapiFlutterPlugin = KnotapiFlutter();
StreamSubscription<KnotEvent>? _streamEvent;
StreamSubscription<KnotSuccess>? _streamSuccess;
StreamSubscription<KnotError>? _streamError;
StreamSubscription<KnotExit>? _streamExit;
// Locale selection
static const List<String> _supportedLocales = [
'en-US',
'es-US',
'es-MX',
'fr-US',
'fr-CA',
'zh-US',
];
String _selectedLocale = 'en-US';
void _onSuccess (KnotSuccess event) {
String merchant = event.merchant;
print("eventName: onSuccess, merchant: $merchant");
}
void _onError (KnotError event) {
String errorCode = event.errorCode;
String errorDescription = event.errorDescription;
print("eventName: onError, Error: $errorCode, Message: $errorDescription");
}
void _onEvent (KnotEvent event) {
String? product = event.product;
String? merchant = event.merchant;
String? merchantId = event.merchantId;
String name = event.event;
String? taskId = event.taskId;
String environment = event.environment;
Map<String, Object?> metaData = event.metaData;
print("eventName: onEvent, product: $product, environment: $environment, merchant: $merchant,${merchantId != null ? 'merchantId: $merchantId,' : ''} event: $name,${taskId != null ? ' taskId: $taskId,' : ''} metaData: $metaData");
}
void _onExit (KnotExit event) {
print("eventName: onExit");
}
@override
void initState() {
super.initState();
_streamError = KnotapiFlutter.onError.listen(_onError);
_streamEvent = KnotapiFlutter.onEvent.listen(_onEvent);
_streamExit = KnotapiFlutter.onExit.listen(_onExit);
_streamSuccess = KnotapiFlutter.onSuccess.listen(_onSuccess);
}
@override
void dispose(){
super.dispose();
_streamSuccess?.cancel();
_streamExit?.cancel();
_streamEvent?.cancel();
_streamError?.cancel();
}
void onOpenCardOnFileSwitcher() {
// ignore: unused_local_variable
CustomerConfiguration customerConfig = CustomerConfiguration(
cardName: "debit card",
customerName: "paypal",
logoId: "44"
);
KnotConfiguration knotConfiguration = KnotConfiguration(
sessionId: "248c5601-7472-4e6e-a913-d7cdd78ab754",
clientId: "61fc568a-bf03-4d3c-9b06-106445808170",
environment: Environment.production,
// product: Product.cardSwitcher,
useCategories: false,
locale: _selectedLocale,
// If you want use customer configuration, uncomment the next line
// customerConfiguration: customerConfig
);
_knotapiFlutterPlugin.open(knotConfiguration);
}
void onOpenTransactionLink() {
_knotapiFlutterPlugin.open(KnotConfiguration(
sessionId: "695ce724-7f61-40f1-a410-cb0c0fcf9b7f",
clientId: "3f4acb6b-a8c9-47bc-820c-b0eaf24ee771",
environment: Environment.development,
product: Product.transactionLink,
merchantIds: [17],
useCategories: false,
locale: _selectedLocale));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.black,
appBarTheme: const AppBarTheme(color: Colors.black),
buttonTheme: const ButtonThemeData(buttonColor: Colors.black),
colorScheme: const ColorScheme(
background: Colors.white,
brightness: Brightness.light,
primary: Colors.black,
onPrimary: Colors.white,
secondary: Colors.white,
onSecondary: Colors.white,
error: Colors.redAccent,
onError: Colors.redAccent,
onBackground: Colors.black,
surface: Colors.white,
onSurface: Colors.black)),
home: Scaffold(
appBar: AppBar(
title: const Text('KnotAPI Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Locale Selector
Padding(
padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Locale: ', style: TextStyle(fontSize: 16)),
const SizedBox(width: 10),
DropdownButton<String>(
value: _selectedLocale,
onChanged: (String? newValue) {
if (newValue != null) {
setState(() {
_selectedLocale = newValue;
});
}
},
items: _supportedLocales.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: onOpenCardOnFileSwitcher,
child: const Text('Open Card On File Switcher'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: onOpenTransactionLink,
child: const Text('Open Transaction Link')),
],
),
),
),
);
}
}