Flutter界面美化插件glassy的使用

Flutter界面美化插件glassy的使用

glassy 是一个用于所有平台的 Flutter 包,它提供了玻璃效果。

截图

使用方法

要使用此包,首先需要将其添加到 pubspec.yaml 文件中:

dependencies:
  flutter:
    sdk: flutter
  glassy: latest_version

如何使用

以下是完整的示例代码,展示了如何在 Flutter 应用程序中使用 glassy 插件:

import 'package:flutter/material.dart';
import 'package:glassy/glassy.dart';
import 'package:glassy/glassy_button.dart';
import 'package:glassy/glassy_card.dart';
import 'package:glassy/glassy_config.dart';
import 'package:glassy/glassy_dialog.dart';

void main() {
  // 这个配置是可选的
  Glassy().setConfig(GlassyConfig(
      radius: 15,
      backgroundColor: Colors.grey,
      backgroundOpacity: 0.2,
      borderOpacity: 0.5));
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Glassy Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const DemoPage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          // 背景图片
          Image.asset("assets/images/test.jpg", fit: BoxFit.cover),
          // 玻璃效果卡片和按钮
          SizedBox(
            child: Column(children: [
              // 第一个玻璃效果卡片
              GlassyCard(
                  child: Row(
                children: [
                  IconButton(
                      onPressed: () {},
                      icon: const Icon(
                        Icons.search,
                        color: Colors.white,
                      )),
                  const Expanded(
                      child: Text(
                    "This is a GlassyCard",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                    ),
                  ))
                ],
              )),
              // 第二个玻璃效果卡片
              const SizedBox(
                width: 300,
                height: 400,
                child: GlassyCard(
                    child: Center(
                  child: Text("This is another Glassy Card",
                      style: TextStyle(color: Colors.white)),
                )),
              ),
              // 玻璃效果按钮
              Expanded(
                  child: Center(
                child: GlassyButton(
                    child: const Text("This is Glassy Button"),
                    onPressed: () {
                      showDialog(
                          context: context,
                          builder: ((context) {
                            return const GlassyDialog(
                                title: "This is Glassy Dialog Title",
                                child: Center(
                                  child: Text(
                                    "This is Glassy Dialog Content Text Widget",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ));
                          }));
                    }),
              ))
            ]),
          )
        ],
      ),
    );
  }
}

玻璃效果组件列表

  • GlassyCard
  • GlassyButton
  • GlassyDialog

许可证

版权所有 © 2020 Sayed Mohammad Amin Emrani

许可协议如下:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

更多关于Flutter界面美化插件glassy的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter界面美化插件glassy的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 中,glassy 是一个用于美化界面的插件,它可以帮助你轻松实现玻璃磨砂效果(Glassmorphism)。这种效果在现代 UI 设计中非常流行,尤其是在 macOS Big Sur 和 iOS 15 中广泛使用。

以下是使用 glassy 插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  glassy: ^1.0.0 # 请检查最新版本

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

2. 使用 Glassy 组件

glassy 插件提供了一个 GlassyContainer 组件,你可以用它来创建一个具有玻璃磨砂效果的容器。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: GlassyContainer(
            width: 300,
            height: 200,
            borderRadius: BorderRadius.circular(20),
            child: Center(
              child: Text(
                'Hello, Glassy!',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

3. 自定义 GlassyContainer

你可以通过以下属性来自定义 GlassyContainer

  • widthheight: 容器的宽度和高度。
  • borderRadius: 容器的圆角半径。
  • blur: 模糊程度,默认值为 10.0
  • color: 背景颜色,默认值为 Colors.white.withOpacity(0.1)
  • border: 边框,默认值为 Border.all(color: Colors.white.withOpacity(0.2), width: 1.0)
  • child: 容器内部的子组件。
GlassyContainer(
  width: 300,
  height: 200,
  borderRadius: BorderRadius.circular(20),
  blur: 15.0,
  color: Colors.blue.withOpacity(0.2),
  border: Border.all(color: Colors.blue.withOpacity(0.3), width: 2.0),
  child: Center(
    child: Text(
      'Custom Glassy!',
      style: TextStyle(
        color: Colors.white,
        fontSize: 24,
      ),
    ),
  ),
);

4. 背景图片

为了增强玻璃磨砂效果,你可以在 GlassyContainer 后面添加一张背景图片。这样,玻璃效果会更加明显。

Scaffold(
  body: Container(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage('assets/background.jpg'),
        fit: BoxFit.cover,
      ),
    ),
    child: Center(
      child: GlassyContainer(
        width: 300,
        height: 200,
        borderRadius: BorderRadius.circular(20),
        child: Center(
          child: Text(
            'Glassy with Background!',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24,
            ),
          ),
        ),
      ),
    ),
  ),
);
回到顶部