Skip to content

Commit

Permalink
Make it easier to trigger tile loading errors and prevent queued snac…
Browse files Browse the repository at this point in the history
…kbars
  • Loading branch information
rorystephenson committed Jul 24, 2023
1 parent 1633a89 commit 436b7dc
Showing 1 changed file with 69 additions and 15 deletions.
84 changes: 69 additions & 15 deletions example/lib/pages/tile_loading_error_handle.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_example/widgets/drawer.dart';
Expand All @@ -13,55 +16,62 @@ class TileLoadingErrorHandle extends StatefulWidget {
}

class _TileLoadingErrorHandleState extends State<TileLoadingErrorHandle> {
static const _showSnackbarDuration = Duration(seconds: 1);
bool _simulateTileLoadErrors = false;
DateTime? _lastShowedTileLoadError;

@override
Widget build(BuildContext context) {
var needLoadingError = true;

return Scaffold(
appBar: AppBar(title: const Text('Tile Loading Error Handle')),
drawer: buildDrawer(context, TileLoadingErrorHandle.route),
body: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
SwitchListTile(
title: const Text('Simulate tile loading errors'),
value: _simulateTileLoadErrors,
onChanged: (newValue) => setState(() {
_simulateTileLoadErrors = newValue;
}),
),
const Padding(
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text('Turn on Airplane mode and try to move or zoom map'),
child: Text(
'Enable tile load error simulation or disable internet and try to move or zoom map.'),
),
Flexible(
child: Builder(builder: (BuildContext context) {
return FlutterMap(
options: MapOptions(
initialCenter: const LatLng(51.5, -0.09),
options: const MapOptions(
initialCenter: LatLng(51.5, -0.09),
initialZoom: 5,
onPositionChanged: (MapPosition mapPosition, bool _) {
needLoadingError = true;
},
),
children: [
TileLayer(
urlTemplate:
'https://tile.openstreetmap.org/{z}/{x}/{y}.png',

// For example purposes. It is recommended to use
// TileProvider with a caching and retry strategy, like
// NetworkTileProvider or CachedNetworkTileProvider
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
evictErrorTileStrategy: EvictErrorTileStrategy.none,
errorTileCallback: (tile, error, stackTrace) {
if (needLoadingError) {
if (_showErrorSnackBar) {
_lastShowedTileLoadError = DateTime.now();
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: const Duration(seconds: 1),
duration: _showSnackbarDuration,
content: Text(
error.toString(),
style: const TextStyle(color: Colors.black),
),
backgroundColor: Colors.deepOrange,
));
});
needLoadingError = false;
}
},
tileProvider: _simulateTileLoadErrors
? _SimulateErrorsTileProvider()
: null,
),
],
);
Expand All @@ -72,4 +82,48 @@ class _TileLoadingErrorHandleState extends State<TileLoadingErrorHandle> {
),
);
}

bool get _showErrorSnackBar =>
_lastShowedTileLoadError == null ||
DateTime.now().difference(_lastShowedTileLoadError!) -
const Duration(milliseconds: 50) >
_showSnackbarDuration;
}

class _SimulateErrorsTileProvider extends TileProvider {
_SimulateErrorsTileProvider() : super();

@override
ImageProvider<Object> getImage(
TileCoordinates coordinates,
TileLayer options,
) =>
_SimulateErrorImageProvider();
}

class _SimulateErrorImageProvider
extends ImageProvider<_SimulateErrorImageProvider> {
_SimulateErrorImageProvider();

@override
ImageStreamCompleter load(
_SimulateErrorImageProvider key,
Future<ui.Codec> Function(
Uint8List, {
bool allowUpscaling,
int? cacheHeight,
int? cacheWidth,
}) decode,
) =>
_SimulateErrorImageStreamCompleter();

@override
Future<_SimulateErrorImageProvider> obtainKey(ImageConfiguration _) =>
Future.error('Simulated tile loading error');
}

class _SimulateErrorImageStreamCompleter extends ImageStreamCompleter {
_SimulateErrorImageStreamCompleter() {
throw 'Simulated tile loading error';
}
}

0 comments on commit 436b7dc

Please sign in to comment.