flutter学习之旅 - Wrap组件

Wrap组件

Wrap可以实现流布局,单行的WrapRow表现几乎一致,单列的WrapColumn都是单行单列的; Wrap则突破了这个限制,mainAxis上的空间不足时,则向crossAxis上去扩展

属性说明
direction主轴方向,默认水平
alignment主轴对齐方向
spacing主轴方向上的间距
verticalDirection定义了children摆放顺序,默认是down,见Fiex相关属性介绍
runAlignmentrun的对齐方式。run可以理解为新的行或者列,如果水平方向布局的话run可以理解为新的一行
runSpacingrun的间距
//自定义按钮
class _MyButton extends StatelessWidget {
  String text;
  void Function()? onPressed;
  _MyButton(this.text, {Key? key, required this.onPressed}) : super(key: key);
  
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onPressed,
      child: Text(text),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return Wrap(
      children: [
        _MyButton("第一集", onPressed: () {}),
        _MyButton("第二集", onPressed: () {}),
        _MyButton("第三集", onPressed: () {}),
        _MyButton("第四集", onPressed: () {}),
        _MyButton("第五集", onPressed: () {}),
        _MyButton("第六集", onPressed: () {}),
      ],
    );
  }
}

使用Wrap组件实现电商App搜索页面布局

import 'package:flutter/material.dart';

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

  
  Widget build(BuildContext context) {
    // final size = MediaQuery.of(context).size;
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color.fromRGBO(255, 255, 255, 1),
          title: const _SearchTitle(),
        ),
        body: ListView(
          children: [
            Row(
              children: const [Text("热搜")],
            ),
            const _ReSouWidget(),
            const SizedBox(height: 10),
            Row(
              children: const [Text("历史记录")],
            ),
            const SizedBox(height: 20),
            _LiShiJiLuWidget("刀剑"),
            const SizedBox(height: 20),
            _LiShiJiLuWidget("孤独摇滚"),
            const SizedBox(height: 40),
            Padding(
              padding: const EdgeInsets.all(40),
              child: OutlinedButton.icon(
                onPressed: () {},
                label: const Text("清除历史记录"),
                icon: const Icon(Icons.delete),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class _SearchTitle extends StatelessWidget {
  const _SearchTitle({Key? key}) : super(key: key);
  // const size = ;
  
  Widget build(BuildContext context) {
    return Row(
      children: const [
        Icon(
          Icons.arrow_back,
          color: Colors.black,
        ),
        SizedBox(
          width: 10,
        ),
        InputChip(
          label: Text("搜索"),
        )
      ],
    );
  }
}

//自定义按钮
class _MyButton extends StatelessWidget {
  String text;
  void Function()? onPressed;
  _MyButton(this.text, {Key? key, required this.onPressed}) : super(key: key);
  
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onPressed,
      child: Text(text),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 5,
      runSpacing: 5,
      children: [
        _MyButton("刀剑神域", onPressed: () {}),
        _MyButton("野良神", onPressed: () {}),
        _MyButton("碧蓝之海", onPressed: () {}),
        _MyButton("总之就是非常可爱", onPressed: () {}),
        _MyButton("盾之勇者成名录", onPressed: () {}),
      ],
    );
  }
}

class _LiShiJiLuWidget extends StatelessWidget {
  String text1;
  _LiShiJiLuWidget(this.text1, {Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 20,
      child: Row(
        children: [const Icon(Icons.search), Text(text1)],
      ),
    );
  }
}

效果图:

效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

结城明日奈是我老婆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值