Flutter矢量图标管理插件galli_vector_plugin的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter矢量图标管理插件galli_vector_plugin的使用

galli_vector_plugin 是一个用于在 Flutter 应用程序中集成来自 Gallimaps 的矢量地图的插件。该插件提供了方便的方式来展示和交互高质量的矢量地图。

特性

  • 高性能矢量地图
  • 地图标记、路线、填充和圆圈
  • 用户交互处理(例如,点击、缩放、倾斜、平移)
  • 自动地图缓存以供离线使用

安装

pubspec.yaml 文件中添加 galli_vector_plugin 依赖:

dependencies:
  galli_vector_plugin: latest

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

使用

在 Dart 代码中导入插件:

import 'package:galli_vector_plugin/galli_vector_plugin.dart';

基本示例

以下是一个简单的示例,展示如何使用 galli_vector_plugin

GalliMap(
  showCurrentLocation: true,
  authToken: "authToken",
  size: (
    height: MediaQuery.of(context).size.height * 2,
    width: MediaQuery.of(context).size.width * 2,
  ),
  compassPosition: (
    position: CompassViewPosition.topRight,
    offset: const Point(32, 82)
  ),
  showCompass: true,
  onMapCreated: (newC) {
    controller = newC;
    setState(() {});
  },
  onMapClick: (LatLng latLng) {

  },
),

完整示例

下面是一个完整的示例,展示了如何在 Flutter 应用程序中使用 galli_vector_plugin

import 'dart:developer';
import 'dart:math' hide log;

import 'package:flutter/material.dart';
import 'package:galli_vector_plugin/galli_vector_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const VectorMap(),
    );
  }
}

class VectorMap extends StatefulWidget {
  const VectorMap({super.key});

  [@override](/user/override)
  State<VectorMap> createState() => _VectorMapState();
}

class _VectorMapState extends State<VectorMap> {
  GalliMapController? controller;
  GalliMethods methods = GalliMethods("token");
  List<Marker> markers = [];
  late void Function() clearMarkers;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: GalliMap(
            showCurrentLocation: true,
            authToken: "token",
            size: (
              height: MediaQuery.of(context).size.height * 2,
              width: MediaQuery.of(context).size.width * 2,
            ),
            compassPosition: (
              position: CompassViewPosition.topRight,
              offset: const Point(32, 82)
            ),
            showCompass: true,
            onMapCreated: (newC) {
              controller = newC;
              setState(() {});
            },
            onMapClick: (LatLng latLng) {
              methods.get360Image(latLng).then((value) {
                if (value != null) {
                  GalliViewer galliViewer = GalliViewer(
                    builder:
                        (BuildContext context, Function() methodFromChild) {
                      clearMarkers = methodFromChild;
                    },
                    image: value,
                    onTap: (latitude, longitude, tilt) {},
                    markers: markers,
                    maxMarkers: 2,
                  );
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (_) => Scaffold(
                              appBar: AppBar(
                                actions: [
                                  GestureDetector(
                                      onTap: () {
                                        clearMarkers();
                                      },
                                      child: const Text("Clear"))
                                ],
                              ),
                              body: galliViewer)));
                } else {
                  log("Image not found");
                }
              });
            },
          ),
        ),
      ),
    );
  }
}

更多关于Flutter矢量图标管理插件galli_vector_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter矢量图标管理插件galli_vector_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用galli_vector_plugin来管理矢量图标的示例代码。galli_vector_plugin是一个允许你在Flutter应用中轻松管理和使用矢量图标的插件。

1. 添加依赖

首先,你需要在你的pubspec.yaml文件中添加galli_vector_plugin的依赖。

dependencies:
  flutter:
    sdk: flutter
  galli_vector_plugin: ^最新版本号 # 替换为实际的最新版本号

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

2. 配置图标资源

假设你已经有一些SVG图标文件,你需要将它们放置在项目的assets文件夹中。例如,你可以创建一个assets/icons目录来存放你的SVG图标。

3. 初始化插件

在你的main.dart文件或者合适的初始化位置,你需要初始化GalliVectorPlugin

import 'package:flutter/material.dart';
import 'package:galli_vector_plugin/galli_vector_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 初始化 GalliVectorPlugin
    GalliVectorPlugin.initialize(context, assetPath: 'assets/icons/');

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Galli Vector Plugin Demo'),
        ),
        body: Center(
          child: IconDemo(),
        ),
      ),
    );
  }
}

4. 使用矢量图标

现在你可以在你的应用中使用矢量图标了。例如,假设你有一个名为example.svg的图标文件,你可以这样使用它:

import 'package:flutter/material.dart';
import 'package:galli_vector_plugin/galli_vector_plugin.dart';

class IconDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        // 使用 GalliVectorIcon 小部件来显示矢量图标
        GalliVectorIcon(
          'example', // 不包含文件扩展名
          size: 48,
          color: Colors.blue,
        ),
        SizedBox(height: 20),
        GalliVectorIcon(
          'example',
          size: 64,
          color: Colors.red,
        ),
      ],
    );
  }
}

5. 完整示例

结合上述步骤,这里是一个完整的示例应用:

import 'package:flutter/material.dart';
import 'package:galli_vector_plugin/galli_vector_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    GalliVectorPlugin.initialize(context, assetPath: 'assets/icons/');

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Galli Vector Plugin Demo'),
        ),
        body: Center(
          child: IconDemo(),
        ),
      ),
    );
  }
}

class IconDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        GalliVectorIcon(
          'example',
          size: 48,
          color: Colors.blue,
        ),
        SizedBox(height: 20),
        GalliVectorIcon(
          'example',
          size: 64,
          color: Colors.red,
        ),
      ],
    );
  }
}

确保你的SVG图标文件已经被正确放置在assets/icons目录下,并且在pubspec.yaml文件中添加了相应的资源引用:

flutter:
  assets:
    - assets/icons/example.svg

这样,你就可以在Flutter应用中成功使用galli_vector_plugin来管理和显示矢量图标了。

回到顶部