Flutter: 如果用作背景,ImageFilter.blur 和 BackdropFilter 是否会很昂贵?

huangapple go评论67阅读模式
英文:

Flutter: Is ImageFilter.blur and BackdropFilter expensive if used as a background

问题

我在制作一个具有玻璃效果的Flutter应用程序,所以我在每个屏幕上使用了ImageFilter.blur和BackdropFilter。

这对性能来说是否太昂贵了?

以下是代码:

 body: SizedBox(
          width: screenWidth,
          height: screenHeight,
          child: Stack(
            children: [
              Positioned(
                top: screenHeight * 0.1,
                left: -128,
                child: Container(
                  height: 186,
                  width: 186,
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color.fromARGB(255, 15, 196, 206),
                  ),
                  child: BackdropFilter(
                    filter: ImageFilter.blur(
                      sigmaX: 200,
                      sigmaY: 200,
                    ),
                    child: Container(
                      height: 166,
                      width: 166,
                      color: const Color.fromARGB(0, 145, 62, 62),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: screenHeight * 0.1,
                right: -128,
                child: Container(
                  height: 186,
                  width: 186,
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color fromARGB(255, 15, 196, 206),
                  ),
                  child: BackdropFilter(
                    filter: ImageFilter.blur(
                      sigmaX: 200,
                      sigmaY: 200,
                    ),
                    child: Container(
                      height: 166,
                      width: 166,
                      color: const Color.fromARGB(0, 145, 62, 62),
                    ),
                  ),
                ),
              ),
              Positioned(
                child: Container(
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                        opacity: 0.1,
                        image: AssetImage('assets/images/logo.png')),
                  ),
                ),
              )
            ],
          ),
        ),

这是每个屏幕的背景。

如果这不是一个好的方法,那么有更好的方法是什么?

使用MediaQuery来获取高度和宽度会不会也降低性能?

非常感谢。

我尝试了上面的代码,发现在Flutter发布版中应用程序有点慢,这是原因吗,还是我应该在其他地方查找问题?

英文:

I am making a flutter app with glass effect so I use *** ImageFilter.blur and BackdropFilter *** in every screen.

Is this so expensive for the performance?

here is the code

 body: SizedBox(
          width: screenWidth,
          height: screenHeight,
          child: Stack(
            children: [
              Positioned(
                top: screenHeight * 0.1,
                left: -128,
                child: Container(
                  height: 186,
                  width: 186,
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color.fromARGB(255, 15, 196, 206),
                  ),
                  child: BackdropFilter(
                    filter: ImageFilter.blur(
                      sigmaX: 200,
                      sigmaY: 200,
                    ),
                    child: Container(
                      height: 166,
                      width: 166,
                      color: const Color.fromARGB(0, 145, 62, 62),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: screenHeight * 0.1,
                right: -128,
                child: Container(
                  height: 186,
                  width: 186,
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color.fromARGB(255, 15, 196, 206),
                  ),
                  child: BackdropFilter(
                    filter: ImageFilter.blur(
                      sigmaX: 200,
                      sigmaY: 200,
                    ),
                    child: Container(
                      height: 166,
                      width: 166,
                      color: const Color.fromARGB(0, 145, 62, 62),
                    ),
                  ),
                ),
              ),
              Positioned(
                child: Container(
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                        opacity: 0.1,
                        image: AssetImage('assets/images/logo.png')),
                  ),
                ),
              )

this is the background of every screen.

If this is not a good way to do it what is ?

and is using MediaQuery for height and width a lot also slows down performance?

thanks a lot

I tried the code above and the app was a bit slow for flutter release, was that the reason or should I look somewhere else.

答案1

得分: 0

这是您提供的Dart代码的中文翻译:

import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          useMaterial3: true,
        ),
        home: const MyHomePage());
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final screenWidth = mediaQuery.size.width;
    final screenHeight = mediaQuery.size.height;

    return Scaffold(
      body: SizedBox(
        width: screenWidth,
        height: screenHeight,
        child: Stack(
          children: [
            Positioned(
              top: screenHeight * 0.1,
              child: ImageFiltered(
                imageFilter: ImageFilter.blur(
                    sigmaX: 150, sigmaY: 150, tileMode: TileMode.decal),
                child: Transform.translate(
                  offset: const Offset(-128, 0),
                  child: const DecoratedBox(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color.fromARGB(255, 15, 196, 206),
                    ),
                    child: SizedBox(
                      height: 186,
                      width: 186,
                    ),
                  ),
                ),
              ),
            ),
            Positioned(
              bottom: screenHeight * 0.1,
              child: ImageFiltered(
                imageFilter: ImageFilter.blur(
                    sigmaX: 150, sigmaY: 150, tileMode: TileMode.decal),
                child: Transform.translate(
                  offset: Offset(screenWidth + 128 - 186, 0),
                  child: const DecoratedBox(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color.fromARGB(255, 15, 196, 206),
                    ),
                    child: SizedBox(
                      height: 186,
                      width: 186,
                    ),
                  ),
                ),
              ),
            ),
            const Positioned(
              child: DecoratedBox(
                decoration: BoxDecoration(
                  image: DecorationImage(
                      opacity: 0.1,
                      image: AssetImage('assets/images/logo.png')),
                ),
                child: SizedBox.expand(),
              ),
            )
          ],
        ),
      ),
    );
  }
}

如果您有任何其他需要,请随时告诉我。

英文:

Can you try this and see if this is something you want to achieve

import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          useMaterial3: true,
        ),
        home: const MyHomePage());
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final screenWidth = mediaQuery.size.width;
    final screenHeight = mediaQuery.size.height;

    return Scaffold(
      body: SizedBox(
        width: screenWidth,
        height: screenHeight,
        child: Stack(
          children: [
            Positioned(
              top: screenHeight * 0.1,
              child: ImageFiltered(
                imageFilter: ImageFilter.blur(
                    sigmaX: 150, sigmaY: 150, tileMode: TileMode.decal),
                child: Transform.translate(
                  offset: const Offset(-128, 0),
                  child: const DecoratedBox(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color.fromARGB(255, 15, 196, 206),
                    ),
                    child: SizedBox(
                      height: 186,
                      width: 186,
                    ),
                  ),
                ),
              ),
            ),
            Positioned(
              bottom: screenHeight * 0.1,
              child: ImageFiltered(
                imageFilter: ImageFilter.blur(
                    sigmaX: 150, sigmaY: 150, tileMode: TileMode.decal),
                child: Transform.translate(
                  offset: Offset(screenWidth + 128 - 186, 0),
                  child: const DecoratedBox(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color.fromARGB(255, 15, 196, 206),
                    ),
                    child: SizedBox(
                      height: 186,
                      width: 186,
                    ),
                  ),
                ),
              ),
            ),
            const Positioned(
              child: DecoratedBox(
                decoration: BoxDecoration(
                  image: DecorationImage(
                      opacity: 0.1,
                      image: AssetImage('assets/images/logo.png')),
                ),
                child: SizedBox.expand(),
              ),
            )
          ],
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年2月19日 23:41:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定