Flutter地图动画插件gg_map_animation的使用

Flutter地图动画插件gg_map_animation的使用

gg_map_animation 是一个用于在 Flutter 中实现地图动画的扩展库。它通过为 Animation 类添加 .map(...) 方法,允许开发者将动画值映射到不同的目标值(例如角度)。本文将详细介绍如何使用该插件,并提供完整的示例代码。


Getting Started(开始使用)

要使用 gg_map_animation,首先需要安装该插件。在 pubspec.yaml 文件中添加以下依赖:

dependencies:
  gg_map_animation: ^版本号

然后运行 flutter pub get 安装依赖。

基本用法

假设你已经有一个动画实例 animation,可以使用 .map(...) 函数将其值映射到另一个值,例如角度。

final mappedAnimation = animation.map((i) => 2 * pi * i);

上述代码将输入动画值 i 映射为从 0 的角度。


示例代码

以下是一个完整的示例,展示如何使用 gg_map_animation 创建一个旋转动画。

示例代码解释

文件结构

example/
├── lib/
│   └── main.dart

示例代码 (main.dart)

// [@license](/user/license)
// Copyright (c) 2019 - 2021 Dr. Gabriel Gatzsche. All Rights Reserved.
//
// Use of this source code is governed by terms that can be
// found in the LICENSE file in the root of this package.

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:gg_map_animation/gg_map_animation.dart'; // 引入 gg_map_animation 插件

// 主程序入口
void main() {
  runApp(MyApp());
}

// 应用程序根组件
class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'GgMapAnimationExample', // 应用名称
      theme: ThemeData(primarySwatch: Colors.blue), // 主题颜色
      home: GgMapAnimationExample(), // 主页面
    );
  }
}

// 自定义动画示例页面
class GgMapAnimationExample extends StatefulWidget {
  GgMapAnimationExample({Key? key}) : super(key: key);

  [@override](/user/override)
  _GgMapAnimationExampleState createState() => _GgMapAnimationExampleState();
}

// 状态管理类
class _GgMapAnimationExampleState extends State<GgMapAnimationExample>
    with TickerProviderStateMixin {
  // 初始化动画控制器
  late AnimationController _animation;
  late Animation<double> _mappedAnimation;

  [@override](/user/override)
  void initState() {
    super.initState();
    _animation = AnimationController(
      vsync: this, // 绑定到当前状态
      duration: Duration(seconds: 3), // 动画持续时间
    );

    // 将动画值映射为角度(0 到 2π)
    _mappedAnimation = _animation.map((inVal) => 2 * pi * inVal);
  }

  // 按钮点击事件
  void _buttonPressed() {
    if (_animation.status == AnimationStatus.forward ||
        _animation.status == AnimationStatus.completed) {
      _animation.reverse(); // 如果正在播放,则反转动画
    } else {
      _animation.forward(); // 否则开始动画
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GgMapAnimationExample'), // 页面标题
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center, // 子元素居中对齐
          children: [
            // 使用 AnimatedBuilder 构建旋转动画
            AnimatedBuilder(
              animation: _mappedAnimation, // 使用映射后的动画
              child: Container(
                width: 100, // 容器宽度
                height: 100, // 容器高度
                color: Colors.blue, // 容器颜色
              ),
              builder: (context, child) {
                return Transform.rotate( // 旋转动画
                  angle: _mappedAnimation.value, // 当前动画值
                  child: child, // 被旋转的子组件
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _buttonPressed, // 点击按钮触发动画
        tooltip: 'Go', // 悬停提示
        child: Icon(Icons.rotate_left_outlined), // 图标
      ),
    );
  }
}

更多关于Flutter地图动画插件gg_map_animation的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter地图动画插件gg_map_animation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


gg_map_animation 是一个用于在 Flutter 应用中实现 Google 地图动画的插件。它可以帮助你在地图上创建平滑的动画效果,例如移动地图视角、缩放、旋转等。以下是如何使用 gg_map_animation 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 gg_map_animation 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  gg_map_animation: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入插件

在你的 Dart 文件中导入 gg_map_animation 插件:

import 'package:gg_map_animation/gg_map_animation.dart';

3. 创建地图控制器

你需要创建一个 GoogleMapController 来控制地图的动画。通常,你可以在 GoogleMaponMapCreated 回调中获取这个控制器:

GoogleMapController? _mapController;

GoogleMap(
  onMapCreated: (GoogleMapController controller) {
    _mapController = controller;
  },
  // 其他参数
);

4. 使用 gg_map_animation 创建动画

你可以使用 gg_map_animation 提供的 MapAnimation 类来创建动画。以下是一个简单的示例,展示如何将地图视角平滑地移动到新的位置:

void _animateToNewLocation() async {
  if (_mapController == null) return;

  final newLocation = LatLng(37.7749, -122.4194); // 新的经纬度
  final zoomLevel = 12.0; // 新的缩放级别

  await MapAnimation.animateTo(
    _mapController!,
    newLocation,
    zoomLevel,
    duration: Duration(seconds: 2), // 动画持续时间
  );
}

5. 触发动画

你可以在按钮点击或其他事件中触发这个动画:

ElevatedButton(
  onPressed: _animateToNewLocation,
  child: Text('Animate to New Location'),
);

6. 其他动画效果

gg_map_animation 还支持其他类型的动画,例如旋转、倾斜等。你可以根据需要调用不同的方法来实现这些效果。

void _rotateMap() async {
  if (_mapController == null) return;

  await MapAnimation.rotateTo(
    _mapController!,
    bearing: 45.0, // 新的旋转角度
    duration: Duration(seconds: 2),
  );
}

7. 完整示例

以下是一个完整的示例,展示了如何使用 gg_map_animation 插件来实现地图动画:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:gg_map_animation/gg_map_animation.dart';

class MapAnimationExample extends StatefulWidget {
  [@override](/user/override)
  _MapAnimationExampleState createState() => _MapAnimationExampleState();
}

class _MapAnimationExampleState extends State<MapAnimationExample> {
  GoogleMapController? _mapController;

  void _animateToNewLocation() async {
    if (_mapController == null) return;

    final newLocation = LatLng(37.7749, -122.4194); // 新的经纬度
    final zoomLevel = 12.0; // 新的缩放级别

    await MapAnimation.animateTo(
      _mapController!,
      newLocation,
      zoomLevel,
      duration: Duration(seconds: 2), // 动画持续时间
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Map Animation Example'),
      ),
      body: Column(
        children: [
          Expanded(
            child: GoogleMap(
              onMapCreated: (GoogleMapController controller) {
                _mapController = controller;
              },
              initialCameraPosition: CameraPosition(
                target: LatLng(37.7749, -122.4194),
                zoom: 12.0,
              ),
            ),
          ),
          ElevatedButton(
            onPressed: _animateToNewLocation,
            child: Text('Animate to New Location'),
          ),
        ],
      ),
    );
  }
}

void main() => runApp(MaterialApp(
  home: MapAnimationExample(),
));
回到顶部