Flutter 实现背景高斯模糊并拦截手势

本文介绍了如何在Flutter中使用BackdropFilter Widget实现背景高斯模糊效果,并通过在外部添加透明Container来拦截手势,防止穿透到底层组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flutter 实现背景高斯模糊并拦截手势

在flutter中可以通过一个Widget直接实现背景模糊效果:BackdropFilter

源码介绍如下(例子被我省略了,大家可以直接查看源码的时候去看一下):

/// A widget that applies a filter to the existing painted content and then
/// paints [child].
///
/// The filter will be applied to all the area within its parent or ancestor
/// widget's clip. If there's no clip, the filter will be applied to the full
/// screen.

这边是我自己写的一个简单例子供大家参考:

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

class BackdropFilterPage extends StatefulWidget {
  @override
  _BackdropFilterPageState createState() {
    // TODO: implement createState
    return _BackdropFilterPageState();
  }
}

class _BackdropFilterPageState extends State<BackdropFilterPage> {
  bool isShow = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(
### 如何在 Flutter实现背景高斯模糊效果 为了实现Flutter中的背景高斯模糊效果,可以利用`BackdropFilter`组件来应用图像过滤器到其子部件上。此方法能够创建一种视觉上的模糊效果,适用于希望给定区域内的内容看起来像是被模糊处理过的情况[^1]。 下面是一个简单的例子展示如何使用 `BackdropFilter` 来达到背景高斯模糊的效果: ```dart import 'package:flutter/material.dart'; import 'dart:ui' as ui; class BlurryBackground extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Blurry Background Example')), body: Center( child: ClipRect( // Clips its child to a rectangular area. child: BackdropFilter( filter: ui.ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), // Applies the blur effect. child: Container( width: 200.0, height: 200.0, decoration: BoxDecoration(color: Colors.grey.withOpacity(0.5)), child: Center(child: Text('Blurred Content', style: TextStyle(fontSize: 24))), ), ), ), ), ); } } ``` 这段代码展示了怎样通过设置不同的sigma值(即标准差)控制水平方向(`sigmaX`)和垂直方向(`sigmaY`)上的模糊强度。这里还设置了容器的颜色透明度以增强模糊感[^3]。 如果想要让这种模糊效果随着用户的交互行为而改变,比如当页面滚动时逐渐增加或减少模糊量,则可引入`AnimationController`配合动画曲线调整`BackdropFilter`内使用的`ImageFilter.blur()`参数,从而获得更加生动有趣的用户体验[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值