flutter - 如何模糊一列中的多个图像?

标签 flutter dart

我已经尝试过https://medium.com/fluttervn/how-to-make-blur-effect-in-flutter-using-backdropfilter-imagefilter-559ffd8ab73的代码。但是它没有我想要的效果。

children: <Widget>[
Card(
    child: Stack(
      children: <Widget>[
        Stack(
          children: <Widget>[
            Center(
              child: Image.asset(
                'assets/image1.jpg',
                fit: BoxFit.fill,
              ),
            ),
            Container(
              width: double.infinity,
              height: 250,
              child: BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 1.0, sigmaY: 1.0),
                child: Container(
                  color: Colors.black.withOpacity(0.1),
                ),
              ),
            )
          ],
        ),
        Center(
          child: Text('Image 1'),
        )
      ],
    ),
  ),
  Card(...),
  Card(...),
],
我曾尝试将每张卡片都包装到一个容器中,但是以前的卡片甚至会变得越来越模糊,甚至是文字。我只希望在文本保持清晰的同时模糊每张卡的图像。

最佳答案

我还没有测试过,但是尝试使用Container小部件代替Image.asset。例如:

Container(
  width:x,
  height:y,
  decoration: BoxDecoration(
     image: DecorationImage(
       image: AssetImage('assets/image1.jpg'),
       fit: BoxFit.cover,
  ),
  child: BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 1.0, sigmaY: 1.0),
                child: Container(
                    color: Colors.black.withOpacity(0.1),
  ),
),
由于每张卡都是相同的,因此您可以创建一个类作为构造函数,并且当您需要一张卡时,您只会添加CardTemplate('text 1', 'assets/image1.jpg'),:
class CardTemplate extends StatelessWidget{



 String image;
 String text;
   
 CardTemplate(this.text,this.image);
 
@override
Widget build(BuildContext context) {

return Card(
  child:Stack(
   children: <Widget>[
    Stack(
      children: <Widget>[
        Center(
          child: Container(
           width: double.infinity,
          height: 250,
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(this.image),
                  fit: BoxFit.cover,
              ),
            ),
            child: BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 1.0, sigmaY: 1.0),
                child: Container(
                    color: Colors.black.withOpacity(0.1),
              ),
            ),
          )
        ),
      
    Center(
      child: Text(this.text),
     )
    ],
   ),
  );
 }
}
请注意,我尚未测试代码,因此可能会有一些语法或拼写错误

关于flutter - 如何模糊一列中的多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63007098/

相关文章:

date - 使用 Dart 将年数转换为日期

dart - 从磁盘读取的文本不符合换行符

dart - 为什么String.endsWith取String时String.startsWith取Pattern作为输入

Flutter/Dart DateFormat 12 :27 to 00:27 bug, 小时 0 错误

flutter - 从Firestore获取某些文档数据以填充下拉列表

firebase - 如何使用 flutter 和 firebase 进行离线身份验证

flutter - 从服务器 : Bad Gateway with exo player 收到状态代码 502

firebase - 未处理的异常 : Invalid argument(s) (onError): The error handler of Future. catchError 必须返回 future 类型的值

dart - Flutter 中的装饰图像模糊

flutter - 没有带有AnimatedSwitcher或AnimatedOpacity的动画