Flutter华为钱包集成插件huawei_wallet的使用

Flutter华为钱包集成插件huawei_wallet的使用

安装

请访问 pub.devAppGallery Connect 配置 获取详细安装说明。

dependencies:
  huawei_wallet: ^版本号

文档

问题或建议

如果您在使用 HMS 示例时遇到任何问题,可以尝试以下选项:

如果您在示例中遇到任何 Bug,请提交到 GitHub 仓库

许可证

Huawei Wallet Flutter 插件遵循 Apache 2.0 许可证


示例代码

/*
    Copyright 2021-2023. Huawei Technologies Co., Ltd. All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License")
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

import 'package:flutter/material.dart';
import 'package:huawei_wallet_example/pages/sdk_page.dart';

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

class _App extends StatelessWidget {
  const _App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const Home(),
      theme: ThemeData(
        appBarTheme: const AppBarTheme(
          color: Colors.black87,
        ),
      ),
    );
  }
}

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

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Huawei Wallet Kit Demo'),
      ),
      body: ListView(
        children: [
          MaterialButton(
            color: Colors.grey,
            padding: const EdgeInsets.symmetric(horizontal: 24),
            onPressed: () async {
              Navigator.of(context).push(
                MaterialPageRoute<dynamic>(
                  builder: (_) => const SdkPage(),
                ),
              );
            },
            child: const Text(
              '通过网页、短信、邮件和应用获取优惠券',
            ),
          ),
        ],
      ),
    );
  }
}

代码解释

  1. 导入必要的库

    import 'package:flutter/material.dart';
    import 'package:huawei_wallet_example/pages/sdk_page.dart';
    
  2. 主函数

    void main() {
      runApp(const _App());
    }
    
  3. 创建应用入口类 _App

    class _App extends StatelessWidget {
      const _App({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: const Home(),
          theme: ThemeData(
            appBarTheme: const AppBarTheme(
              color: Colors.black87,
            ),
          ),
        );
      }
    }
    
  4. 创建首页 Home

    class Home extends StatefulWidget {
      const Home({Key? key}) : super(key: key);
    
      @override
      State<Home> createState() => _HomeState();
    }
    
  5. 实现首页状态 _HomeState

    class _HomeState extends State<Home> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Huawei Wallet Kit Demo'),
          ),
          body: ListView(
            children: [
              MaterialButton(
                color: Colors.grey,
                padding: const EdgeInsets.symmetric(horizontal: 24),
                onPressed: () async {
                  Navigator.of(context).push(
                    MaterialPageRoute<dynamic>(
                      builder: (_) => const SdkPage(),
                    ),
                  );
                },
                child: const Text(
                  '通过网页、短信、邮件和应用获取优惠券',
                ),
              ),
            ],
          ),
        );
      }
    }
    

更多关于Flutter华为钱包集成插件huawei_wallet的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter华为钱包集成插件huawei_wallet的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


要在Flutter应用中集成华为钱包(Huawei Wallet)功能,你可以使用huawei_wallet插件。以下是如何使用该插件的详细步骤:

1. 添加依赖

首先,在pubspec.yaml文件中添加huawei_wallet插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  huawei_wallet: ^1.0.0  # 请使用最新版本

然后运行flutter pub get来安装依赖。

2. 配置华为开发者账号

确保你已经注册了华为开发者账号,并在华为开发者控制台中创建了应用。你需要获取到应用的App IDApp Secret

3. 初始化插件

在Flutter应用的入口处(通常是main.dart),初始化huawei_wallet插件。

import 'package:flutter/material.dart';
import 'package:huawei_wallet/huawei_wallet.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化华为钱包插件
  await HuaweiWallet.init(
    appId: 'your_app_id',  // 替换为你的App ID
    appSecret: 'your_app_secret',  // 替换为你的App Secret
  );
  
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Huawei Wallet Demo',
      home: WalletDemo(),
    );
  }
}

4. 添加卡券到华为钱包

你可以使用HuaweiWallet.addPass方法将卡券添加到华为钱包。以下是一个示例:

import 'package:flutter/material.dart';
import 'package:huawei_wallet/huawei_wallet.dart';

class WalletDemo extends StatelessWidget {
  Future<void> _addPassToWallet() async {
    try {
      // 定义卡券信息
      final pass = Pass(
        passType: 'loyalty',  // 卡券类型,例如:loyalty, coupon, ticket, etc.
        passData: {
          'organizationName': 'Example Company',
          'description': 'Example Loyalty Card',
          'cardNumber': '1234567890',
          // 其他卡券字段
        },
        passStyle: PassStyle(
          backgroundColor: '#FFFFFF',
          foregroundColor: '#000000',
          labelColor: '#FF0000',
          // 其他样式字段
        ),
      );

      // 添加卡券到华为钱包
      final result = await HuaweiWallet.addPass(pass);
      if (result) {
        print('Pass added to Huawei Wallet successfully');
      } else {
        print('Failed to add pass to Huawei Wallet');
      }
    } catch (e) {
      print('Error adding pass to Huawei Wallet: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Huawei Wallet Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _addPassToWallet,
          child: Text('Add Pass to Huawei Wallet'),
        ),
      ),
    );
  }
}
回到顶部