Flutte插件solwave_dart的使用方法分享

Flutte插件solwave_dart的使用方法分享

Solwave 是一个 Dart SDK,它使 Solana 移动去中心化应用程序(dApps)能够实现应用内交易。通过使用此 SDK,开发者可以在其移动应用程序中实现应用内交易,而无需为用户构建独立的钱包。

示例代码

以下是使用 solwave_dart 插件的完整示例代码:

// 导入必要的库
import 'package:example/helpers.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'package:solwave_dart/solwave_dart.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    Solwave.instance.init(
      apiKey: 'YOUR_API_KEY', // 替换为您的 API 密钥
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Solwave Flutter',
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.black87,
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Solwave Flutter'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool isWalletAvailable = false;
  WalletEntity? wallet;
  int balance = 0;

  getWallet() async {
    final w = await Solwave.instance.getUserWallet();
    if (w != null) {
      final b = await getBalance(w.publicAddress!);

      setState(() {
        wallet = w;
        balance = b;
        isWalletAvailable = true;
      });
    }
  }

  @override
  void initState() {
    getWallet();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color(0xFF111218), Color(0xFF111218)],
          ),
        ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Stack(
            children: [
              Align(
                alignment: Alignment.topRight,
                child: SvgPicture.asset(
                  'assets/rhombus.svg',
                  width: MediaQuery.of(context).size.width * 0.8,
                  fit: BoxFit.fitWidth,
                ),
              ),
              Align(
                alignment: Alignment.centerLeft,
                child: Transform.scale(
                  scale: -1.0,
                  child: SvgPicture.asset(
                    'assets/rhombus.svg',
                    width: MediaQuery.of(context).size.width * 0.8,
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
              Align(
                alignment: Alignment.bottomCenter,
                child: Text(
                  '© $currentYear Saganize',
                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    fontSize: 13,
                    color: Colors.white60,
                    fontFamily: 'Inter',
                    fontWeight: FontWeight.w300,
                  ),
                ),
              ),
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16),
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const Text(
                                'Solwave',
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  fontSize: 18,
                                  color: Colors.white,
                                  fontFamily: 'Inter',
                                  fontWeight: FontWeight.w300,
                                ),
                              ),
                              SvgPicture.asset(
                                'assets/pbsaganize.svg',
                                fit: BoxFit.fitWidth,
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              SocialIcon(
                                icon: 'assets/github.svg',
                                onPress: () {
                                  launchURL('https://github.com/Saganize');
                                },
                              ),
                              SocialIcon(
                                icon: 'assets/twitter.svg',
                                onPress: () {
                                  launchURL('https://twitter.com/saganize');
                                },
                              ),
                              SocialIcon(
                                icon: 'assets/saga.svg',
                                onPress: () {
                                  launchURL('https://saganize.com/');
                                },
                              ),
                            ],
                          ),
                        ],
                      ),
                      SizedBox(
                        height: MediaQuery.of(context).size.height * 0.05,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          GestureDetector(
                            onTap: () {
                              Solwave.instance.selectWallet(
                                context,
                                onWalletSelection: (wallet) {
                                  getWallet();
                                },
                              );
                            },
                            child: Container(
                              decoration: BoxDecoration(
                                border: Border(
                                  top: BorderSide(
                                    color: Colors.grey[500]!,
                                    width: 1.5,
                                  ),
                                  left: BorderSide(
                                    color: Colors.grey[500]!,
                                    width: 1.5,
                                  ),
                                  right: BorderSide(
                                    color: Colors.grey[500]!,
                                    width: 1.5,
                                  ),
                                ),
                                boxShadow: [
                                  BoxShadow(
                                    color: const Color(0xFF111220)
                                        .withOpacity(0.35),
                                    blurRadius: 35,
                                    offset: const Offset(0, 30),
                                  ),
                                ],
                                borderRadius: BorderRadius.circular(15),
                              ),
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 12, vertical: 14),
                              child: Text(
                                !isWalletAvailable
                                    ? 'Select Wallet'
                                    : truncateString(wallet!.publicAddress!),
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.white,
                                  fontFamily: 'Inter',
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                          Text(
                            '${balance / 1000000000} SOL',
                            textAlign: TextAlign.center,
                            style: const TextStyle(
                              fontSize: 16,
                              color: Colors.white,
                              fontFamily: 'Inter',
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: MediaQuery.of(context).size.height * 0.06,
                      ),
                      const Text(
                        'Solwave Flutter Scaffold',
                        style: TextStyle(
                          fontSize: 28,
                          color: Colors.white,
                          fontFamily: 'Rubik',
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      const Text(
                        'Unleash the full power of Blockchain with \nSolana and Flutter.',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.white60,
                          fontFamily: 'Inter',
                          fontWeight: FontWeight.w300,
                        ),
                      ),
                      SizedBox(
                        height: MediaQuery.of(context).size.height * 0.25,
                      ),
                      SolwaveButton(
                        label: 'AIRDROP 1 SOL',
                        onPressed: !isWalletAvailable
                            ? null
                            : () async {
                                await requestAirdrop(wallet!.publicAddress!);
                                getWallet();
                              },
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                      SolwaveButton(
                        label: 'SIGN MESSAGE',
                        onPressed: !isWalletAvailable
                            ? null
                            : () {
                                Solwave.instance.signMessage(context,
                                    message:
                                        '''The quick brown fox jumps over the lazy dog''',
                                    onMessageSigned: (signature, message) {
                                  debugPrint('Signature callback $signature');
                                });
                              },
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                      SolwaveButton(
                        label: 'SEND TRANSACTION',
                        onPressed: !isWalletAvailable
                            ? null
                            : () {
                                Solwave.instance.sendTransaction(
                                  context,
                                  transaction: SolanaTransaction(
                                    instructions: [
                                      SystemInstruction.transfer(
                                        fundingAccount:
                                            Ed25519HDPublicKey.fromBase58(
                                                wallet!.publicAddress!),
                                        recipientAccount:
                                            Ed25519HDPublicKey.fromBase58(
                                                'Bu3mTU2X7SoZUkyNU37jispVqRLkSSwiQuN6rGbvQx9f'),
                                        lamports: 100000,
                                      )
                                    ],
                                  ),
                                  onTransacitonComplete: (_) {
                                    getWallet();
                                  },
                                );
                              },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class SolwaveButton extends StatelessWidget {
  final String label;
  final VoidCallback? onPressed;
  const SolwaveButton({
    super.key,
    required this.label,
    required this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20.0),
      child: Container(
        decoration: BoxDecoration(
            border: Border(
              top: BorderSide(color: Colors.white.withOpacity(0.7), width: 1.5),
            ),
            borderRadius: BorderRadius.circular(15)),
        child: TextButton(
          style: ButtonStyle(
            minimumSize: MaterialStateProperty.all<Size>(
              const Size.fromHeight(50),
            ),
            elevation: const MaterialStatePropertyAll(2.0),
            backgroundColor: MaterialStateProperty.all<Color>(
                onPressed == null ? Colors.white30 : const Color(0xff2380EA)),
            shape: MaterialStateProperty.all<RoundedRectangleBorder>(
              RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0),
              ),
            ),
          ),
          onPressed: onPressed,
          child: Text(
            label,
            style: const TextStyle(
                color: Colors.white,
                fontFamily: 'Inter',
                fontWeight: FontWeight.bold,
                fontSize: 16),
          ),
        ),
      ),
    );
  }
}

class SocialIcon extends StatelessWidget {
  final String icon;
  final VoidCallback onPress;
  const SocialIcon({
    Key? key,
    required this.icon,
    required this.onPress,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 6.0),
      child: GestureDetector(
        onTap: onPress,
        child: SvgPicture.asset(
          icon,
          fit: BoxFit.fitWidth,
        ),
      ),
    );
  }
}

功能说明

  1. 初始化 SDK

    Solwave.instance.init(
      apiKey: 'YOUR_API_KEY', // 替换为您的 API 密钥
    );
    
  2. 获取用户钱包

    final w = await Solwave.instance.getUserWallet();
    
  3. 请求空投

    await requestAirdrop(wallet!.publicAddress!);
    
  4. 签署消息

    Solwave.instance.signMessage(context, message: 'Your message here', onMessageSigned: (signature, message) { debugPrint('Signature callback $signature'); });
    
  5. 发送交易

    Solwave.instance.sendTransaction(
      context,
      transaction: SolanaTransaction(
        instructions: [
          SystemInstruction.transfer(
            fundingAccount: Ed25519HDPublicKey.fromBase58(wallet!.publicAddress!),
            recipientAccount: Ed25519HDPublicKey.fromBase58('Recipient address'),
            lamports: 100000,
          )
        ],
      ),
      onTransacitonComplete: (_) {
        getWallet();
      },
    );

1 回复

solwave_dart 是一个针对 Flutter 的插件,可能与 Solana 区块链相关。Solana 是一个高性能区块链平台,专注于快速交易和低费用。如果 solwave_dart 是一个与 Solana 相关的插件,它可能提供了与 Solana 区块链交互的功能,例如创建钱包、发送交易、查询账户信息等。

以下是 solwave_dart 插件的一些潜在使用场景和功能:

1. 钱包管理

  • 创建钱包:生成新的 Solana 钱包地址和私钥。
  • 导入钱包:通过私钥或助记词导入现有的 Solana 钱包。
  • 管理钱包:查看钱包余额、交易历史等。

2. 交易处理

  • 发送交易:在 Solana 区块链上发送 SOL(Solana 的原生代币)或其他代币。
  • 签名交易:使用私钥对交易进行签名。
  • 查询交易状态:查询特定交易的确认状态。

3. 智能合约交互

  • 部署智能合约:在 Solana 区块链上部署新的智能合约。
  • 调用智能合约:与已部署的智能合约进行交互,执行合约函数。

4. 账户管理

  • 查询账户信息:获取指定 Solana 账户的余额、代币持有等信息。
  • 创建账户:在 Solana 区块链上创建新的账户。

5. 代币管理

  • 创建代币:在 Solana 区块链上创建新的 SPL 代币。
  • 管理代币:转账、查询代币余额等。

6. 网络交互

  • 选择网络:连接到 Solana 的主网、测试网或开发网。
  • 查询区块链信息:获取区块链的最新区块高度、节点信息等。

7. 事件监听

  • 监听交易:监听特定地址的交易事件。
  • 监听区块:监听新区块的生成。

8. 安全功能

  • 加密存储:安全地存储私钥和助记词。
  • 身份验证:实现用户身份验证和授权功能。

9. DApp 集成

  • 集成 DApp:将 solwave_dart 集成到去中心化应用(DApp)中,实现与 Solana 区块链的交互。

10. 跨平台支持

  • 多平台支持solwave_dart 可能支持 iOS、Android 和 Web 平台,使开发者能够跨平台构建区块链应用。

示例代码(假设用法)

import 'package:solwave_dart/solwave_dart.dart';

void main() async {
  // 初始化插件
  final solwave = SolwaveDart();

  // 创建新钱包
  final wallet = await solwave.createWallet();
  print('Wallet Address: ${wallet.address}');
  print('Private Key: ${wallet.privateKey}');

  // 查询余额
  final balance = await solwave.getBalance(wallet.address);
  print('Balance: $balance SOL');

  // 发送交易
  final txId = await solwave.sendTransaction(
    sender: wallet.privateKey,
    receiver: 'RECEIVER_ADDRESS',
    amount: 1.0,
  );
  print('Transaction ID: $txId');
}
回到顶部