当前位置:   article > 正文

Flutter进阶实战 7-20 毛玻璃效果制作_flutter 毛玻璃效果

flutter 毛玻璃效果

前言:Flutter的Fliter Widget 也是非常强大的,它可以制作出你想要的神奇滤镜效果。这里我们就以实战的方式,制作一个毛玻璃效果,通过实例来学习Fitler 的用法。

效果如下:这位老师你认识吗?

实现过程:

一、main.dart文件编写

  1. import 'package:flutter/material.dart';
  2. import 'Frosted_class.dart';
  3. void main() => runApp(new MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return new MaterialApp(
  8. title: '毛玻璃效果',
  9. theme: new ThemeData(
  10. primarySwatch: Colors.blue,
  11. ),
  12. home: new FrostedClassDemo(),
  13. );
  14. }
  15. }

二、BackdropFilter Widget 

 BackdropFilter就是背景滤镜组件,使用它可以给父元素增加滤镜效果,它里边最重要的一个属性是filter。 filter属性中要添加一个滤镜组件,实例中我们添加了图片滤镜组件,并给了模糊效果。

新建一个Frosted_class.dart的文件,然后写入下面的代码,具体的解释已经写到了代码的注释中。

  1. import 'package:flutter/material.dart';
  2. import 'dart:ui'; //图片过滤器ImageFilter需要引入的包
  3. class FrostedClassDemo extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. body: Stack( //使用层叠组件,图片和毛玻璃、字体重叠在一起
  8. children: <Widget>[ //子元素数组
  9. //第一层在约束盒子里放一张网络照片
  10. ConstrainedBox( //约束盒子组件,添加额外的约束条件在child上
  11. //约束条件
  12. constraints: const BoxConstraints.expand(),//随着约束条件进行扩展expand
  13. child: Image.network('http://img.wpxap.com/data/attachment/forum/201203/11/0021593jdvh2zpdd3s3dlv.jpg'),
  14. ),
  15. //第二层添加毛玻璃
  16. Center(
  17. child: ClipRect( //可裁切的矩形
  18. child: BackdropFilter( //背景过滤器
  19. //引入图片过虑器(blur:模糊设置)
  20. filter: ImageFilter.blur(sigmaX: 5.0,sigmaY: 5.0),
  21. child: Opacity( //设置透明度
  22. opacity: 0.5,//半透明
  23. child: Container(
  24. width: 500.0,
  25. height: 700.0,
  26. decoration: BoxDecoration(color: Colors.grey.shade200),//盒子修饰器
  27. child: Center(
  28. child: Text(
  29. '毛玻璃效果',
  30. style: Theme.of(context).textTheme.display3,
  31. ),
  32. ),
  33. ),
  34. ),
  35. ),
  36. ),
  37. )
  38. ],
  39. ),
  40. );
  41. }
  42. }

 

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

闽ICP备14008679号