ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Flutter之毛玻璃效果的实现

2021-04-13 12:02:19  阅读:260  来源: 互联网

标签:效果 Theme display3 context child 组件 Flutter 毛玻璃


用Flutter实现毛玻璃效果的代码如下:

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

class FrostedGlassDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          ConstrainedBox(
            constraints: BoxConstraints.expand(),
            child: Image.network('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhuafans.dbankcloud.com%2Fpic%2F2017%2F01%2F25%2F195116eebdd5aa2fdaebff832280a391_IMG_20170125_153017.jpg%3Fmode%3Ddownload&refer=http%3A%2F%2Fhuafans.dbankcloud.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620875132&t=ea50802522897aad43419379eea3267a'),
          ),
          Center(
            child: ClipRect(
              child: BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 3.0,sigmaY: 3.0),
                child: Opacity(
                  opacity: 0.5,
                  child: Container(
                    width: 500.0,
                    height: 800.0,
                    decoration: BoxDecoration(
                      color: Colors.grey.shade200
                    ),
                    child: Center(
                      child:Text(
                        'Cat',
                        style: Theme.of(context).textTheme.display3
                      )
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

其中,要点为:

1.用Stack组件包含图片和毛玻璃两个组件。

2.图片组件外层加了约束盒子组件,从而增加约束,实现全屏扩展图片。

3.使用剪切矩形组件包含毛玻璃组件,并居中。

4.使用背景过滤器和透明组件实现毛玻璃的具体效果。

5.在容器组件中设置毛玻璃的宽高,并使用盒子装饰器组件修饰容器,设置颜色,也可以不使用盒子装饰器直接设置颜色。

如:

child: Container(
  width: 500.0,
  height: 800.0,
  color: Colors.grey.shade200,
  child: Center(
    child:Text(
      'Cat',
      style: Theme.of(context).textTheme.display3
    )
  ),
),

6.文字组件应居中,否则会在页面的左上角。

7.使用

Theme.of(context).textTheme.display3

设置字体的样式,也可以使用TextStyle组件进行设置。

 

毛玻璃效果如下:

 

标签:效果,Theme,display3,context,child,组件,Flutter,毛玻璃
来源: https://www.cnblogs.com/luoyihao/p/14652480.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有