Skip to content

Instantly share code, notes, and snippets.

@pingbird
Created July 13, 2022 06:14
Show Gist options
  • Select an option

  • Save pingbird/524909de70a6e96280c71332d2ec02b2 to your computer and use it in GitHub Desktop.

Select an option

Save pingbird/524909de70a6e96280c71332d2ec02b2 to your computer and use it in GitHub Desktop.
svg to flutter icons
const gulp = require('gulp');
const iconfont = require('gulp-iconfont');
const ext_replace = require('gulp-ext-replace');
const consolidate = require('gulp-consolidate')
const runTimestamp = Math.round(Date.now() / 1000);
gulp.task('default', function () {
return gulp.src(['icons/*.svg'])
.pipe(
iconfont({
fontName: 'SvgIcons',
prependUnicode: true,
formats: ['ttf'],
timestamp: runTimestamp,
fontHeight: 2000,
normalize: true,
}),
)
.on('glyphs', function (glyphs) {
const options = {
fontPath: '../fonts/',
glyphs: glyphs.map(mapGlyph)
}
console.log(glyphs, options);
gulp.src('svg_icons.dart.tmpl')
.pipe(consolidate('lodash', options))
.pipe(ext_replace('.dart', '.dart.tmpl'))
.pipe(gulp.dest('lib/'))
})
.pipe(gulp.dest('fonts/'));
});
const snakeToCamel = (str) => str.replace(
/([-_][a-z])/g,
(group) => group.toUpperCase()
.replace('-', '')
.replace('_', '')
);
function mapGlyph(glyph) {
return {
name: snakeToCamel(glyph.name),
codepoint: glyph.unicode[0].charCodeAt(0),
}
}
{
"devDependencies": {
"gulp": "^4.0.2",
"gulp-consolidate": "^0.2.0",
"gulp-ext-replace": "^0.3.0",
"gulp-iconfont": "^11.0.0",
"gulp-rename": "^2.0.0"
}
}
import 'package:flutter/widgets.dart';
class SvgIcons {
SvgIcons._();
static const _fam = 'SvgIcons';
static const _pkg = 'svg_icons';
<% _.each(glyphs, function(glyph) { %> static const IconData <%= glyph.name %> = IconData(0x<%= glyph.codepoint.toString(16).toUpperCase() %>, fontFamily: _fam, fontPackage: _pkg);
<% }); %>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment