Flutter谷歌钱包集成插件flutter_google_wallet的使用
Flutter谷歌钱包集成插件flutter_google_wallet的使用
flutter_google_wallet
是一个用于在Flutter应用中集成Google Wallet功能的插件,主要支持添加票证或其他内容到Google Wallet。
特性
- 检查设备上是否可用Google Wallet。
- 将票证或任何内容添加到Google Wallet。
使用方法
基本示例
首先,在你的应用中创建一个 FlutterGoogleWalletPlugin
实例:
final flutterGoogleWalletPlugin = FlutterGoogleWalletPlugin();
然后,调用 initWalletClient
方法来初始化Android上的Wallet实例:
flutterGoogleWalletPlugin.initWalletClient();
在显示按钮之前,调用 getWalletApiAvailabilityStatus
方法检查设备是否安装了Google Wallet应用:
FutureBuilder<bool>(
future: flutterGoogleWalletPlugin.getWalletApiAvailabilityStatus(),
builder: (BuildContext context, AsyncSnapshot<bool> available) {
if (available.data ?? false) {
return AddToGoogleWalletButton(
locale: Locale('en', 'US'),
onPress: () {
flutterGoogleWalletPlugin.savePasses(
jsonPass: exampleJsonPass,
addToGoogleWalletRequestCode: 2);
});
} else {
return const SizedBox.shrink();
}
},
),
还需要将Google Wallet按钮的SVG文件添加到 pubspec.yaml
的资源中:
assets:
- packages/flutter_google_wallet/assets/svg/button/frFR_add_to_google_wallet_wallet-button.svg
- packages/flutter_google_wallet/assets/svg/button/enUS_add_to_google_wallet_wallet-button.svg
并在 MaterialApp
中添加本地化配置:
localizationsDelegates: const [
I18nGoogleWallet.delegate,
],
完整示例代码
以下是一个完整的示例代码,展示如何在Flutter应用中集成Google Wallet功能:
import 'package:flutter/material.dart';
import 'package:flutter_google_wallet/flutter_google_wallet_plugin.dart';
import 'package:flutter_google_wallet/generated/l10n.dart';
import 'package:flutter_google_wallet/widget/add_to_google_wallet_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
final flutterGoogleWalletPlugin = FlutterGoogleWalletPlugin();
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String _platformVersion = 'Unknown';
late Future<bool> _isWalletAvailable;
@override
void initState() {
super.initState();
_isWalletAvailable = Future(() async {
await widget.flutterGoogleWalletPlugin.initWalletClient();
return widget.flutterGoogleWalletPlugin.getWalletApiAvailabilityStatus();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: const [I18nGoogleWallet.delegate],
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
Text('Running on: $_platformVersion\n'),
FutureBuilder<bool>(
future: _isWalletAvailable,
builder: (BuildContext context, AsyncSnapshot<bool> available) {
if (available.data == true) {
return Padding(
padding: const EdgeInsets.all(10),
child: Align(
alignment: Alignment.topCenter,
child: AddToGoogleWalletButton(
locale: const Locale('fr', 'FR'),
onPress: () {
widget.flutterGoogleWalletPlugin.savePasses(
jsonPass: exampleJsonPass,
addToGoogleWalletRequestCode: 2);
},
),
),
);
} else {
return const SizedBox.shrink();
}
},
),
],
),
),
),
);
}
}
// 这是一个带有虚假数据的pass,当添加到Google Wallet时会触发错误
// 对于真实数据的pass,请遵循Google开发者指南:https://developers.google.com/wallet
const exampleJsonPass = '''
{
"aud": "google",
"origins": [
"https://localhost:8080"
],
"iss": "123456789@developer.gserviceaccount.com",
"iat": 123456789,
"typ": "savetowallet",
"payload": {
"loyaltyObjects": [
{
"barcode": {
"alternateText": "12345",
"type": "qrCode",
"value": "28343E3"
},
"id": "123456789.LoyaltyObject",
"loyaltyPoints": {
"balance": {
"string": "500"
},
"label": "Points"
},
"accountId": "1234567890",
"classId": "123456789.LoyaltyClass",
"accountName": "Jane Doe",
"state": "active",
"version": 1
}
]
}
}
''';
通过以上步骤和代码示例,你可以在Flutter应用中轻松集成Google Wallet功能,并为用户提供添加票证等便捷服务。
更多关于Flutter谷歌钱包集成插件flutter_google_wallet的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复