Flutter学习笔记04-Icon图标组件

本文介绍如何在Flutter项目中使用内置及自定义图标,包括添加阿里图标库图标的具体步骤,并提供了从创建项目到运行的完整示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考学习视频:https://www.bilibili.com/video/BV1S4411E7LY?p=23&vd_source=cee744cae4ead27f8193fc7904647073

视频内容的学习笔记:
1.先在main.dart中敲出如下代码👇

import 'package:flutter/material.dart';

void main() {
  //const : 多个相同的实例共享存储空间
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return const Text("你好flutter");
  }
}

2.使用内置Icon组件,修改过MyHomePage 👇

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Column(
      children: const [
        SizedBox(height: 10),
        Icon(
          Icons.home,
          color: Colors.yellow,
        ),
        SizedBox(height: 10),
        Icon(
          Icons.settings,
          color: Colors.red,
          size: 60,
        )
      ],
    );
  }
}

显示效果如下图👇
在这里插入图片描述

3.在Flutter中使用阿里巴巴图标库的图标
阿里巴巴图标库:https://www.iconfont.cn
添加你喜欢的图标到购物车
在这里插入图片描述
点击下载代码
在这里插入图片描述
下载的文件
在这里插入图片描述
其中我们将json文件和ttf文件放到flutter项目,具体位置:在项目根目录创建fonts文件夹,放入json文件和ttf文件
在这里插入图片描述
放置好后,修改pubspec.yaml

# To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
   fonts:
     - family: myIcon
       fonts:
         - asset: fonts/iconfont.ttf

创建一个新的dart文件

import 'package:flutter/material.dart';

class MyIcon {
  static const IconData book =
      IconData(0xe607, fontFamily: "myIcon", matchTextDirection: true);

  static const IconData weixin =
      IconData(0xf0106, fontFamily: "myIcon", matchTextDirection: true);

  static const IconData gouwuche =
      IconData(0xe73d, fontFamily: "myIcon", matchTextDirection: true);
}

在main.dart中使用

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Column(
      children: const [
        SizedBox(height: 10),
        Icon(
          Icons.home,
          color: Colors.yellow,
        ),
        SizedBox(height: 10),
        Icon(MyIcon.book),
        SizedBox(height: 10),
        Icon(MyIcon.weixin),
        SizedBox(height: 10),
        Icon(MyIcon.gouwuche)
      ],
    );
  }
}

显示效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值