Flutter微信图标集成插件wx_icon的使用

Flutter微信图标集成插件wx_icon的使用

设计令人惊叹的图标,通过流畅的动画轻松地在不同状态之间切换。我们的直观API使您能够轻松创建美观且交互性强的元素,用于您的Flutter应用程序。

预览

示例

使用方法

要了解有关wx_icon使用的类和其他参考,请参阅API文档

WxIcon(
  Icons.star,
  color: _color,
  opacity: _opacity,
  size: _size,
  scale: _scale,
  rotate: _rotate,
  flipX: _flipX,
  flipY: _flipY,
)

IconButton(
  onPressed: _toggleSelected,
  icon: WxToggleIcon(
    Icons.star,
    selected: _selected,
    style: WxDrivenToggleIconStyle.scaleDown(
      size: 24,
      rotate: 45,
      selectedStyle: const WxToggleIconStyle(rotate: 0),
    ),
  ),
)

示例代码

以下是一个完整的示例代码,展示了如何使用wx_icon插件来创建具有动画效果的图标:

import 'package:flutter/material.dart';
import 'package:wx_icon/wx_icon.dart';
import 'package:choice/choice.dart';
import 'package:animated_checkmark/animated_checkmark.dart';
import 'package:wx_text/wx_text.dart';
import 'dart:math';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Widgetarian Icon',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: false,
      ),
      home: const MyHomePage(title: 'Widgetarian Icon'),
    );
  }
}

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> {
  final _random = Random();

  final _icons = [
    Icons.star,
    Icons.favorite,
    Icons.bookmark,
  ];
  int _icon = 0;

  Color? _color;
  double _opacity = 1.0;
  double _size = 64;
  double _rotate = 0;
  double _scale = 1;
  bool _flipX = false;
  bool _flipY = false;

  void changeIcon() {
    final maxIndex = _icons.length;
    int randomIndex;
    do {
      randomIndex = 0 + _random.nextInt(maxIndex - 0);
    } while (_icon == randomIndex);
    setState(() {
      _icon = randomIndex;
    });
  }

  void setColor(Color color) {
    setState(() => _color = color);
  }

  void setOpacity(double opacity) {
    setState(() => _opacity = opacity);
  }

  void setSize(double size) {
    setState(() => _size = size);
  }

  void _increaseRotate() {
    setState(() {
      _rotate += 45;
    });
  }

  void _decreaseRotate() {
    setState(() {
      _rotate -= 45;
    });
  }

  void _increaseScale() {
    setState(() {
      _scale += .5;
    });
  }

  void _decreaseScale() {
    setState(() {
      _scale -= .5;
    });
  }

  void _toggleFlipX() {
    setState(() {
      _flipX = !_flipX;
    });
  }

  void _toggleFlipY() {
    setState(() {
      _flipY = !_flipY;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Wrapper(
      children: [
        Example(
          title: 'WxIcon',
          child: Column(
            children: <Widget>[
              InkWell(
                onTap: changeIcon,
                child: WxIcon(
                  _icons[_icon],
                  color: _color,
                  opacity: _opacity,
                  size: _size,
                  scale: _scale,
                  rotate: _rotate,
                  flipX: _flipX,
                  flipY: _flipY,
                ),
              ),
              const SizedBox(height: 20),
              SizedBox(
                width: 250,
                child: Slider(
                  value: _size,
                  max: 200,
                  min: 10,
                  label: _size.round().toString(),
                  onChanged: setSize,
                ),
              ),
              SizedBox(
                width: 250,
                child: Slider(
                  value: _opacity,
                  max: 1,
                  min: 0,
                  label: _opacity.round().toString(),
                  onChanged: setOpacity,
                ),
              ),
              const SizedBox(height: 10),
              Wrap(
                spacing: 10,
                children: [
                  InkResponse(
                    radius: 24,
                    onTap: _decreaseRotate,
                    child: const Icon(Icons.rotate_left),
                  ),
                  InkResponse(
                    radius: 24,
                    onTap: _decreaseScale,
                    child: const Icon(Icons.keyboard_double_arrow_down),
                  ),
                  InkResponse(
                    radius: 24,
                    onTap: _toggleFlipX,
                    child: const Icon(Icons.compare_arrows),
                  ),
                  RotatedBox(
                    quarterTurns: 1,
                    child: InkResponse(
                      radius: 24,
                      onTap: _toggleFlipY,
                      child: const Icon(Icons.compare_arrows),
                    ),
                  ),
                  InkResponse(
                    radius: 24,
                    onTap: _increaseScale,
                    child: const Icon(Icons.keyboard_double_arrow_up),
                  ),
                  InkResponse(
                    radius: 24,
                    onTap: _increaseRotate,
                    child: const Icon(Icons.rotate_right),
                  ),
                ],
              ),
              const SizedBox(height: 15),
              Container(
                alignment: Alignment.center,
                child: GridView.builder(
                  shrinkWrap: true,
                  itemCount: Colors.primaries.length,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    mainAxisSpacing: 2,
                    crossAxisSpacing: 2,
                    crossAxisCount: 9,
                  ),
                  itemBuilder: (_, i) {
                    final color = Colors.primaries[i];
                    return Card(
                      color: color,
                      child: InkWell(
                        onTap: () => setColor(color),
                        child: AnimatedCheckmark(
                          weight: 4,
                          color: Colors.white70,
                          value: _color == color,
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
        Example(
          title: 'WxToggleIcon',
          child: Choice(
            multiple: true,
            clearable: true,
            builder: (choice, child) {
              return Wrap(
                spacing: 15,
                runSpacing: 15,
                children: [
                  IconButton(
                    iconSize: 32,
                    onPressed: () => choice.select('bookmark'),
                    icon: WxToggleIcon(
                      Icons.bookmark,
                      baseIcon: Icons.bookmark_border,
                      selected: choice.selected('bookmark'),
                      size: 32,
                    ),
                  ),
                  IconButton(
                    iconSize: 32,
                    onPressed: () => choice.select('favorite'),
                    icon: WxToggleIcon.scaleUp(
                      Icons.favorite,
                      baseIcon: Icons.favorite_border,
                      selected: choice.selected('favorite'),
                      size: 32,
                    ),
                  ),
                  IconButton(
                    iconSize: 32,
                    onPressed: () => choice.select('star'),
                    icon: WxToggleIcon.scaleDown(
                      Icons.star,
                      selected: choice.selected('star'),
                      size: 32,
                      rotate: 45,
                      selectedStyle: const WxToggleIconStyle(
                        rotate: 0,
                      ),
                    ),
                  ),
                  IconButton(
                    iconSize: 32,
                    onPressed: () => choice.select('tree'),
                    icon: WxToggleIcon(
                      Icons.account_tree,
                      baseIcon: Icons.account_tree_outlined,
                      selected: choice.selected('tree'),
                      size: 32,
                      color: Colors.orange,
                    ),
                  ),
                ],
              );
            },
          ),
        )
      ],
    );
  }
}

class Wrapper extends StatelessWidget {
  const Wrapper({
    super.key,
    required this.children,
  });

  final List<Widget> children;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(vertical: 20),
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(
              maxWidth: 300,
            ),
            child: Column(
              children: children,
            ),
          ),
        ),
      ),
    );
  }
}

class Example extends StatelessWidget {
  const Example({
    super.key,
    required this.title,
    required this.child,
  });

  final String title;
  final Widget child;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(0, 10, 0, 5),
          child: Center(
            child: WxText.headlineSmall(
              title,
              gradient: const LinearGradient(
                colors: [
                  Colors.green,
                  Colors.blue,
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        Card(
          child: Padding(
            padding: const EdgeInsets.all(20),
            child: SizedBox(
              width: double.infinity,
              child: Center(child: child),
            ),
          ),
        ),
      ],
    );
  }
}

更多关于Flutter微信图标集成插件wx_icon的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter微信图标集成插件wx_icon的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 项目中使用 wx_icon 插件来集成微信图标非常简单。wx_icon 插件提供了多种微信相关的图标,方便你在应用中使用。以下是详细的步骤:

1. 添加依赖

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

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

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

2. 导入包

在需要使用微信图标的 Dart 文件中,导入 wx_icon 包:

import 'package:wx_icon/wx_icon.dart';

3. 使用图标

wx_icon 提供了多种微信图标,你可以通过 WxIcon 类来使用这些图标。以下是一些示例:

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('微信图标示例'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Icon(WxIcons.wechat), // 微信图标
            SizedBox(height: 20),
            Icon(WxIcons.wechat_pay), // 微信支付图标
            SizedBox(height: 20),
            Icon(WxIcons.wechat_moments), // 微信朋友圈图标
            SizedBox(height: 20),
            Icon(WxIcons.wechat_mini_program), // 微信小程序图标
          ],
        ),
      ),
    );
  }
}

4. 自定义图标大小和颜色

你可以通过 Icon 组件的 sizecolor 属性来自定义图标的大小和颜色:

Icon(
  WxIcons.wechat,
  size: 50.0,
  color: Colors.green,
),
回到顶部