Image 组件详解 Icon 组件详解 ListView 组件详解

本文深入解析Flutter中的Image组件,包括加载远程和本地图片,以及实现圆形图片的方法。接着介绍Icon组件的使用,特别是如何添加自定义图标。最后详细探讨ListView组件,涵盖垂直列表、水平列表及动态列表的实现。Flutter以其高性能、美观的UI和丰富的组件库,助力开发者快速构建应用。

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

Flutter文章目录


提示:如有雷同,请联系作者删除


前言

Flutter 是一款开源的跨平台移动应用开发框架,它可以让开发者在单个代码库中构建高性能、美观的 iOS 和 Android 应用。Flutter 采用了现代化的编程语言 Dart,并自带丰富的 UI 组件和工具库。通过 Flutter,开发者可以快速地创建出具有高质量的视觉效果、流畅的用户体验、和响应式的布局的应用,同时避免了传统跨平台框架的繁琐和不稳定。


提示:以下是本篇文章正文内容,下面案例可供参考

一、Image 组件详解

1. 加载远程图片

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

  
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        margin: const EdgeInsets.all(10.0),
        width: 100,
        height: 100,
        color: Colors.red,
        child: Image.network(
          'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}

2. Container实现圆形图片

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

  
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        margin: const EdgeInsets.all(10.0),
        width: 100,
        height: 100,
        decoration: const BoxDecoration(
          shape: BoxShape.circle, // 圆形装饰线
          image: DecorationImage(
            image: NetworkImage(
              'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
            ),
            fit: BoxFit.cover, // 图片填充方式
          ),
        ),
      ),
    );
  }
}

3. clipOval 实现圆形图片

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

  
  Widget build(BuildContext context) {
    return Center(
      child: ClipOval(
        child: Image.network(
          'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
          width: 100,
          height: 100,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}

4. circleAvatar 实现圆形图片

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

  
  Widget build(BuildContext context) {
    return const Center(
      child: CircleAvatar(
        backgroundImage: NetworkImage(
          'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
        ),
        radius: 50, // 圆形半径
      ),
    );
  }
}

5. 加载本地图片

加载本地图片需要主意细节

  1. 需要找到pubspec.yaml
  2. 找到assets把你存在本地的图片放上去在这里插入图片描述
    主意格式 格式不对会加载不出来
class MyImage4 extends StatelessWidget{
  const MyImage4({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return const Center(
      child: CircleAvatar(
        backgroundImage: AssetImage(
          'images/04.png',
        ),
        radius: 50,
      ),
    );
  }
}

二、Icon 组件详解

1. Flutter 使用框架自带的Icon是非常简单啊

代码如下(示例):

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

  
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children:const [
          Icon(
            Icons.favorite,
            color: Colors.pink,
            size: 24.0,
            semanticLabel: 'Text to announce in accessibility modes', // 无障碍访问
          ),
          SizedBox(height: 20),
          Icon(
            Icons.audiotrack,
            color: Colors.green, // 图标颜色
            size: 30.0, // 图标大小
          ),
          SizedBox(height: 20),
          Icon(
            Icons.beach_access,
            color: Colors.blue,
            size: 36.0,
          ),
          SizedBox(height: 20),
          Icon(
            Icons.access_alarm,
            color: Colors.orange,
            size: 48.0,
          ),
          SizedBox(height: 20),
          Icon(
            Icons.access_time,
            color: Colors.red,
            size: 60.0,
          ),
        ]
      )
    );
  }
}

2. 这里主要是介绍自定义图片使用方法

  1. 阿里巴巴矢量图标库找到自己想要的图标添加到购物车
  2. 点击购物车图标弹出右边侧栏点击下载代码
  3. 在我们项目中新建fonts文件把下载文件里面的iconfont.ttf和iconfont.json放到我们项目的fonts中
  4. 找打pubspec.yaml文件在文件中找到fonts,中定义自定义图标
  5. 在lib里面新建utils文件夹在文件夹中新建自己自定义图片的规则

代码如下(示例):

提示:主意Unicode编码就是json里面的Unicode,前面0x是规范

// 比如我在utils里面新建的文件叫my_icon.dart

// 导入图标库
import 'package:flutter/material.dart';

// 自定义图标
class MyIcons {
  static const IconData zhifubao = IconData(
    0xe654, // 图标的Unicode编码
    fontFamily: 'myIcon', // 图标的字体
    matchTextDirection: true // 文字方向和图标方向一致
  );
  static const IconData weixin = IconData(
    0xe7ba,
    fontFamily: 'myIcon',
    matchTextDirection: true
  );
  static const IconData xiaomi = IconData(
    0xe781,
    fontFamily: 'myIcon',
    matchTextDirection: true
  );
}

使用自定义图标

import 'package:flutter/material.dart';
import 'utils/my_icon.dart'; // 导入图标库
void main() {
  runApp(MaterialApp(
      title: 'Flutter Tutorial',
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Flutter Tutorial'),
          ),
          body: Column(
            children: const [
              MyApp(),
            ],
          )
      )
  ));
}


// container 组件详解
class MyApp extends StatelessWidget{
  const MyApp({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children:const [
          // 实现自定义图标
          Icon(
            MyIcons.zhifubao,
            color: Colors.blue,
            size: 60.0,
          ),
          SizedBox(height: 20),
          Icon(
            MyIcons.weixin,
            color: Colors.green,
            size: 60.0,
          ),
          SizedBox(height: 20),
          Icon(
            MyIcons.xiaomi,
            color: Colors.orange,
            size: 60.0,
          ),
        ]
      )
    );
  }
}

2.ListView 组件详解

1.垂直列表

代码如下(示例):

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

  
  Widget build(BuildContext context) {
    return ListView(
      children: const <Widget>[
        ListTile(
          leading: Icon( // 左侧图标 主意leading和trailing的他们单单可以放图标 也可以放其他的组件
            Icons.favorite,
            color: Colors.pink,
            size: 24.0,
            semanticLabel: 'Text to announce in accessibility modes',
          ),
          title: Text('One-line with leading widget'),
          trailing: Icon( // 右侧图标
            Icons.arrow_forward_ios_outlined,
            color: Colors.grey,
            size: 20.0,
          ),
        ),
        // 线条 用于分割
        Divider(),
        ListTile(
          leading: Icon(
            Icons.audiotrack,
            color: Colors.green,
            size: 30.0,
          ),
          title: Text('One-line with leading widget'),
          trailing: Icon(
            Icons.arrow_forward_ios_outlined,
            color: Colors.grey,
            size: 20.0,
          ),
        ),
        Divider(),
        ListTile(
          leading: Icon(
            Icons.beach_access,
            color: Colors.blue,
            size: 36.0,
          ),
          title: Text('One-line with leading widget'),
          // 右侧图标
          trailing: Icon(
            Icons.arrow_forward_ios_outlined,
            color: Colors.grey,
            size: 20.0,
          ),
        ),
        Divider(),
      ],
    );
  }
}

2. 水平列表

class MyApp extends StatelessWidget{

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

  
  Widget build(BuildContext context) {
    return ListView(
      scrollDirection: Axis.horizontal, // 水平列表
      children: <Widget>[
        Container(
          width: 160.0,
          color: Colors.red,
        ),
        Container(
          width: 160.0,
          color: Colors.blue,
        ),
        Container(
          width: 160.0,
          color: Colors.green,
        ),
        Container(
          width: 160.0,
          color: Colors.yellow,
        ),
        Container(
          width: 160.0,
          color: Colors.orange,
        ),

      ],
    );
  }
}

3. 动态列表的几种写法

提示:个人比较倾向于第一种写法

class MyApp extends StatelessWidget{

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

    
    Widget build(BuildContext context) {
      return ListView.builder(
          itemCount: 20,
          itemExtent: 50.0, // 强制高度为50.0
          itemBuilder: (BuildContext context, int index) {
            return ListTile(title: Text("$index"));
          }
      );
    }
}


class MyApp extends StatelessWidget{

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

  
  Widget build(BuildContext context) {
    return ListView.separated(
        itemCount: 20,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(title: Text("$index"));
        },
        separatorBuilder: (BuildContext context, int index) {
          return index % 2 == 0 ? Divider(color: Colors.blue) : Divider(color: Colors.red);
        }
    );
  }
}

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

  // ignore: non_constant_identifier_names
  List<Widget> _ListData() {
    List<Widget> list = [];
    for (var i = 0; i < 20; i++) {
      list.add(ListTile(title: Text("我是列表$i")));
      // 添加分割线 奇数蓝色 偶数红色
      if (i % 2 == 0) {
        list.add(const Divider(color: Colors.blue));
      } else {
        list.add(const Divider(color: Colors.red));
      }
      // 最后一个不添加分割线
      if (i == 19) {
        list.removeLast();
      }
    }
    return list;
  }

  
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Column(
          children: _ListData(),
        )
      ],
    );
  }
}


总结

提示:这里对文章进行总结:

在 Flutter 中,Image 组件用于展示图片,Icon 组件用于展示图标,ListView 组件用于展示列表。Image 组件通过 image 、 width 、 height 、 fit 和 alignment 等属性来展示图片;Icon 组件通过 icon 、 color 和 size 等属性来展示图标;ListView 组件通过 children 、 scrollDirection 、 reverse 、 controller 、 itemExtent 、 padding 、 shrinkWrap 和 physics 等属性来展示列表。使用这些组件,开发者可以快速地构建出具有高质量的视觉效果、流畅的用户体验和响应式的布局的应用。同时,Flutter 采用了现代化的编程语言 Dart,并自带丰富的 UI 组件和工具库,为开发者提供了更加高效和有力的工具和支持。 有些功能还需要大家慢慢去发掘这里就不一一为大家介绍了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mylgcs

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

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

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

打赏作者

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

抵扣说明:

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

余额充值