Flutter模糊玻璃效果插件blur_glass的使用

Flutter模糊玻璃效果插件blur_glass的使用

参考示例

Blur Glass - Google Chrome.png

pub

https://pub.dev/packages/blur_glass

使用

pubspec.yaml文件中添加依赖:

dependencies:
    blur_glass: ^lastest_version

使用示例:

BlurGlass(
    margin: EdgeInsets.all(5.0),
    padding: EdgeInsets.all(5.0),
    child: Row(
            children: [
                Icon(Icons.star),
                Icon(Icons.list),
                Icon(Icons.zoom_in),
            ],
        )
)

函数创建者

  BlurGlass({Key? key,
             required this.child,
             this.margin,
             this.padding,
             this.color,
             this.outBorderRadius = 20.0, // 外部圆角半径
             this.inBorderRadius = 30.0,  // 内部圆角半径
             this.filterX = 10.0,         // 模糊程度 X 轴
             this.filterY = 10.0          // 模糊程度 Y 轴
             }) : super(key: key);

完整示例代码

import 'package:flutter/material.dart';
import 'package:blur_glass/blur_glass.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: 'Blur Glass',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key,}) : super(key: key);
  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  IconButton _IconButton(String iconPath, String linkPath) {
    return IconButton(
      icon: ImageIcon(AssetImage(iconPath)),
      color: Colors.white,
      splashColor: Colors.transparent,
      highlightColor: Colors.transparent,
      onPressed: () {
        //launchUrl(Uri.parse(linkPath));
      },
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF2E2E48),
      body: Container(
          margin: const EdgeInsets.all(10.0), //.only(top: 10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            image: const DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage('assets/background.jpg'),
            ),
          ),
          child: Flex(
            direction: Axis.vertical,
            children: [
              Expanded(
                flex: 9,
                child: Center(
                  child: BlurGlass(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          '   WELCOME\nTO  MY  BLOG!',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 70,
                              fontWeight: FontWeight.w900,
                              shadows: [
                                BoxShadow(
                                    blurRadius: 5,
                                    color: Colors.white.withOpacity(0.54)),
                              ]),
                        ),
                        const SizedBox(height: 20),
                        const Text(
                          ' ShadowPlusing ',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 23,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    BlurGlass(
                        margin: EdgeInsets.all(5.0),
                        padding: EdgeInsets.all(5.0),
                        child: Row(
                          children: [
                            _IconButton('assets/github.png',
                                'https://github.com/shAdow-XJY'),
                            _IconButton('assets/gitee.png',
                                'https://gitee.com/shAdowPlusing'),
                            _IconButton('assets/bilibili.png',
                                'https://space.bilibili.com/437699902'),
                          ],
                        )
                    )
                  ],
                ),
              )
            ],
          )),
    );
  }
}

更多关于Flutter模糊玻璃效果插件blur_glass的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter模糊玻璃效果插件blur_glass的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中实现模糊玻璃效果,可以使用 blur_glass 插件。这个插件允许你在UI中轻松添加模糊效果,类似于iOS的毛玻璃效果。以下是如何使用 blur_glass 插件的步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  blur_glass: ^1.0.0  # 请查看最新版本

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

2. 使用 BlurGlass 组件

在你的Flutter应用程序中,你可以使用 BlurGlass 组件来创建模糊玻璃效果。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          children: [
            // 背景图片
            Image.asset(
              'assets/background.jpg', // 替换为你的图片路径
              fit: BoxFit.cover,
              width: double.infinity,
              height: double.infinity,
            ),
            // 模糊玻璃效果
            Center(
              child: BlurGlass(
                radius: 20, // 模糊半径
                child: Container(
                  width: 200,
                  height: 200,
                  padding: EdgeInsets.all(20),
                  child: Center(
                    child: Text(
                      'Blur Glass Effect',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部