Skip to content

Commit

Permalink
使用FutureBuilder优化加载的图片太多的话会出现死机的情况(并没有做到完美)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyy committed May 4, 2020
1 parent a0ed2c2 commit e554a5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8825b64741e9b7fce9b9b0cf48cada3b
1035c17ca568f51d78cb3ee1a8242c67
73 changes: 39 additions & 34 deletions lib/page/rss_page/rss_detail.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rss/main.dart';
Expand Down Expand Up @@ -198,7 +196,7 @@ class _RssDetailState extends State<RssDetail> {
}
}

// 已修复加载的图片太多的话会出现死机的情况
// 修复加载的图片太多的话会出现死机的情况
class _MyWidgetFactory extends WidgetFactory {
_MyWidgetFactory(HtmlWidgetConfig config) : super(config);

Expand All @@ -219,40 +217,47 @@ class RssDetailImage extends StatefulWidget {
}

class _RssDetailImageState extends State<RssDetailImage> {
Uint8List data;

@override
void initState() {
_initAsync();
super.initState();
}

_initAsync() async {
data = await getImageData(this.widget.url);
if (mounted) {
setState(() {
data = data;
});
}
}

@override
Widget build(BuildContext context) {
// 直接通过内存加载图片,一方面可以通过异步的网络请求获取图片信息,防止线程阻塞,另一方面可以很好的将byte数组传到下一个页面,方便下载
return (data == null)
? new Center(child: new CircularProgressIndicator())
: new GestureDetector(
child: Image.memory(data),
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => PhotoViewSimpleScreen(
imageData: data,
imageProvider: MemoryImage(data),
heroTag: 'simple')),
);
});
return new FutureBuilder(
future: getImageData(this.widget.url),
builder: (BuildContext context, AsyncSnapshot snapshot) {
// 请求已结束
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
// 请求失败,显示错误
return new Center(
child: GestureDetector(
child: Container(
child: Text('图片加载失败,点击重新加载',
style: TextStyle(color: Colors.white)),
color: Colors.black45),
onTap: () {
getImageData(this.widget.url);
},
));
} else {
// 请求成功,显示数据
return new GestureDetector(
child: Image.memory(snapshot.data),
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => PhotoViewSimpleScreen(
imageData: snapshot.data,
imageProvider: MemoryImage(snapshot.data),
heroTag: 'simple')),
);
});
}
} else {
// 请求未结束,显示loading
return new Center(child: CircularProgressIndicator());
}
},
);
}

getImageData(String url) async {
Expand Down
4 changes: 2 additions & 2 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PODS:
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral/.symlinks/flutter/darwin-x64`)
- FlutterMacOS (from `Flutter/ephemeral/.symlinks/flutter/darwin-x64-release`)
- path_provider (from `Flutter/ephemeral/.symlinks/plugins/path_provider/macos`)
- path_provider_macos (from `Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos`)
- shared_preferences (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences/macos`)
Expand All @@ -32,7 +32,7 @@ SPEC REPOS:

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral/.symlinks/flutter/darwin-x64
:path: Flutter/ephemeral/.symlinks/flutter/darwin-x64-release
path_provider:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider/macos
path_provider_macos:
Expand Down

0 comments on commit e554a5f

Please sign in to comment.