Flutter插件flutter_msitef_plugin的使用方法
Flutter插件flutter_msitef_plugin的使用方法
本项目是一个用于Flutter的插件包的起点,该插件包包含特定于Android的实现代码。此插件展示了与SiTef(电子资金转账系统)的通信。
目录
开始使用Flutter插件flutter_msitef_plugin
本项目是Flutter的一个插件包的起点,该插件包包含特定于Android和/或iOS的平台特定实现代码。
要开始进行Flutter开发,请参阅在线文档,其中提供了教程、示例、移动开发指南以及完整的API参考。
要开始使用M-Sitef,请参阅M-Sitef在线文档,其中提供了教程、示例和开发及使用指导。
功能
- 与SiTef通信
- 支持Android
需求
- Flutter 3.3.0 或更高版本
- Dart 3.5.0 或更高版本
安装
- 将此存储库克隆到本地机器:
git clone git@github.com:unisystemdev/flutter-msitef-plugin.git
- 导航到克隆的存储库:
cd flutter-msitef-plugin
- 清理项目:
flutter clean
- 安装依赖项:
flutter pub get
- 运行项目:
flutter run
使用
导入插件
在Dart文件中导入插件:
import 'package:flutter_msitef_plugin/flutter_msitef_plugin.dart';
示例代码
以下是使用插件的示例代码:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:async';
import 'package:flutter_msitef_plugin/flutter_msitef_plugin.dart';
// 主应用函数
void main() {
runApp(const MyApp());
}
// 主应用类
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
home: IntentForm(), // 设置初始页面
);
}
}
// 表单组件类
class IntentForm extends StatefulWidget {
const IntentForm({super.key});
[@override](/user/override)
_IntentFormState createState() => _IntentFormState();
}
class _IntentFormState extends State<IntentForm> {
final _flutterMsitefPlugin = FlutterMsitefPlugin(); // 插件实例
final _formKey = GlobalKey<FormState>(); // 表单键
String tipoPinpad = "";
String tipoParcelamento = "NENHUM";
// 文本控制器映射表
final Map<String, TextEditingController> controllers = {
"empresaSitef": TextEditingController(text: "00000000"),
"enderecoSitef": TextEditingController(text: "192.168.1.100"),
"CNPJ_CPF": TextEditingController(text: "00000000000000"),
"cnpj_automacao": TextEditingController(text: "68884084000107"),
"comExterna": TextEditingController(text: "0"),
"otp": TextEditingController(text: ""),
"tokenRegistroTls": TextEditingController(text: ""),
"operador": TextEditingController(text: "0001"),
"numeroCupom": TextEditingController(text: "1"),
"valor": TextEditingController(text: "9000"),
"numParcelas": TextEditingController(text: "1"),
"timeoutColeta": TextEditingController(text: "30"),
};
// 获取表单值
Map<String, String> getFormValues() {
final formValues = controllers.map((key, controller) => MapEntry(key, controller.text));
formValues['tipoPinpad'] = tipoPinpad;
formValues['tipoParcelamento'] = tipoParcelamento;
return formValues;
}
// 成功回调函数
Future<void> msitefSuccess(MSitefResponse response) {
final String message = 'CODRESP: ${response.codresp} \n\n'
'CODTRANS: ${response.codtrans} \n\n'
'TIPO_PARC: ${response.tipoParc} \n\n'
'REDE_AUT: ${response.redeAut} \n\n'
'BANDEIRA: ${response.bandeira} \n\n'
'NSU_SITEF: ${response.nsuSitef} \n\n'
'NSU_HOST: ${response.nsuHost} \n\n'
'NUM_PARC: ${response.numParc}';
showToast("SUCCESS", message);
return Future.value();
}
// 失败回调函数
Future<void> msitefFail(MSitefResponseFail response) {
final message = "Resultado m-SiTef FAIL: ${response.codresp} - ${response.message} ";
showToast('ERROR', message);
return Future.value();
}
// 启动管理交易
void msitefAdm() async {
final formData = getFormValues();
await _flutterMsitefPlugin.msitefAdm(
params: formData,
callback: msitefSuccess,
callbackFail: msitefFail,
);
}
// 启动信用销售
void msitefVendaCredito() async {
final formData = getFormValues();
await _flutterMsitefPlugin.msitefCredito(
params: formData,
callback: msitefSuccess,
callbackFail: msitefFail,
);
}
// 启动借记销售
void msitefVendaDebito() async {
final formData = getFormValues();
await _flutterMsitefPlugin.msitefDebito(
params: formData,
callback: msitefSuccess,
callbackFail: msitefFail,
);
}
// 启动Pix支付
void msitefPix() async {
final formData = getFormValues();
await _flutterMsitefPlugin.msitefPix(
params: formData,
callback: msitefSuccess,
callbackFail: msitefFail,
);
}
// 取消交易
void msitefCancel() async {
final formData = getFormValues();
await _flutterMsitefPlugin.msitefCancelamento(
params: formData,
callback: msitefSuccess,
callbackFail: msitefFail,
);
}
// 执行带异常处理的函数
void executeWithExceptionHandling(Function action) {
try {
action();
} catch (e) {
showToast('ERROR', '$e');
}
}
// 显示Toast
void showToast(String tipo, String message) {
Color backgroundColor;
ToastGravity gravity;
switch (tipo) {
case 'SUCCESS':
backgroundColor = Colors.green;
gravity = ToastGravity.CENTER;
break;
case 'ERROR':
backgroundColor = Colors.red;
gravity = ToastGravity.BOTTOM;
break;
default:
backgroundColor = Colors.grey; // 默认颜色
gravity = ToastGravity.TOP;
}
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_LONG,
gravity: gravity,
backgroundColor: backgroundColor,
textColor: Colors.white,
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('mSitef: demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: [
...controllers.entries.map((entry) {
return TextFormField(
controller: entry.value,
decoration: InputDecoration(labelText: entry.key),
);
}),
DropdownButtonFormField<String>(
value: tipoPinpad,
decoration: const InputDecoration(labelText: "tipoPinpad"),
items: ["", "ANDROID_USB", "ANDROID_BT"].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
tipoPinpad = newValue!;
});
},
),
DropdownButtonFormField<String>(
value: tipoParcelamento,
decoration: const InputDecoration(labelText: "tipoParcelamento"),
items: ["NENHUM", "LOJA", "ADM"].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
tipoParcelamento = newValue!;
});
},
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => executeWithExceptionHandling(msitefAdm),
child: const Text('ADM'),
),
ElevatedButton(
onPressed: () => executeWithExceptionHandling(msitefVendaCredito),
child: const Text('VendaCredito'),
),
ElevatedButton(
onPressed: () => executeWithExceptionHandling(msitefVendaDebito),
child: const Text('VendaDebito'),
),
ElevatedButton(
onPressed: () => executeWithExceptionHandling(msitefPix),
child: const Text('Pix'),
),
ElevatedButton(
onPressed: () => executeWithExceptionHandling(msitefCancel),
child: const Text('Cancelamento'),
),
],
),
),
),
),
);
}
[@override](/user/override)
void dispose() {
controllers.forEach((key, controller) {
controller.dispose(); // 释放文本控制器资源
});
super.dispose();
}
}
更多关于Flutter插件flutter_msitef_plugin的使用方法的实战教程也可以访问 https://www.itying.com/category-92-b0.html