Load of Flutter resources

-Assets-fonts iconfont. Ttf-img-2.0x-3.0x Weik.pngCopy the code

The picture

All images are stored in different folders at different rates, as referenced in pubspec.yaml

flutter:

  # To add assets to your application, add an assets section, like this:
  assets:
     - assets/img/
     - Assets/img/x / 2.0
     - Assets/img/x / 3.0
Copy the code

To make it easy to reference image resources in our code, we generate an R.art resource file

class R {
  static final String assetsImgWechat = 'assets/img/wechat';
}
Copy the code

You can then use it directly in your code:

Image.asset(R.assetsImgWechat, width: 100.0)
Copy the code

Image processing script

// Copyright 2018 DebuggerX <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

var preview_server_port = 2227;

void main() async {
  generatorByAssets();
}

void generatorByAssets() async {
  var directory = new Directory("assets/img");
  var newLines = <String> [];var varNames = <String> [];var resource = <String> [];if (directory.existsSync()) {
    var list = directory.listSync(recursive: false);
    for (var file in list) {
      if (newFile(file.path).statSync().type == FileSystemEntityType.file && ! file.path.contains('.DS_Store')) {
        if (file.path.contains('@2x')) {
          var newpath = file.path.replaceAll('assets/img'.'assets/img / 2.0 x');
          newpath = newpath.replaceAll('@2x'.' ');
          var f = File(file.path);
          f.copySync(newpath);
          file.delete();
          continue;
        }
        if (file.path.contains('@3x')) {
          var newpath = file.path.replaceAll('assets/img'.'assets/img / 3.0 x');
          newpath = newpath.replaceAll('@3x'.' ');
          var f = File(file.path);
          f.copySync(newpath);
          file.delete();
          continue;
        }

        var path = file.path.replaceAll('\ \'.'/');
        var varName = path
            .replaceAll(The '-'.'_')
            .replaceAll('/'.'_')
            .replaceAll('.png'.' ')
            .replaceAll('.jpg'.' ')
            .replaceAll('.jpeg'.' ')
            .toLowerCase();
        var pos = 0;
        String char;
        while (true) {
          pos = varName.indexOf('_', pos);
          if (pos == - 1) break;
          char = varName.substring(pos + 1, pos + 2);
          varName = varName.replaceFirst('_$char'.'_${char.toUpperCase()}');
          pos++;
        }
        varName = varName.replaceAll('_'.' ');
        resource.add("static final String $varName = '$path';");
        varNames.add("    $varName,");
        newLines.add('-$path'); }}}else {
    throw new FileSystemException('Directory wrong');
  }

  var r = new File('lib/common/r.dart');
  if (r.existsSync()) {
    r.deleteSync();
  }
  r.createSync();
  var content = 'class R {\n';
  for (var line in resource) {
    content = '$content  $line\n';
  }
  content = '$content} ';
  // content = '$content\n static final values = [\n';
  // for (var line in varNames) {
  // content = '$content$line\n';
  // }
  // content = '$content  ];\n}\n';
  r.writeAsStringSync(content);
}

void generatorByPubspec() async {
  bool working = false;
  var pubSpec = new File('pubspec.yaml');
  var pubLines = pubSpec.readAsLinesSync();
  var newLines = <String> [];var varNames = <String> [];var resource = <String> [];for (var line in pubLines) {
    if (line.contains('begin') &&
        line.contains(The '#') &&
        line.contains('assets')) {
      working = true;
      newLines.add(line);
    }
    if (line.contains('end') && line.contains(The '#') && line.contains('assets'))
      working = false;
    if (working) {
      if (line.trim().startsWith(The '#') && line.trim().endsWith(The '*')) {
        newLines.add(line);
        var directory =
            new Directory(line.replaceAll(The '#'.' ').replaceAll(The '*'.' ').trim());
        if (directory.existsSync()) {
          var list = directory.listSync(recursive: true);
          for (var file in list) {
            if (new File(file.path).statSync().type ==
                FileSystemEntityType.file) {
              var path = file.path.replaceAll('\ \'.'/');
              var varName =
                  path.replaceAll('/'.'_').replaceAll('. '.'_').toLowerCase();
              var pos = 0;
              String char;
              while (true) {
                pos = varName.indexOf('_', pos);
                if (pos == - 1) break;
                char = varName.substring(pos + 1, pos + 2);
                varName =
                    varName.replaceFirst('_$char'.'_${char.toUpperCase()}');
                pos++;
              }
              varName = varName.replaceAll('_'.' ');
              resource
                  .add("/ / /! [] (HTTP: / / http://127.0.0.1:$preview_server_port/$path)");
              resource.add("static final String $varName = '$path';");
              varNames.add("    $varName,");
              newLines.add('-$path'); }}}else {
          throw new FileSystemException('Directory wrong'); }}}else{ newLines.add(line); }}var r = new File('lib/r.dart');
  if (r.existsSync()) {
    r.deleteSync();
  }
  r.createSync();
  var content = 'class R {\n';
  for (var line in resource) {
    content = '$content  $line\n';
  }
  content = '$content\n  static final values = [\n';
  print(content);

  for (var line in varNames) {
    content = '$content  $line\n';
  }
  content = '$content]; \n}\n';
  r.writeAsStringSync(content);

  var spec = ' ';
  for (var line in newLines) {
    spec = '$spec$line\n';
  }
  pubSpec.writeAsStringSync(spec);

  var ser;
  try {
    ser = await HttpServer.bind('127.0.0.1', preview_server_port);
    print('Successfully start the image preview server on the machine <$preview_server_port> port ');
    ser.listen(
      (req) {
        var index = req.uri.path.lastIndexOf('. ');
        var subType = req.uri.path.substring(index + 1);
        print(subType); req.response .. headers.contentType =new ContentType('image', subType) .. add(new File('.${req.uri.path}').readAsBytesSync()) .. close(); }); }catch (e) {
    print('Image Preview server started or port occupied'); }}Copy the code

Font

  1. Import font icon file; This step is the same as importing the font file, assuming that our font icon file is saved in the root directory of the project, the path is “fonts/iconfont. TTF “:

    Fonts: -family: myIcon # Specifies a font name. Fonts: -asset: fonts/iconfontCopy the code
  2. For ease of use, we define a MyIcons class that does the same thing as the Icons class: define all Icons in the font file as static variables:

    class MyIcons{
      / / book icon
      static const IconData book = const IconData(
          0xe614, 
          fontFamily: 'myIcon', 
          matchTextDirection: true
      );
      // Wechat icon
      static const IconData wechat = const IconData(
          0xec7d,  
          fontFamily: 'myIcon', 
          matchTextDirection: true
      );
    }
    Copy the code
  3. use

    Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Icon(MyIcons.book,color: Colors.purple,),
        Icon(MyIcons.wechat,color: Colors.green,),
      ],
    )
    Copy the code