每次我打开预览屏幕或返回到预览屏幕时,其中的图像需要1-2秒才能再次加载图像

  • 本文关键字:屏幕 图像 1-2秒 加载 返回 flutter dart
  • 更新时间 :
  • 英文 :


我有一个游戏预览屏幕,我有一个无状态的小部件支架和所有,然后一个次要的无状态小部件,以显示每个游戏的预览。但是当我打开这个预览页面时,我的所有3个预览图像都需要一些时间来加载图像,当我点击预览时,我可以玩游戏,从游戏返回后,它再次重建/加载预览图像。

class Games extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Games",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 25.0),),
),
body: SingleChildScrollView(
child: Column(
children: [
GamePreviewScreen(index: 0,),
GamePreviewScreen(index: 1,),
GamePreviewScreen(index: 2,),
],
),
),
);
}
}

预览无状态小部件:-

class GamePreviewScreen extends StatelessWidget {
final int index;
String imagePath='';
GamePreviewScreen({this.index,});
String getPath()
{
if(index==0)
return 'images/EgyptGame/preview.png';
else if(index==1)
return 'images/EgyptGame/preview.png';
else
return 'images/EgyptGame/preview.png';
}
openGame(BuildContext context)
{
if(index==0)
Navigator.push(context, MaterialPageRoute(builder: (context)=>HomePageForGame2()));
else if(index==1)
Navigator.push(context, MaterialPageRoute(builder: (context)=>HomePageForGame2()));
else
Navigator.push(context, MaterialPageRoute(builder: (context)=>HomePageForGame2()));
}
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: (){openGame(context);},
child: Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.cyan[200],width: 5),
),
child: Image.asset(getPath(),fit: BoxFit.fill,),
),
);
}
}

检查一下你的图片大小,尺寸,并将其保存为jpg格式,它会加载得很快。

将您的小部件转换为StatefulWidget或在Games页面和GamePreviewScreenGestureDetector中添加GlobalKeyScaffold

可以使用缓存结构。此外,当使用调试模式时,会提供更慢、更累人的体验。如果您以发布模式运行应用程序,此问题可能会消失。

在终端中尝试flutter run --release,也许你正在调试模式下运行应用程序。如果你在这个模式下运行应用程序会慢很多因为它有一些特性

最新更新