Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nmolaei7878 committed Aug 24, 2021
0 parents commit e67279c
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file added git
Empty file.
196 changes: 196 additions & 0 deletions lib/google_map_custom_markers.dart
Original file line number Diff line number Diff line change
@@ -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<BitmapDescriptor> 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<BitmapDescriptor>
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<BitmapDescriptor> 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<BitmapDescriptor>
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<BitmapDescriptor> 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<BitmapDescriptor> 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());
}

}
Loading

0 comments on commit e67279c

Please sign in to comment.