From e67279cccf53cb1e2385d11bcfb2cb5c3fcd423e Mon Sep 17 00:00:00 2001 From: mohammad molaei Date: Wed, 25 Aug 2021 02:21:37 +0430 Subject: [PATCH] initial commit --- .gitignore | 75 +++++ .metadata | 10 + CHANGELOG.md | 3 + LICENSE | 1 + README.md | 14 + git | 0 lib/google_map_custom_markers.dart | 196 ++++++++++++ pubspec.lock | 364 +++++++++++++++++++++++ pubspec.yaml | 55 ++++ test/google_map_custom_markers_test.dart | 12 + 10 files changed, 730 insertions(+) create mode 100644 .gitignore create mode 100644 .metadata create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 git create mode 100644 lib/google_map_custom_markers.dart create mode 100644 pubspec.lock create mode 100644 pubspec.yaml create mode 100644 test/google_map_custom_markers_test.dart diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a247422 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +build/ + +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Flutter.podspec +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/ephemeral +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/Flutter/flutter_export_environment.sh +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 diff --git a/.metadata b/.metadata new file mode 100644 index 0000000..d536721 --- /dev/null +++ b/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: f4abaa0735eba4dfd8f33f73363911d63931fe03 + channel: stable + +project_type: package diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..41cc7d8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1 + +* TODO: Describe initial release. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ba75c69 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a950e9f --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# google_map_custom_markers + +A new Flutter package. + +## Getting Started + +This project is a starting point for a Dart +[package](https://flutter.dev/developing-packages/), +a library module containing code that can be shared easily across +multiple Flutter or Dart projects. + +For help getting started with Flutter, view our +[online documentation](https://flutter.dev/docs), which offers tutorials, +samples, guidance on mobile development, and a full API reference. diff --git a/git b/git new file mode 100644 index 0000000..e69de29 diff --git a/lib/google_map_custom_markers.dart b/lib/google_map_custom_markers.dart new file mode 100644 index 0000000..95b73a8 --- /dev/null +++ b/lib/google_map_custom_markers.dart @@ -0,0 +1,196 @@ +library google_map_custom_markers; + +import 'dart:async'; +import 'dart:io'; +import 'dart:typed_data'; +import 'dart:ui' as ui; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; +import 'package:flutter_svg/flutter_svg.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; + +class MapMarker { + static Future bitmapDescriptorFromSvgAsset( + {required String assetName,required BuildContext context, + required double size, + }) async { + final mediaQuery = MediaQuery.of(context); + // Read SVG file as String + String svgString = + await DefaultAssetBundle.of(context).loadString(assetName); + // Create DrawableRoot from SVG String + DrawableRoot svgDrawableRoot = + await svg.fromSvgString(svgString, svgString); + // toPicture() and toImage() don't seem to be pixel ratio aware, so we calculate the actual sizes here + double devicePixelRatio = mediaQuery.devicePixelRatio; + double width = + size * devicePixelRatio; // where 32 is your SVG's original width + double height = size * devicePixelRatio; // same thing + // Convert to ui.Picture + ui.Picture picture = svgDrawableRoot.toPicture(size: Size(width, height)); + // Convert to ui.Image. toImage() takes width and height as parameters + // you need to find the best size to suit your needs and take into account the + // screen DPI + ui.Image image = await picture.toImage(width.toInt(), height.toInt()); + ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png); + return BitmapDescriptor.fromBytes(bytes!.buffer.asUint8List()); + } + + static Future + bitmapDescriptorFromPictureAssetWithCenterText({ + required String assetPath, + required String text, + required Size size, + double fontSize = 15, + Color fontColor = Colors.black, + FontWeight fontWeight = FontWeight.w500 + }) async { + ByteData imageFile = await rootBundle.load(assetPath); + final ui.PictureRecorder pictureRecorder = ui.PictureRecorder(); + final Canvas canvas = Canvas(pictureRecorder); + final Path clipPath = Path(); + final Radius radius = Radius.circular(size.width / 2); + clipPath.addRRect( + RRect.fromRectAndCorners( + Rect.fromLTWH(0.0, 0.0, size.width.toDouble(), size.height.toDouble()), + topLeft: radius, + topRight: radius, + bottomLeft: radius, + bottomRight: radius, + ), + ); + TextPainter painter = TextPainter(textDirection: TextDirection.ltr); + painter.text = TextSpan( + text: text, + style: TextStyle( + fontSize: fontSize, color: fontColor, fontWeight: fontWeight), + ); + + canvas.clipPath(clipPath); + final Uint8List imageUint8List = imageFile.buffer.asUint8List(); + final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List); + final ui.FrameInfo imageFI = await codec.getNextFrame(); + paintImage( + fit: BoxFit.contain, + alignment: Alignment.center, + canvas: canvas, + rect: Rect.fromLTWH(0, 0, size.width.toDouble(), size.height.toDouble()), + image: imageFI.image); + painter.layout(); + painter.paint( + canvas, + Offset((size.width * 0.5) - painter.width * 0.5, + (size.height * .5) - painter.height * 0.5)); + + final _image = await pictureRecorder + .endRecording() + .toImage(size.width.toInt(), (size.height).toInt()); + final data = await _image.toByteData(format: ui.ImageByteFormat.png); + return BitmapDescriptor.fromBytes(data!.buffer.asUint8List()); + } + + static Future bitmapDescriptorFromPictureAsset({ + required String assetPath, + required double width, + required double height, + }) async { + + ByteData imageFile = await rootBundle.load(assetPath); + final ui.PictureRecorder pictureRecorder = ui.PictureRecorder(); + final Canvas canvas = Canvas(pictureRecorder); + final Uint8List imageUint8List = imageFile.buffer.asUint8List(); + final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List); + final ui.FrameInfo imageFI = await codec.getNextFrame(); + + paintImage( + canvas: canvas, + rect: Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble()), + image: imageFI.image); + + final _image = await pictureRecorder + .endRecording() + .toImage(width.toInt(), (height).toInt()); + final data = await _image.toByteData(format: ui.ImageByteFormat.png); + return BitmapDescriptor.fromBytes(data!.buffer.asUint8List()); + } + + static Future + bitmapDescriptorFromCircleCanvasWithCenterText( + {required Size size,required String text, double fontSize = 15.0,Color circleColor = Colors.red,Color fontColor = Colors.black,FontWeight fontWeight = FontWeight.w500,}) async { + final ui.PictureRecorder pictureRecorder = ui.PictureRecorder(); + final Canvas canvas = Canvas(pictureRecorder); + final Paint paint = Paint()..color = circleColor; + final Radius radius = Radius.circular(size.width / 2); + + canvas.drawRRect( + RRect.fromRectAndCorners( + Rect.fromLTWH(0.0, 0.0, size.width.toDouble(), size.height.toDouble()), + topLeft: radius, + topRight: radius, + bottomLeft: radius, + bottomRight: radius, + ), + paint); + + TextPainter painter = TextPainter(textDirection: TextDirection.ltr); + painter.text = TextSpan( + text: text, + style: TextStyle(fontSize: fontSize, color: fontColor,fontWeight: fontWeight), + ); + + painter.layout(); + painter.paint( + canvas, + Offset((size.width * 0.5) - painter.width * 0.5, + (size.height * .5) - painter.height * 0.5)); + final img = await pictureRecorder.endRecording().toImage(size.width.toInt(), size.height.toInt()); + final data = await img.toByteData(format: ui.ImageByteFormat.png); + + return BitmapDescriptor.fromBytes(data!.buffer.asUint8List()); + } + + static Future downloadResizeCirclePicture( + {required String url, int imageSize = 50}) async { + final File imageFile = await DefaultCacheManager().getSingleFile(url); + final ui.PictureRecorder pictureRecorder = ui.PictureRecorder(); + final Canvas canvas = Canvas(pictureRecorder); + final Path clipPath = Path(); + clipPath.addRRect(RRect.fromRectAndRadius( + Rect.fromLTWH(0, 0, imageSize.toDouble(), imageSize.toDouble()), + Radius.circular(100))); + canvas.clipPath(clipPath); + final Uint8List imageUint8List = await imageFile.readAsBytes(); + final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List); + final ui.FrameInfo imageFI = await codec.getNextFrame(); + paintImage( + canvas: canvas, + rect: Rect.fromLTWH(0, 0, imageSize.toDouble(), imageSize.toDouble()), + image: imageFI.image); + final _image = await pictureRecorder + .endRecording() + .toImage(imageSize, (imageSize * 1.1).toInt()); + final data = await _image.toByteData(format: ui.ImageByteFormat.png); + return BitmapDescriptor.fromBytes(data!.buffer.asUint8List()); + } + + static Future downloadResizePicture( + {required String url, int imageSize = 50}) async { + final File imageFile = await DefaultCacheManager().getSingleFile(url); + final ui.PictureRecorder pictureRecorder = ui.PictureRecorder(); + final Canvas canvas = Canvas(pictureRecorder); + final Uint8List imageUint8List = await imageFile.readAsBytes(); + final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List); + final ui.FrameInfo imageFI = await codec.getNextFrame(); + paintImage( + canvas: canvas, + rect: Rect.fromLTWH(0, 0, imageSize.toDouble(), imageSize.toDouble()), + image: imageFI.image); + final _image = await pictureRecorder + .endRecording() + .toImage(imageSize, (imageSize * 1.1).toInt()); + final data = await _image.toByteData(format: ui.ImageByteFormat.png); + return BitmapDescriptor.fromBytes(data!.buffer.asUint8List()); + } + +} diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..67cac31 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,364 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.6.1" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.15.0" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.2" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_cache_manager: + dependency: "direct main" + description: + name: flutter_cache_manager + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.2" + flutter_plugin_android_lifecycle: + dependency: transitive + description: + name: flutter_plugin_android_lifecycle + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + flutter_svg: + dependency: "direct main" + description: + name: flutter_svg + url: "https://pub.dartlang.org" + source: hosted + version: "0.22.0" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + google_maps_flutter: + dependency: "direct main" + description: + name: google_maps_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.6" + google_maps_flutter_platform_interface: + dependency: transitive + description: + name: google_maps_flutter_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + http: + dependency: transitive + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.3" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" + matcher: + dependency: transitive + description: + name: matcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.10" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.0" + path_drawing: + dependency: transitive + description: + name: path_drawing + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.1" + path_parsing: + dependency: transitive + description: + name: path_parsing + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.1" + path_provider: + dependency: transitive + description: + name: path_provider + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + path_provider_macos: + dependency: transitive + description: + name: path_provider_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.11.1" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.0" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.3" + rxdart: + dependency: transitive + description: + name: rxdart + url: "https://pub.dartlang.org" + source: hosted + version: "0.27.1" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.1" + sqflite: + dependency: transitive + description: + name: sqflite + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0+4" + sqflite_common: + dependency: transitive + description: + name: sqflite_common + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + stack_trace: + dependency: transitive + description: + name: stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.10.0" + stream_channel: + dependency: transitive + description: + name: stream_channel + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + stream_transform: + dependency: transitive + description: + name: stream_transform + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + synchronized: + dependency: transitive + description: + name: synchronized + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + test_api: + dependency: transitive + description: + name: test_api + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.0" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + uuid: + dependency: transitive + description: + name: uuid + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.4" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.6" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.2" +sdks: + dart: ">=2.13.0 <3.0.0" + flutter: ">=2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..521d46f --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,55 @@ +name: google_map_custom_markers +description: A new Flutter package. +version: 0.0.1 +homepage: + +environment: + sdk: ">=2.12.0 <3.0.0" + flutter: ">=1.17.0" + +dependencies: + flutter: + sdk: flutter + flutter_cache_manager: ^3.1.2 + google_maps_flutter: ^2.0.6 + flutter_svg: ^0.22.0 +dev_dependencies: + flutter_test: + sdk: flutter + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter. +flutter: + + # To add assets to your package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/assets-and-images/#from-packages + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/assets-and-images/#resolution-aware. + + # To add custom fonts to your package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/custom-fonts/#from-packages diff --git a/test/google_map_custom_markers_test.dart b/test/google_map_custom_markers_test.dart new file mode 100644 index 0000000..ae5f810 --- /dev/null +++ b/test/google_map_custom_markers_test.dart @@ -0,0 +1,12 @@ +import 'package:flutter_test/flutter_test.dart'; + +import 'package:google_map_custom_markers/google_map_custom_markers.dart'; + +void main() { + test('adds one to input values', () { + final calculator = Calculator(); + expect(calculator.addOne(2), 3); + expect(calculator.addOne(-7), -6); + expect(calculator.addOne(0), 1); + }); +}