当前位置: 首页 > news >正文

Flutter中实现中国省份地图

Flutter中实现中国省份地图功能开发指南

局部效果展示

可以点击省份改变颜色,更多功能可以自行拓展。

Flutter中实现中国省份地图_android

本文记录在Flutter项目中安卓端实现中国地图的过程。由于实现是通过Flutter调用安卓原生代码完成,iOS端需要另行处理。对于iOS开发者,可以考虑使用Appuploader这样的工具来简化iOS应用的测试和发布流程。

在Flutter中打开android文件夹

右键android文件夹,选择Flutter -> Open Android module in Android Studio

Flutter中实现中国省份地图_android_02

点击后会像打开一个纯Android项目一样,在这个界面中可以编写原生代码和相应插件。

如果你是第一次打开,它会下载gradle和构建项目需要的依赖。这个过程可能需要一些时间。

常见问题解决方案

Gradle下载超时问题

方法一:
修改仓库配置为阿里镜像:

allprojects {
    repositories {
        google()
        maven { url "https://jitpack.io" }
        mavenCentral()
        maven { url "https://maven.aliyun.com/repository/public" }
    }
}

方法二:
手动下载对应版本的Gradle并放到指定目录下。

构建失败问题

[flutter项目]/android/build.gradle中,注释掉一行代码:

rootProject.buildDir = '../build'
subprojects {
    //project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

注意:在Flutter运行的时候,记得取消这行的注释。

原生代码编写

核心代码需要放到android\app\src\main\res\raw文件夹下。

目录结构如下图所示:

Flutter中实现中国省份地图_android_03

在MainActivity中加入以下代码:

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        flutterEngine.plugins.add(mutableSetOf<FlutterPlugin>(ChinaProvinceViewFlutterPlugin()))
    }
}

Flutter端代码实现

china_province_view.dart

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

const _ChinaProvinceView_TAG =
    'com.mrper.coronavirus.widgets.china-province-view';

class ChinaProvinceView extends StatefulWidget {
  ChinaProvinceView({
    required this.width,
    required this.onViewCreated,
  })  : assert(width != null && width > 0, '地图宽度必须大于0');

  final double width;
  final Function(int id) onViewCreated;

  
  _ChinaProvinceViewState createState() => _ChinaProvinceViewState();
}

class _ChinaProvinceViewState extends State<ChinaProvinceView> {
  final double _mapWHRatio = 1369.0 / 1141.0;

  
  Widget build(BuildContext context) => SizedBox(
      width: widget.width,
      height: widget.width / _mapWHRatio,
      child: AndroidView(
          viewType: _ChinaProvinceView_TAG,
          creationParamsCodec: StandardMessageCodec(),
          onPlatformViewCreated: widget.onViewCreated));
}

class ChinaProvinceViewController {
  late MethodChannel? _methodChannel;
  late EventChannel? _eventChannel;

  ChinaProvinceViewController(int viewId) {
    _methodChannel = MethodChannel('$_ChinaProvinceView_TAG-$viewId');
    _eventChannel = EventChannel('$_ChinaProvinceView_TAG-$viewId-event');
  }

  set selectedBackgroundColor(int value) => _methodChannel?.invokeMethod(
      'setSelectedBackgroundColor', {'value': value ?? Colors.red.value});

  void dispose() {
    if (_methodChannel != null) {
      _methodChannel?.setMethodCallHandler(null);
      _methodChannel = null;
    }
    if (_eventChannel != null) {
      _eventChannel = null;
    }
  }
}

map_page.dart

import 'package:ecology/component/app_bar.dart';
import 'package:flutter/material.dart';
import 'china_province_view.dart';

class MapPage extends StatefulWidget {
  
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  late ChinaProvinceViewController _chinaProvinceViewController;

  void _onChinaProvinceViewCreated(int viewId) {
    _chinaProvinceViewController = ChinaProvinceViewController(viewId)
      ..selectedBackgroundColor = Colors.blue.value;
  }

  
  Widget build(BuildContext context) => Container(
    child: SingleChildScrollView(
        child: Column(children: [_buildChinaMapView()])),
  );

  Widget _buildChinaMapView() {
    return Container(
        margin: const EdgeInsets.all(5),
        child: ChinaProvinceView(
            width: MediaQuery.of(context).size.width - 10,
            onViewCreated: _onChinaProvinceViewCreated));
  }

  
  void dispose() {
    _chinaProvinceViewController?.dispose();
    super.dispose();
  }
}

总结

通过本文,我们实现了在Flutter项目中调用安卓原生代码来展示中国省份地图的功能。对于iOS开发者,可以考虑使用Appuploader这样的工具来简化iOS应用的测试和发布流程,提高开发效率。

_chinaProvinceViewController?.dispose();
super.dispose();
}
}


## 总结

通过本文,我们实现了在Flutter项目中调用安卓原生代码来展示中国省份地图的功能。对于iOS开发者,可以考虑使用Appuploader这样的工具来简化iOS应用的测试和发布流程,提高开发效率。

该篇文章代码参考自gitee上的开源项目。
http://www.dtcms.com/a/107895.html

相关文章:

  • dom操作笔记、xml和document等
  • C语言学习笔记
  • 如何平衡元器件成本与性能
  • Day19 -实例:xcx逆向提取+微信开发者工具动态调试+bp动态抓包对小程序进行资产收集
  • React-Markdown详解
  • 解决大小写、保留字与特殊字符问题!Oracle双引号在SQL中的特殊应用
  • 论文阅读笔记:Denoising Diffusion Implicit Models (4)
  • PyTorch 激活函数
  • PyQt5和OpenCV车牌识别系统
  • Java基础 4.2
  • Mysql 在什么样的情况下会产生死锁?
  • Python爬虫第2节-网页基础和爬虫基本原理
  • 2.Linux的权限理解
  • mysql docker容器启动遇到的问题整理
  • 华为面试,机器学习深度学习知识点:
  • Windows C++ 排查死锁
  • MIT6.S081 - Lab6 Copy-on-Write(写时复制)
  • 模拟集成电路设计与仿真 : Mismatch
  • 数据库 第一章 MYSQL基础(4)
  • 《汽车噪声控制》课程作业
  • 英飞凌高信噪比MEMS麦克风驱动人工智能交互
  • Pandas基础及series对象
  • Token是什么?
  • 时序数据库 InfluxDB(六)
  • Python爬虫第一战(爬取优美图库网页图片)
  • *快排延伸-自省排序
  • conda activate激活环境失败问题
  • 《雷神之锤 III 竞技场》快速求平方根倒数的计算探究
  • conda 激活环境vscode的Bash窗口
  • 数据清洗的具体内容