当前位置:   article > 正文

Flutter之毛玻璃效果的实现

Flutter之毛玻璃效果的实现

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

  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. class FrostedGlassDemo extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. body: Stack(
  8. children: [
  9. ConstrainedBox(
  10. constraints: BoxConstraints.expand(),
  11. 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'),
  12. ),
  13. Center(
  14. child: ClipRect(
  15. child: BackdropFilter(
  16. filter: ImageFilter.blur(sigmaX: 3.0,sigmaY: 3.0),
  17. child: Opacity(
  18. opacity: 0.5,
  19. child: Container(
  20. width: 500.0,
  21. height: 800.0,
  22. decoration: BoxDecoration(
  23. color: Colors.grey.shade200
  24. ),
  25. child: Center(
  26. child:Text(
  27. 'Cat',
  28. style: Theme.of(context).textTheme.display3
  29. )
  30. ),
  31. ),
  32. ),
  33. ),
  34. ),
  35. )
  36. ],
  37. ),
  38. );
  39. }
  40. }

其中,要点为:

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

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

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

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

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

如:

  1. child: Container(
  2. width: 500.0,
  3. height: 800.0,
  4. color: Colors.grey.shade200,
  5. child: Center(
  6. child:Text(
  7. 'Cat',
  8. style: Theme.of(context).textTheme.display3
  9. )
  10. ),
  11. ),

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

7.使用

Theme.of(context).textTheme.display3

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

毛玻璃效果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/935242
推荐阅读
相关标签
  

闽ICP备14008679号