Last active
October 12, 2018 11:08
-
-
Save figengungor/520b70be318bd79e1dc126185b8de19b to your computer and use it in GitHub Desktop.
Image Cache Clear Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:http/http.dart' as http; | |
import 'package:flutter/painting.dart' as painting; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: PhotosPage(), | |
); | |
} | |
} | |
class PhotosPage extends StatefulWidget { | |
@override | |
_PhotosPageState createState() => _PhotosPageState(); | |
} | |
class _PhotosPageState extends State<PhotosPage> { | |
Future<List<Photo>> _futurePhotos; | |
@override | |
void initState() { | |
super.initState(); | |
_futurePhotos = JsonPlaceholderApi().getPhotos(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("Photos"),), | |
body: RefreshIndicator( | |
child: Center( | |
child: FutureBuilder<List<Photo>>( | |
future: _futurePhotos, | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return _getGridView(snapshot.data); | |
} else if (snapshot.hasError) { | |
return getErrorMessage(snapshot.error, _onRetryPressed); | |
} | |
// By default, show a loading spinner | |
return CircularProgressIndicator(); | |
}, | |
), | |
), onRefresh: _handleRefresh, | |
), | |
); | |
} | |
_getGridView(List<Photo> data) { | |
if (data.isNotEmpty) { | |
return GridView.builder( | |
itemCount: data.length, | |
itemBuilder: (context, index) { | |
return _getGridItem(data[index]); | |
}, | |
gridDelegate: | |
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), | |
); | |
} else { | |
return Center(child: Text('No data found.')); | |
} | |
} | |
_getGridItem(Photo photo) { | |
return GridTile( | |
child: Image.network( | |
photo.thumbnailUrl, | |
fit: BoxFit.cover, | |
), | |
footer: Container( | |
color: Color(0x60000000), | |
padding: EdgeInsets.all(8.0), | |
child: Text( | |
photo.title, | |
style: TextStyle(color: Colors.white), | |
)), | |
); | |
} | |
_onRetryPressed() { | |
setState(() { | |
_futurePhotos = JsonPlaceholderApi().getPhotos(); | |
}); | |
} | |
Future<Null> _handleRefresh() async { | |
setState(() { | |
_futurePhotos = JsonPlaceholderApi().getPhotos(); | |
}); | |
painting.imageCache.clear(); | |
return null; | |
} | |
} | |
class JsonPlaceholderApi { | |
static const String baseUrl = 'jsonplaceholder.typicode.com'; | |
static const String photosPath = 'photos'; | |
Future<List<Photo>> getPhotos() async { | |
final uri = Uri.https(baseUrl, photosPath); | |
final jsonResponse = await _getJsonResponse(uri); | |
final photos = jsonResponse | |
.map<Photo>((dynamic item) => Photo.fromJson(item)) | |
.toList(); | |
return photos; | |
} | |
Future<List<dynamic>> _getJsonResponse(Uri uri) async { | |
var httpResponse = await http.get(uri); | |
if (httpResponse.statusCode == HttpStatus.OK) { | |
var jsonResponse = json.decode(httpResponse.body); | |
return jsonResponse; | |
} else { | |
throw Exception('\nStatus code:${httpResponse.statusCode}'); | |
} | |
} | |
} | |
class Photo { | |
final int albumId; | |
final int id; | |
final String title; | |
final String url; | |
final String thumbnailUrl; | |
Photo.fromJson(Map<String, dynamic> jsonMap) | |
: this.albumId = jsonMap['albumId'], | |
this.id = jsonMap['id'], | |
this.title = jsonMap['title'], | |
this.url = jsonMap['url'], | |
this.thumbnailUrl = jsonMap['thumbnailUrl']; | |
} | |
getErrorMessage(Object error, VoidCallback onRetryPressed) { | |
var errorMessage; | |
if (error is TimeoutException) { | |
errorMessage = 'Time out'; | |
} else if (error is IOException) { | |
errorMessage = 'No Internet Connection'; | |
} else { | |
errorMessage = error.toString(); | |
} | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
"$errorMessage", | |
textAlign: TextAlign.center, | |
), | |
SizedBox( | |
height: 16.0, | |
), | |
RaisedButton( | |
onPressed: onRetryPressed, | |
child: Text('Retry'), | |
), | |
], | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment