我正在尝试构建具有给定宽度和高度的图像网格,将它们包裹在内部Containers
并使用fit: BoxFit.fill
以在选择图像时设置边框(我不在乎保持图像纵横比,我想每个容器的总宽度和高度相同)。
问题是,当图像在被点击后被重新绘制时,我注意到一个白色的闪光。当图像很少时,这似乎不会发生,但有 15 幅以上的图像会很嘈杂。
我尝试添加gaplessPlayback: true
图像小部件,正如我在这里找到的那样,但这并没有解决我的问题。
这是一个显示我的问题的 gif(我使用了 16 张图片,尺寸为 1920x1080):
编辑:
我忘了指出这只是一个示例,我在代码中使用了边框,但在我的情况下,我还想在容器中添加填充以使图像更小(如在 android 照片库中),这意味着每次点击的图像都应该重新绘制。
这是我的代码:
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
// See https://github.com/flutter/flutter/wiki/Desktop-shells#target-platform-override
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(body: ImageGridView()),
);
}
}
class ImageGridView extends StatelessWidget {
List<File> _fileList = [];
ImageGridView(){
_fileList = [
File('C:/flutter/img/1.jpg'),
File('C:/flutter/img/3.jpg'),
File('C:/flutter/img/4.jpg'),
File('C:/flutter/img/5.jpg'),
File('C:/flutter/img/6.jpg'),
File('C:/flutter/img/7.jpg'),
File('C:/flutter/img/8.jpg'),
File('C:/flutter/img/9.jpg'),
File('C:/flutter/img/10.jpg'),
File('C:/flutter/img/11.jpg'),
File('C:/flutter/img/12.jpg'),
File('C:/flutter/img/13.jpg'),
File('C:/flutter/img/14.jpg'),
File('C:/flutter/img/15.jpg'),
File('C:/flutter/img/16.jpg'),
];
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Wrap(
children: _fileList.map((file) {
return WindowsAsset(file);
}).toList(),
),
);
}
}
class WindowsAsset extends StatefulWidget {
final File _file;
WindowsAsset(this._file);
@override
State<StatefulWidget> createState() => WindowsAssetState();
}
class WindowsAssetState extends State<WindowsAsset> {
bool selected = false;
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width / 2;
final height = width * 1080 / 1920;
return Container(
width: width,
height: height,
child: Container(
child: Container(
constraints: BoxConstraints.expand(),
decoration: !selected
? null
: BoxDecoration(
border: Border.all(
color: Color.fromRGBO(153, 209, 255, 1),
width: 4
),
),
child: Container(
child: GestureDetector(
child: Image.file(
widget._file,
filterQuality: FilterQuality.medium,
fit: BoxFit.fill,
gaplessPlayback: true,
),
onTap: () => setState(() {
selected = !selected;
}),
),
),
),
),
);
}
}
我该如何解决?谢谢!