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 {
  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: [
            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杂记)