Flutter粒子效果连接插件particle_connect的使用

Flutter粒子效果连接插件particle_connect的使用

开始使用

这个项目是一个用于 Flutter 的插件项目,特别包含了 Android 和/或 iOS 的平台特定实现代码。

对于 Flutter 开发的帮助,可以查看 官方文档,其中包含教程、示例、移动开发指导和完整的 API 参考。

示例代码

以下是 particle_connect 插件的一个完整示例代码:

import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:particle_connect_example/theme.dart';
import 'package:provider/provider.dart';

import 'connect_demo/connect_logic.dart';
import 'connect_demo/connect_with_wallet.dart';
import 'connect_demo/connected_wallet_oprate.dart';
import 'connect_demo/select_chain_page.dart';

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

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

  // 这个小部件是你的应用的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return OKToast(
      // 2-A: 包裹你的应用
      textStyle: const TextStyle(fontSize: 19.0, color: Colors.white),
      backgroundColor: Colors.black,
      animationCurve: Curves.easeIn,
      animationBuilder: const OffsetAnimationBuilder().call,
      animationDuration: const Duration(milliseconds: 200),
      duration: const Duration(seconds: 5),
      child: ChangeNotifierProvider(
        create: (context) => ConnectLogic(),
        child: MaterialApp(
          theme: ThemeData(primarySwatch: pnPalette),
          home: const MyHomePage(title: 'Particle'),
        ),
      ),
    );
  }
}

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

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  void initState() {
    super.initState();

    Provider.of<ConnectLogic>(context, listen: false).init();
    Provider.of<ConnectLogic>(context, listen: false).refreshConnectedAccounts();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Consumer<ConnectLogic>(
            builder: (context, provider, child) {
              return Row(
                children: [
                  Text(widget.title),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const SelectChainPage()),
                        );
                      },
                      child: Align(
                        alignment: Alignment.centerRight,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(4.0),
                              child: SizedBox(
                                width: 20.0,
                                height: 20.0,
                                child: Image.network(
                                  provider.currChainInfo.icon,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Text(
                              provider.currChainInfo.fullname,
                              textAlign: TextAlign.center,
                              style: const TextStyle(
                                fontSize: 14,
                                decoration: TextDecoration.underline,
                              ),
                            ),
                            const Icon(Icons.arrow_forward_ios, size: 14),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              );
            },
          ),
        ),
        body: Stack(
          children: [
            Consumer<ConnectLogic>(
              builder: (context, connectLogic, child) {
                if (connectLogic.connectedAccounts.isEmpty) {
                  return const Center(
                    child: Text('No connected accounts found.'),
                  );
                }
                return ListView.builder(
                  itemCount: connectLogic.connectedAccounts.length,
                  itemBuilder: (context, index) {
                    final account = connectLogic.connectedAccounts[index];
                    return InkWell(
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => WalletOpratePage(account: account),
                          ),
                        );
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Row(
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(4.0),
                              child: SizedBox(
                                width: 40.0,
                                height: 40.0,
                                child: ClipOval(
                                  child: account.icons != null && account.icons!.isNotEmpty
                                      ? Image.network(
                                          account.icons!.firstOrNull ?? '',
                                          fit: BoxFit.fill,
                                        )
                                      : const SizedBox.shrink(),
                                ),
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '${account.name}',
                                  style: const TextStyle(fontSize: 18),
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(4.0),
                                  child: Text(
                                    account.publicAddress,
                                    style: const TextStyle(fontSize: 8),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            ),
            Align(
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                    'assets/images/powerby_particle_network.webp',
                    width: 200,
                    height: 20,
                    fit: BoxFit.cover))
          ],
        ),
        floatingActionButton: SizedBox(
          height: 150,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: SizedBox(
                  width: 150,
                  child: FloatingActionButton.extended(
                    heroTag: "connectWithConnectKit",
                    onPressed: () {
                      Provider.of<ConnectLogic>(context, listen: false).connectWithConnectKit();
                    },
                    icon: const Icon(Icons.add),
                    label: const Text('ConnectKit'),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: SizedBox(
                  width: 150,
                  child: FloatingActionButton.extended(
                    heroTag: "connectWalletPage",
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => const ConnectWalletPage()),
                      );
                    },
                    icon: const Icon(Icons.add),
                    label: const Text('Connect'),
                  ),
                ),
              ),
            ],
          ),
        ));
  }
}

更多关于Flutter粒子效果连接插件particle_connect的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter粒子效果连接插件particle_connect的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用particle_connect插件来实现粒子效果的具体代码示例。particle_connect插件允许你创建和连接粒子,形成动态且吸引人的视觉效果。

首先,确保你的Flutter项目中已经添加了particle_connect依赖。在你的pubspec.yaml文件中添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  particle_connect: ^最新版本号  # 请替换为实际的最新版本号

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

接下来,在你的Flutter应用中,你可以按照以下步骤使用particle_connect插件:

  1. 导入必要的包
import 'package:flutter/material.dart';
import 'package:particle_connect/particle_connect.dart';
  1. 创建粒子效果

在你的主页面或任何你想显示粒子效果的Widget中,你可以使用ParticleConnect组件。以下是一个完整的示例代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Particle Connect Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ParticleScreen(),
    );
  }
}

class ParticleScreen extends StatefulWidget {
  @override
  _ParticleScreenState createState() => _ParticleScreenState();
}

class _ParticleScreenState extends State<ParticleScreen> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    )..repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Particle Connect Demo'),
      ),
      body: Center(
        child: ParticleConnect(
          // 配置粒子连接属性
          particles: [
            ParticleConnectData(
              x: 0.5,
              y: 0.5,
              radius: 20.0,
              color: Colors.red,
              connections: [
                ParticleConnection(
                  target: ParticleConnectData(
                    x: 0.8,
                    y: 0.2,
                    radius: 20.0,
                    color: Colors.blue,
                  ),
                  width: 2.0,
                  color: Colors.black,
                ),
              ],
            ),
            ParticleConnectData(
              x: 0.2,
              y: 0.8,
              radius: 20.0,
              color: Colors.green,
              connections: [
                ParticleConnection(
                  target: ParticleConnectData(
                    x: 0.8,
                    y: 0.8,
                    radius: 20.0,
                    color: Colors.yellow,
                  ),
                  width: 2.0,
                  color: Colors.white,
                ),
              ],
            ),
          ],
          // 可选:动画属性
          animationController: _controller,
          animationDuration: const Duration(seconds: 2),
          animationCurve: Curves.elasticInOut,
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个ParticleConnect组件。这个组件定义了两个粒子,每个粒子都有一个目标连接。我们还添加了一个动画控制器,使粒子在动画中移动。

请注意,ParticleConnectData用于定义粒子的位置和属性,而ParticleConnection用于定义粒子之间的连接。你可以根据需要调整这些属性,以实现不同的粒子效果。

确保你已经正确安装了particle_connect插件,并且你的Flutter环境配置正确,然后运行这个示例代码,你应该能看到一个简单的粒子效果。根据你的需求,你可以进一步自定义和扩展这个示例。

回到顶部