Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tile handling #1356

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 0 additions & 74 deletions lib/src/layer/tile_layer/animated_tile.dart

This file was deleted.

26 changes: 13 additions & 13 deletions lib/src/layer/tile_layer/tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Tile {
DateTime? loaded;
late DateTime loadStarted;

AnimationController? animationController;
final AnimationController? animationController;

double get opacity => animationController == null
? (active ? 1.0 : 0.0)
Expand All @@ -33,12 +33,18 @@ class Tile {
required this.coords,
required this.tilePos,
required this.imageProvider,
required final TickerProvider vsync,
this.tileReady,
this.current = false,
this.active = false,
this.retain = false,
this.loadError = false,
});
final Duration? duration,
}) : animationController = duration != null
? AnimationController(duration: duration, vsync: vsync)
: null {
animationController?.addStatusListener(_onAnimateEnd);
}

void loadTileImage() {
loadStarted = DateTime.now();
Expand Down Expand Up @@ -78,15 +84,9 @@ class Tile {
_imageStream?.removeListener(_listener);
}

void startFadeInAnimation(Duration duration, TickerProvider vsync,
{double? from}) {
animationController?.removeStatusListener(_onAnimateEnd);
animationController?.dispose();

animationController = AnimationController(duration: duration, vsync: vsync)
..addStatusListener(_onAnimateEnd);

animationController!.forward(from: from);
void startFadeInAnimation({double? from}) {
animationController?.reset();
animationController?.forward(from: from);
}

void _onAnimateEnd(AnimationStatus status) {
Expand All @@ -96,14 +96,14 @@ class Tile {
}

void _tileOnLoad(ImageInfo imageInfo, bool synchronousCall) {
if (null != tileReady) {
if (tileReady != null) {
this.imageInfo = imageInfo;
tileReady!(coords, null, this);
}
}

void _tileOnError(dynamic exception, StackTrace? stackTrace) {
if (null != tileReady) {
if (tileReady != null) {
tileReady!(
coords, exception ?? 'Unknown exception during loadTileImage', this);
}
Expand Down
10 changes: 4 additions & 6 deletions lib/src/layer/tile_layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {

final tileWidgets = <Widget>[
for (var tile in tilesToRender)
TileWidget(
AnimatedTile(
tile: tile,
size: _tileSize,
tileTransformation: zoomToTransformation[tile.coords.z] ??
Expand Down Expand Up @@ -633,6 +633,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
imageProvider:
widget.tileProvider.getImage(coords.wrap(_wrapX, _wrapY), widget),
tileReady: _tileReady,
vsync: this,
duration: widget.tileFadeInDuration,
);

_tileManager.add(coords, newTile);
Expand Down Expand Up @@ -712,11 +714,7 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
(tile.loadError && null == widget.errorImage)) {
tile.active = true;
} else {
tile.startFadeInAnimation(
widget.tileFadeInDuration!,
this,
from: fadeInStart,
);
tile.startFadeInAnimation(from: fadeInStart);
}

if (mounted) {
Expand Down
26 changes: 18 additions & 8 deletions lib/src/layer/tile_layer/tile_widget.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/tile_layer/animated_tile.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_transformation.dart';

class TileWidget extends StatelessWidget {
class AnimatedTile extends StatelessWidget {
final Tile tile;
final CustomPoint size;
final TileTransformation tileTransformation;
final ImageProvider? errorImage;
final TileBuilder? tileBuilder;

const TileWidget({
const AnimatedTile({
required this.tile,
required this.size,
required this.tileTransformation,
Expand All @@ -26,16 +25,27 @@ class TileWidget extends StatelessWidget {
final num width = size.x * tileTransformation.scale;
final num height = size.y * tileTransformation.scale;

Widget tileWidget;
if (tile.loadError && errorImage != null) {
tileWidget = Image(image: errorImage!);
} else if (tile.animationController == null) {
tileWidget = RawImage(image: tile.imageInfo?.image, fit: BoxFit.fill);
} else {
tileWidget = AnimatedBuilder(
animation: tile.animationController!,
builder: (context, child) => RawImage(
image: tile.imageInfo?.image,
fit: BoxFit.fill,
opacity: tile.animationController!),
);
}

return Positioned(
left: pos.x.toDouble(),
top: pos.y.toDouble(),
width: width.toDouble(),
height: height.toDouble(),
child: AnimatedTile(
tile: tile,
errorImage: errorImage,
tileBuilder: tileBuilder,
),
child: tileBuilder?.call(context, tileWidget, tile) ?? tileWidget,
);
}
}