Flutter自带图标组件和自定义图标

自带图标组件使用

import 'package:flutter/material.dart';

void main(){
  runApp(const MyApp());
}

class MyApp extends StatelessWidget{
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.blue
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text("Flutter app")),
        body: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget{
  const MyHomePage({super.key});

  
  Widget build(BuildContext context) {
    return Column(
      children: const [
        SizedBox(height: 20,),
        Icon(Icons.home,color: Colors.red,size: 40,),
        SizedBox(height: 20,),
        Icon(Icons.settings),
        SizedBox(height: 20,),
        Icon(Icons.search),
        Icon(Icons.personal_injury_outlined),
        SizedBox(height: 20,),
        Icon(
          Icons.category,
          size: 60,
          color: Colors.blue,
        ),
        Icon(
          Icons.shop,
          size: 60,
          color: Colors.red,
        )
      ],
    );
  }
  
}

借助阿里巴巴图标库自定义字体图片

  1. 导入字体图标文件;这一步和导入字体文件相同,假设我们的字体图标文件保存在项目根目录下,路径为"fontsnconfont.ttf"
  • 阿里巴巴图标库
  • 加入购物车
  • 下载代码
  • 在根目录创建fonts文件夹,把iconfont.json和iconfont.ttf放入
  1. 找到pubspec.yaml,搜索font,引入字体的代码
# fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  fonts:
    - family: ityingIcon #指定字体名 根据自己的需求定义
      fonts:
        - asset: fonts/iconfont.ttf
  1. 为了使用方便,我们定义一个MyIcons 类,功能和Icons 类一样:将字体文件中的所有图标都定义成静态变量:
  • 创建 ifyingFont.dart
import 'package:flutter/material.dart';

class ItyingFont {
  static const IconData book = IconData(
    0xf00a1, //在iconfont.json 里 unicode
    fontFamily: "ityingIcon",
    matchTextDirection: true
  );
  static const IconData weixin = IconData(
      0xf0106, //在iconfont.json 里 unicode
      fontFamily: "ityingIcon",
      matchTextDirection: true
  );
  static const IconData cart = IconData(
      0xf00a1, //在iconfont.json 里 unicode
      fontFamily: "344b",
      matchTextDirection: true
  );
}
  • 添加新的图标,继续上述1操作

配置pubspec.yaml

  fonts:
    - family: ityingIcon #指定字体名 根据自己的需求定义
      fonts:
        - asset: fonts/iconfont.ttf
    - family: flutterIcon
      fonts:
        - asset: fonts/newiconfont.ttf

修改ifyingFont.dart

import 'package:flutter/material.dart';

class ItyingFont {
  static const IconData book = IconData(
    0xf00a1, //在iconfont.json 里 unicode
    fontFamily: "ityingIcon",
    matchTextDirection: true
  );

  static const IconData weixin = IconData(
      0xf0106, //在iconfont.json 里 unicode
      fontFamily: "ityingIcon",
      matchTextDirection: true
  );
  static const IconData cart = IconData(
      0x344b, //在iconfont.json 里 unicode
      fontFamily: "ityingIcon",
      matchTextDirection: true
  );

  static const IconData yonghu = IconData(
      0xe633, //在iconfont.json 里 unicode
      fontFamily: "flutterIcon",
      matchTextDirection: true
  );
  static const IconData address = IconData(
      0xe63c, //在iconfont.json 里 unicode
      fontFamily: "flutterIcon",
      matchTextDirection: true
  );
  static const IconData category = IconData(
      0xe62f, //在iconfont.json 里 unicode
      fontFamily: "flutterIcon",
      matchTextDirection: true
  );
}

继续添加图标

import 'package:flutter/material.dart';
import 'package:fitst_flutter_app/ifyingFont.dart';
void main(){
  runApp(const MyApp());
}

class MyApp extends StatelessWidget{
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.blue
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text("Flutter app")),
        body: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget{
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: const [
        SizedBox(height: 20,),
        Icon(Icons.home,color: Colors.red,size: 40,),
        SizedBox(height: 20,),
        Icon(Icons.settings),
        SizedBox(height: 20,),
        Icon(Icons.search),
        Icon(Icons.personal_injury_outlined),
        SizedBox(height: 20,),
        Icon(
          Icons.category,
          size: 60,
          color: Colors.blue,
        ),
        Icon(
          Icons.shop,
          size: 60,
          color: Colors.red,
        ),
        SizedBox(height: 20,),
        Icon(ItyingFont.address,size: 40,color: Colors.black,),
        SizedBox(height: 20,),
        Icon(ItyingFont.category,size: 40,color: Colors.black,),
        SizedBox(height: 20,),
        Icon(ItyingFont.book,size: 40,color: Colors.orange,),
        SizedBox(height: 20,),
        Icon(ItyingFont.weixin,size: 40,color: Colors.green,),
        SizedBox(height: 20,),
        Icon(ItyingFont.cart,size: 40,color: Colors.black,),

      ],
    );
  }
  
}

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蒋的学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值