我是靠谱客的博主 感性牛排,最近开发中收集的这篇文章主要介绍Flutter 实现背景高斯模糊并拦截手势,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        appBar: AppBar(
          title: Text("BackdropFilter"),
        ),
        body: Stack(
          children: <Widget>[
            listData(),
            isShow
                ? stackWidget()
                : Container(
                    width: 0,
                    height: 0,
                  ),
          ],
        ));
  }

Widget stackWidget() {
  var size = MediaQuery.of(context).size;
  return Container(
    width: size.width,
    height: size.height,
    alignment: Alignment.center,
    color: Colors.transparent,
    child: BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
      child: InkWell(
        onTap: () {
          setState(() {
            isShow = false;
          });
        },
        child: Text(
            "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"),
      ),
    ),
  );
}

  Widget listData() {
    return ListView.builder(
        itemCount: 30,
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            child: Text(
              "position=$index",
              style: TextStyle(fontSize: 18),
            ),
            onTap: () {
              print("item click");
              setState(() {
                isShow = true;
              });
            },
          );
        });
  }
}
  1. Stack应该作为最外层的Widget,这样才能正常使底层进行模糊化,例子中我是通过一个isShow的来回赋值来进行模糊的显示效果。
  2. 在实际开发中遇到一个问题,BackdropFilter控件如果作为外层,在空白位置它不会拦截手势,点击滑动都会透过BackdropFilter,所以在代码中我在BackdropFilter外围添加了一个Container,并且设置宽高为屏幕宽高,颜色设置为透明,这样就将手势进行了拦截,阻止一些奇奇怪怪的事情发生。(大家可以把我界面中的外围Container去掉,然后操作一下底层的listview,就明白了)
  3. BackdropFilter中的filter: ImageFilter需要import ‘dart:ui’;

最后

以上就是感性牛排为你收集整理的Flutter 实现背景高斯模糊并拦截手势的全部内容,希望文章能够帮你解决Flutter 实现背景高斯模糊并拦截手势所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部