svg_path_parser 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

svg_path_parser #

Pub Version (including pre-releases) Flutter Test package

A Flutter/Dart utility to parse an SVG path into a equivalent Path object from dart:ui library.

Getting Started #

Add this to your package's pubspec.yaml file:

dependencies:
  svg_path_parse: ^0.1.1

Now in your Dart code, you can use:

import 'package:svg_path_parser/svg_path_parser.dart';

You can use parseSvgPath() to parse a valid SVG path string to Path object;

Path path = parseSvgPath('m.29 47.85 14.58 14.57 62.2-62.2h-29.02z');

Examples #

View the example folder to see an example (drawing flutter logo using svg paths).

[0.1.1] - 31-May-2020 #

  • Added flag failSilently to parseSvgPath which will return an empty Path object if the provided path is invalid.

[0.1.0] - 18-April-2020 #

Changes:

  • Added example
  • Added Testcase for Parser
  • Added Docs for Parser

[0.0.9] - 17-April-2020 #

  • Initial Development release.
    • Added Testcase

example/lib/main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

/// A Stateful widget that paints flutter logo using [CustomPaint] and [Path].
class MyHomePage extends StatefulWidget {
  final paths = [
    ['m48.75 95.97-25.91-25.74 14.32-14.57 40.39 40.31z', Color(0xff02539a)],
    ['m22.52 70.25 25.68-25.68h28.87l-39.95 39.95z', Color(0xd745d1fd)],
    ['m.29 47.85 14.58 14.57 62.2-62.2h-29.02z', Color(0xff45d1fd)]
  ];
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool showBorder = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Transform.scale(
        scale: 2.0,
        child: GestureDetector(
          child: Center(
            child: Container(
              width: 100,
              height: 100,
              child: Stack(
                children: widget.paths.map((e) {
                  return CustomPaint(
                      painter: MyPainter(parseSvgPath(e[0]), e[1],
                          showPath: showBorder));
                }).toList(),
              ),
            ),
          ),
          behavior: HitTestBehavior.translucent,
          onTap: () {
            setState(() {
              // hide/show border
              showBorder = !showBorder;
            });
          },
        ),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  final Path path;
  final Color color;
  final bool showPath;
  MyPainter(this.path, this.color, {this.showPath = true});

  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = color
      ..strokeWidth = 4.0;
    canvas.drawPath(path, paint);
    if (showPath) {
      var border = Paint()
        ..color = Colors.black
        ..strokeWidth = 1.0
        ..style = PaintingStyle.stroke;
      canvas.drawPath(path, border);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  svg_path_parser: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:svg_path_parser/svg_path_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Jun 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
string_scanner ^1.0.5 1.0.5
Transitive dependencies
charcode 1.1.3
collection 1.14.12
meta 1.1.8
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test any