flutter use svg icon

https://segmentfault.com/a/1190000015132238?utm_source=index-hottest

 

This paper describes how to use svg icon flutter project. It allows the reader to practice successfully in the project in accordance with the procedures in this document.

Upgrade flutter

Because the environment is 0.3.2beta flutter version, a prompt appears when running the project: the need to use

flutter upgrade

Command to upgrade flutter

However, there has been such a mistake:

upgrade

Looks like the network caused no recourse but to re next official

Since the environment is a mac, so download mac version

https://storage.googleapis.co...

After the download is complete decompression replace the original flutter path just fine.

Use flutter_svg

Foreword

Specifically to google a bit to find these two issue

https://github.com/flutter/fl...
https://github.com/flutter/fl...

Meaning that flutter official not ready to provide all support svg, so now it is someone in the community to maintain.

github:https://github.com/dnfield/fl...

New Project

flutter create flutter_myapp

New dependence

pubspec.yaml Added:

flutter_svg: ^0.3.2

Svg file to add to the resource

We can go http://www.iconfont.cn/  find some svg

svg file all required resources in the project root folder, the folder's name, we can use the convention here choose to use the assets.

Edit pubspec.yaml, the above-mentioned svg resources to all new resource items.

  assets:
    - assets/close.svg
    - assets/set.svg
    - assets/warning.svg
    - assets/wrong.svg

So we can be so used:

SvgPicture close = new SvgPicture.asset(
      "assets/close.svg",
      color: Colors.grey,
    );

Edit main.dart


import 'package:flutter/material.dart';

import 'package:flutter_svg/flutter_svg.dart';

void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => new _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { SvgPicture close = new SvgPicture.asset( "assets/close.svg", color: Colors.grey, ); SvgPicture set = new SvgPicture.asset("assets/set.svg", color: Colors.redAccent); SvgPicture warning = new SvgPicture.asset("assets/warning.svg", color: Colors.blueAccent); SvgPicture wrong = new SvgPicture.asset("assets/wrong.svg", color: Colors.greenAccent); return new Scaffold( appBar: new AppBar( title: new Text(widget.title), ), body: new Column( children: <Widget>[ new Row( children: <Widget>[ new SizedBox( width: 50.0, height: 50.0, child: close, ), new SizedBox( width: 50.0, height: 50.0, child: set, ), new SizedBox( width: 50.0, height: 50.0, child: warning, ), new SizedBox( width: 50.0, height: 50.0, child: wrong, ), ], ) ], ), // This trailing comma makes auto-formatting nicer for build methods. ); } } 

effect:

Code:
HTTPS: //github.com/jzoom/flut ...

If you have questions, please add qq group discussions 854,192,563

 

Guess you like

Origin www.cnblogs.com/sundaysme/p/12624211.html
Recommended