Flutter生成以太坊地址标识图标插件blockies的使用

Flutter生成以太坊地址标识图标插件blockies的使用

pub.dev Dart CI Stars Issues MIT License

Blockies

Blockies库用于生成一种称为Identicon的小图标。Identicon是一种基于哈希值(如区块链地址)的可视化表示,用于标识计算机系统中的用户作为头像,同时保护用户的隐私。

默认情况下,生成的Identicon是10x10个块。所有颜色都是根据种子生成的,除非手动覆盖。Blocky的大小取决于父部件的大小。

使用方法

// 创建一个Blockies实例
Blockies(
  seed: '0xe95C0ed548f63B181f6528B8e3c57a7c93C2E3Dc', // 用于生成identicon的种子
  color: Color(0xFFD95030),     // 手动指定图标颜色,默认为随机颜色
  bgColor: Color(0x000000),     // 选择不同的背景颜色,默认基于种子随机生成
  spotColor: Color(0xFFE6D690), // 选择不同的斑点颜色,默认基于种子随机生成
  size: 8,                      // 图标宽度和高度的块数,默认为10
);

变更日志

有关最近的变更信息,请参阅 CHANGELOG

致谢

许可证

该软件使用MIT许可证。更多信息请参阅 许可证文件

示例代码

import 'package:flutter/material.dart';

import 'package:blockies/blockies.dart';

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

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

  // 这是你的应用的根部件
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Blockies Demo',
      home: const BlockiesPage(),
    );
  }
}

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

  [@override](/user/override)
  State<BlockiesPage> createState() => _BlockiesPageState();
}

class _BlockiesPageState extends State<BlockiesPage> {
  final _formKey = GlobalKey<FormState>();

  final seedTextController =
      TextEditingController(text: '0xe95C0ed548f63B181f6528B8e3c57a7c93C2E3Dc');
  final sizeTextController = TextEditingController(text: '10');

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Blockies demo')),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                TextFormField(
                  controller: seedTextController,
                  maxLength: 100,
                  validator: (value) {
                    if (value == null || value.isEmpty)
                      return 'Can\'t be empty';
                    return null;
                  },
                  decoration: InputDecoration(
                    label: Text('Seed'),
                    hintText:
                        'A string used to generate the randomized identicon',
                  ),
                  onChanged: (value) {},
                ),
                TextFormField(
                  controller: sizeTextController,
                  maxLength: 3,
                  validator: (value) {
                    if (value == null || value.isEmpty)
                      return 'Can\'t be empty';
                    if (int.tryParse(value) == null) return 'Has to be number';
                    return null;
                  },
                  decoration: InputDecoration(
                    label: Text('Size'),
                    hintText: 'The resulting blocky will be SIZExSIZE',
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 24),
                  child: ElevatedButton(
                      child: Text('Generate Blocky!'),
                      onPressed: () {
                        if (_formKey.currentState!.validate()) {
                          setState(() {});
                        }
                      }),
                ),
                Container(
                  padding: const EdgeInsets.only(top: 24),
                  width: MediaQuery.of(context).size.width / 2,
                  child: AspectRatio(
                    aspectRatio: 1,
                    child: Blockies(
                      seed: seedTextController.text,
                      size: int.parse(sizeTextController.text),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter生成以太坊地址标识图标插件blockies的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter生成以太坊地址标识图标插件blockies的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


blockies 是一个用于生成以太坊地址标识图标的插件,类似于MetaMask中显示的地址头像。它可以根据以太坊地址生成一个独特的、基于哈希值的图标。在Flutter中,你可以使用 flutter_blockies 插件来实现类似的功能。

安装 flutter_blockies 插件

首先,你需要在 pubspec.yaml 文件中添加 flutter_blockies 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_blockies: ^1.0.0

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

使用 flutter_blockies 插件

安装完成后,你可以在你的Flutter应用中使用 Blockies 组件来生成以太坊地址的标识图标。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Blockies Example'),
        ),
        body: Center(
          child: Blockies(
            seed: '0x1234567890abcdef1234567890abcdef12345678', // Ethereum address
            size: 10, // Number of blocks (default is 10)
            scale: 10, // Scale factor (default is 10)
            color: Colors.blue, // Base color
            bgColor: Colors.white, // Background color
            spotColor: Colors.red, // Spot color
          ),
        ),
      ),
    );
  }
}
回到顶部