Flutter图标阴影效果插件flutter_icon_shadow的使用

Flutter图标阴影效果插件flutter_icon_shadow的使用

介绍

Flutter Icon Shadow 插件允许你在图标下方添加阴影。你可以自定义阴影的颜色、模糊程度(sigma)和偏移量。这个插件是从 icon_shadow 包中分叉出来的,增加了空安全和更多功能。

sample

如何使用

1. 添加依赖到 pubspec.yaml 文件

在你的 pubspec.yaml 文件中添加 flutter_icon_shadow 依赖:

flutter pub add flutter_icon_shadow
2. 添加图标

只需将你的图标包裹在 IconShadow 小部件中,它接受一个 Icon 小部件作为第一个参数。

更多的参数也可以使用:Color? shadowColordouble shadowBlurSigma = 0.9Offset shadowOffset = Offset.zero

以下是一个完整的示例 demo:

// ignore_for_file: depend_on_referenced_packages, library_private_types_in_public_api

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

void main() => runApp(const MyApp());

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

  // 这个小部件是应用程序的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.deepPurple,
      ),
      home: const MyHomePage(),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: const Text('Flutter Icon Shadow'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                // 默认阴影
                const IconShadow(
                  Icon(
                    Icons.lightbulb_outline,
                    color: Colors.lightBlueAccent,
                    size: 36,
                  ),
                ),
                // 自定义颜色和偏移量
                IconShadow(
                  const Icon(
                    Icons.lightbulb_outline,
                    color: Colors.lightBlueAccent,
                    size: 36,
                  ),
                  shadowColor: Colors.lightBlueAccent[100],
                  shadowOffset: const Offset(2, 2), // 可选,可能会因边界框而被裁剪
                ),
                // 不显示阴影
                const IconShadow(
                  Icon(
                    Icons.lightbulb_outline,
                    color: Colors.lightBlueAccent,
                    size: 36,
                  ),
                  showShadow: false,
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                // 自定义颜色
                IconShadow(
                  Icon(
                    Icons.home,
                    color: Colors.green[900],
                    size: 36,
                  ),
                  shadowColor: Colors.yellowAccent,
                ),
                // 自定义偏移量
                IconShadow(
                  Icon(
                    Icons.home,
                    color: Colors.green[900],
                    size: 36,
                  ),
                  shadowOffset: const Offset(2, 2),
                ),
                // 不显示阴影
                IconShadow(
                  Icon(
                    Icons.home,
                    color: Colors.green[900],
                    size: 36,
                  ),
                  showShadow: false,
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                // 自定义颜色
                IconShadow(
                  const Icon(
                    Icons.access_alarm,
                    color: Colors.black54,
                    size: 36,
                  ),
                  shadowColor: Colors.yellowAccent[400],
                ),
                // 自定义颜色和偏移量
                IconShadow(
                  const Icon(
                    Icons.access_alarm,
                    color: Colors.black54,
                    size: 36,
                  ),
                  shadowColor: Colors.red[400],
                  shadowOffset: const Offset(2, 2),
                ),
                // 自定义颜色
                IconShadow(
                  const Icon(
                    Icons.access_alarm,
                    color: Colors.black54,
                    size: 36,
                  ),
                  shadowColor: Colors.cyanAccent[400],
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter图标阴影效果插件flutter_icon_shadow的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图标阴影效果插件flutter_icon_shadow的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用 flutter_icon_shadow 插件在 Flutter 中为图标添加阴影效果的代码示例。

首先,确保你已经在 pubspec.yaml 文件中添加了 flutter_icon_shadow 依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_icon_shadow: ^x.y.z  # 请将 x.y.z 替换为最新的版本号

然后运行 flutter pub get 来获取依赖。

接下来,在你的 Dart 文件中,你可以按照以下步骤使用 flutter_icon_shadow 插件:

import 'package:flutter/material.dart';
import 'package:flutter_icon_shadow/flutter_icon_shadow.dart';
import 'package:flutter_icons/flutter_icons.dart';  // 假设你使用 Flutter Icons 库

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

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Icon Shadow Demo'),
      ),
      body: Center(
        child: IconShadow(
          icon: Icon(FlutterIcons.material_home, size: 48),  // 使用 Flutter Icons 库中的图标
          shadowColor: Colors.black.withOpacity(0.5),
          shadowBlur: 10.0,
          shadowOffsetX: 3.0,
          shadowOffsetY: 3.0,
        ),
      ),
    );
  }
}

在这个示例中,我们使用了 flutter_icon_shadow 包中的 IconShadow 小部件。以下是对 IconShadow 构造函数参数的解释:

  • icon: 要显示并添加阴影效果的图标。这里我们使用了 FlutterIcons 库中的图标,你需要确保已经添加了相应的图标库依赖。
  • shadowColor: 阴影的颜色。
  • shadowBlur: 阴影的模糊半径。
  • shadowOffsetX: 阴影在水平方向上的偏移量。
  • shadowOffsetY: 阴影在垂直方向上的偏移量。

请注意,如果你不使用 FlutterIcons 库,你也可以直接使用 Flutter 自带的图标,例如:

Icon(Icons.home, size: 48),

这个代码示例展示了如何使用 flutter_icon_shadow 插件在 Flutter 应用中为图标添加阴影效果。你可以根据需要调整阴影的颜色、模糊半径和偏移量,以达到你期望的视觉效果。

回到顶部