【自学Flutter】10 Wrap布局和 Chip标签组件的使用

本文详细介绍并展示了在Flutter框架中使用Wrap布局和Chip标签组件的方法。通过具体代码实例,阐述了如何设置组件属性,如间距、对齐方式及内容等,实现美观且功能丰富的界面设计。

10 Wrap布局和Chip标签组件的使用

1.源代码
import 'package:flutter/material.dart';

void main () => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  GlobalKey globalKey = new GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          body: Center(
            child: Wrap(
              spacing: 20.0,
              runSpacing: 12.0,
              alignment: WrapAlignment.center,
              children: <Widget>[
                Chip(
                  label: Text("动漫"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("D"),
                  ),
                ),
                Chip(
                  label: Text("音乐"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("Y"),
                  ),
                ),
                Chip(
                  label: Text("影视"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("Y"),
                  ),
                ),
                Chip(
                  label: Text("纪录"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("J"),
                  ),
                ),
                Chip(
                  label: Text("课程"),
                  deleteIcon: Icon(Icons.cancel),
                  deleteIconColor: Colors.red,
                  deleteButtonTooltipMessage: "长按删除按钮显示这个信息",
                  onDeleted: (){},
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("K"),
                  ),
                )
              ],
            ),
          )
        )
    );
  }
}

2.解释源代码
import 'package:flutter/material.dart';

void main () => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  GlobalKey globalKey = new GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          body: Center(
            //Wrap布局
            child: Wrap(
              //水平方向间距
              spacing: 20.0,
              //垂直方向间距
              runSpacing: 12.0,
              //与主轴(水平方向)的对齐方式
              alignment: WrapAlignment.center,
              children: <Widget>[
                //Chip标签组件
                Chip(
                  //标签文本
                  label: Text("动漫"),
                  //左侧内容(一般为小图标)
                  avatar: CircleAvatar(
                    //左侧内容背景颜色
                    backgroundColor: Colors.green,
                    //内容文本
                    child: Text("D"),
                  ),
                ),
                Chip(
                  label: Text("音乐"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("Y"),
                  ),
                ),
                Chip(
                  label: Text("影视"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("Y"),
                  ),
                ),
                Chip(
                  label: Text("纪录"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("J"),
                  ),
                ),
                Chip(
                  label: Text("课程"),
                  //删除图标
                  deleteIcon: Icon(Icons.cancel),
                  //删除图标背景颜色
                  deleteIconColor: Colors.red,
                  //删除按钮提示信息
                  deleteButtonTooltipMessage: "长按删除按钮显示这个信息",
                  //删除回调
                  onDeleted: (){},
                  avatar: CircleAvatar(
                    backgroundColor: Colors.green,
                    child: Text("K"),
                  ),
                )
              ],
            ),
          )
        )
    );
  }
}

3.效果图

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值