英文:
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(),
),
)
],
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论