Flutter汽车导航插件vietmap_automotive_flutter的使用
Flutter汽车导航插件vietmap_automotive_flutter的使用
这是用于Vietmap Automotive SDK的Flutter插件,可以帮助你在Flutter应用中集成Vietmap逐向导航SDK,包括Android Auto和Apple CarPlay。
开始使用
- 将插件添加到你的
pubspec.yaml
文件中。将VIETMAP_AUTOMOTIVE_FLUTTER_VERSION
替换为插件的最新版本:
dependencies:
vietmap_automotive_flutter: ^0.0.2
- 在
settings.gradle
文件中导入Android Auto模块:
def relativePath = ""
settingsDir.eachDir { subDir ->
if(subDir.name == "app"){
def pathComponents = settingsDir.absolutePath.split('/')
def rPath = ""
pathComponents.each { component ->
rPath += '../'
}
relativePath = rPath.substring(9, rPath.length())
}
}
include ':androidauto'
project(':androidauto').projectDir = file("${relativePath}.pub-cache/hosted/pub.dev/vietmap_automotive_flutter-VIETMAP_AUTOMOTIVE_FLUTTER_VERSION/android/androidauto")
- 在
build.gradle
(模块app)文件中导入Android Auto模块:
dependencies {
implementation project(':androidauto')
}
- 在
build.gradle
(项目)和settings.gradle
文件中添加以下Maven URL:
maven { url = uri("https://www.jitpack.io") }
例如:
build.gradle
:
allprojects {
repositories {
google()
mavenCentral()
maven { url = uri("https://www.jitpack.io") }
}
}
settings.gradle
:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url = uri("https://www.jitpack.io") }
}
}
- 在
AndroidManifest.xml
文件中添加以下权限:
<uses-permission android:name="androidx.car.app.MAP_TEMPLATES" />
<uses-permission android:name="androidx.car.app.NAVIGATION_TEMPLATES" />
<uses-permission android:name="androidx.car.app.ACCESS_SURFACE" />
- 在
AndroidManifest.xml
文件的app
标签内添加以下元数据:
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc" />
示例代码
以下是使用vietmap_automotive_flutter
插件的完整示例代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:vietmap_automotive_flutter/vietmap_automotive_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = '未知';
final _vietmapAutomotiveFlutterPlugin = VietmapAutomotiveFlutter();
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 平台消息是异步的,因此我们在异步方法中初始化。
Future<void> initPlatformState() async {
String platformVersion;
// 平台消息可能会失败,因此我们使用try/catch处理PlatformException。
// 我们还处理消息可能返回null的情况。
try {
platformVersion = await _vietmapAutomotiveFlutterPlugin.getPlatformVersion() ?? '未知平台版本';
} on PlatformException {
platformVersion = '获取平台版本失败。';
}
// 如果在异步平台消息飞行时小部件从树中移除,我们应该丢弃回复而不是调用setState来更新我们的非存在外观。
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Center(
child: Text('运行在: $_platformVersion\n'),
),
),
);
}
}
更多关于Flutter汽车导航插件vietmap_automotive_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复