dart 如何在Flutter中模糊小部件

zvms9eto  于 2023-06-03  发布在  Flutter
关注(0)|答案(7)|浏览(589)

我需要模糊一个小部件,例如,一个简单的容器。我怎么模糊它?

new Container(
  child: new Text('hello I am here'),
  height: 100.0,
  width: 100.0,
  color: Colors.red,
)

考虑上面的容器。

dgsult0t

dgsult0t1#

下面是示例代码:

import 'dart:ui' as ui;

 Widget backdropFilterExample(BuildContext context, Widget child) {
    return Stack(
      fit: StackFit.expand,
      children: <Widget>[
        child,
        BackdropFilter(
          filter: ui.ImageFilter.blur(
            sigmaX: 8.0,
            sigmaY: 8.0,
          ),
          child: Container(
            color: Colors.transparent,
          ),
        )
      ],
    );
  }

Google还提供了以下示例代码:
https://api.flutter.dev/flutter/widgets/BackdropFilter-class.html

rt4zxlrg

rt4zxlrg2#

您可以创建一个**Stack**,如下所示:
第一个孩子:要模糊的内容(在您的情况下:容器)
第二个孩子:BackdropFilter
在第二个子对象(BackdropFilter)中,有一个参数child,用于将小部件插入树中,该小部件不受BackdropFilter的影响。
在BackdropFilter中,设置过滤器:ImageFilter.blur(5.0,5.0)
5.0是你想要模糊的数量。

tvokkenx

tvokkenx3#

我使用BackdropFilterPosition.fill示例:

import 'dart:ui' as ui;

 Widget buildBlur({@required BuildContext context, @required Widget child}) {
    return Stack(
      children: <Widget>[
        child, // Your child
        Positioned.fill(
            child: BackdropFilter(
              filter: ui.ImageFilter.blur(
                sigmaX: 1.0,
                sigmaY: 1.0,
              ),
              child: Center(
               child: XedProgress.indicator(), // replace your loading widget
             ),
           ),
         )
      ],
    );
  }
e4yzc0pl

e4yzc0pl4#

请注意,如果您想模糊某些部件而不是下面的所有部件,请查看ImageFilter class而不是BackdropFilter。
比如说

Widget _emptyCard() {
return Stack(
  alignment: Alignment.bottomCenter,
  children: [
    ImageFiltered(
      imageFilter: ImageFilter.blur(sigmaX: 3.0, sigmaY: 3.0),
      child: _fakeBlurredStats(),
    ),
    _noStatsMessage(),
  ],
);

}

yftpprvb

yftpprvb5#

模糊滤镜构件

import 'dart:ui';

import 'package:flutter/material.dart';

class BlurFilter extends StatelessWidget {
  final Widget child;
  final double sigmaX;
  final double sigmaY;
  BlurFilter({this.child, this.sigmaX = 5.0, this.sigmaY = 5.0});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        child,
        ClipRect(
          child: BackdropFilter(
            filter: ImageFilter.blur(
              sigmaX: sigmaX,
              sigmaY: sigmaY,
            ),
            child: Opacity(
              opacity: 0.01,
              child: child,
            ),
          ),
        ),
      ],
    );
  }
}

用法

import 'package:flutter/material.dart';
import 'package:my_app/widgets/blur_filter.dart';

class WidgetToImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('blur filter'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Text('123'),
            Text('999'),
            BlurFilter(
              child: Text('1000'),
            ),
            Text('888'),
          ],
        ),
      ),
    );
  }
}
nsc4cvqm

nsc4cvqm6#

我们需要3件事,使模糊小部件

  • 1.Stack
  • 2.BackdropFilter
  • 3.ImageFilter.blur
Stack(
     children: <Widget>[
       Container(child: Text( "Hello")),              // First child
       BlurryEffect(0.5,0.1,Colors.grey.shade200)    //  Second Child
     ],
)

并且,

class BlurryEffect extends StatelessWidget {
  final double opacity;
  final double blurry;
  final Color shade;

  BlurryEffect(this.opacity,this.blurry,this.shade);

    @override  Widget build(BuildContext context) {
    return Container(
      child: ClipRect(
        child:  BackdropFilter(
          filter:  ImageFilter.blur(sigmaX:blurry, sigmaY:blurry),
            child:  Container(
              width: double.infinity,
              height:  double.infinity,
              decoration:  BoxDecoration(color: shade.withOpacity(opacity)),
             ),
           ),
          ),
        );
  }
}
2vuwiymt

2vuwiymt7#

我使用onWindowFocusChanged来检测MainActivity上的onPause

@Override
public void onWindowFocusChanged (boolean hasFocus){
    if (!hasFocus){
        channel.invokeMethod("on_pause", "");
    }

    super.onWindowFocusChanged(hasFocus);
}

相关问题