首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Flutter入门(二)

Flutter入门(二)

作者头像
用户3112896
发布于 2019-12-27 10:41:53
发布于 2019-12-27 10:41:53
62500
代码可运行
举报
文章被收录于专栏:安卓圈安卓圈
运行总次数:0
代码可运行

* 网格布局

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  List<Widget> _getListData() {
    var tempList = listData.map((value) {
      return Container(
        child: Column(
          children: <Widget>[
            Image.network(value['imageUrl']),
            SizedBox(
              height: 10,
            ),
            Text(
              value['title'],
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 20),
            ),
          ],
        ),
        decoration: BoxDecoration(
            border: Border.all(
          color: Colors.yellow,
          width: 1,
        )),
      );
    });
    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GridView.count(
      crossAxisSpacing: 20.0, //水平子Widget之间间距
      mainAxisSpacing: 20.0, //垂直子Widget之间间距
      padding: EdgeInsets.all(10),
      crossAxisCount: 2, //一行的Widget数量
      // childAspectRatio: 0.7, //宽度和高度的比例
      children: this._getListData(),
    );
  }
}

效果图

优化

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  Widget _getListData(context, index) {
    return Container(
      child: Column(
        children: <Widget>[
          Image.network(listData[index]['imageUrl']),
          SizedBox(
            height: 10,
          ),
          Text(
            listData[index]['title'],
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 20),
          ),
        ],
      ),
      decoration: BoxDecoration(
          border: Border.all(
        color: Colors.yellow,
        width: 1,
      )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisSpacing: 10.0, //水平子Widget之间间距
        mainAxisSpacing: 10.0, //垂直子Widget之间间距
        crossAxisCount: 2, //一行的Widget数量
      ),
      itemCount: listData.length,
      itemBuilder: this._getListData,
    );
  }
}

* Padding(个人感觉和Container很像)(下面的代码很难看,重要的是思路)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
        child: GridView.count(
          crossAxisCount: 2,
          childAspectRatio: 1.7,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/1.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/2.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/3.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/4.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/5.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/6.png',
                fit: BoxFit.cover,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Image.network(
                'https://www.itying.com/images/flutter/7.png',
                fit: BoxFit.cover,
              ),
            ),
          ],
        ));
  }
}

效果图

* Row横向排序(Column同理)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 400.0,
      width: 300.0,
      color: Colors.yellow,
      padding: EdgeInsets.all(10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          IconContainer(Icons.home, color: Colors.blue),
          IconContainer(Icons.search, color: Colors.red),
          IconContainer(Icons.select_all, color: Colors.orange)
        ],
      ),
    );
  }
}

class IconContainer extends StatelessWidget {
  double size;
  Color color;
  IconData icon;
  IconContainer(this.icon, {this.color = Colors.red, this.size = 30.0});
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80.0,
      width: 80.0,
      color: this.color,
      child: Center(
        child: Icon(
          this.icon,
          size: this.size,
          color: Colors.white,
        ),
      ),
    );
  }
}

效果图

* 复杂布局

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      Row(
        children: <Widget>[
          Expanded(
              child: Container(
            height: 200,
            color: Colors.black,
            child: Text('你好Flutter'),
          ))
        ],
      ),
      SizedBox(height: 10),
      Row(children: <Widget>[
        Expanded(
            flex: 2,
            child: Container(
                height: 180,
                child: Image.network(
                    'https://www.itying.com/images/flutter/1.png',
                    fit: BoxFit.cover))),
        SizedBox(width: 10),
        Expanded(
            flex: 1,
            child: Container(
                height: 180,
                child: ListView(
                  children: <Widget>[
                    Container(
                        height: 85,
                        child: Image.network(
                            'https://www.itying.com/images/flutter/2.png',
                            fit: BoxFit.cover)),
                    SizedBox(height: 10),
                    Container(
                        height: 85,
                        child: Image.network(
                            'https://www.itying.com/images/flutter/3.png',
                            fit: BoxFit.cover)),
                  ],
                )))
      ])
    ]);
  }
}

效果图

* Stack+Align

Stack(类似android的relativelayout)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        height: 400,
        width: 300,
        color: Colors.red,
        child: Stack(
          children: <Widget>[
            Align(
              alignment: Alignment.topLeft,
              child: Icon(Icons.home, size: 40, color: Colors.white),
            ),
            Align(
              alignment: Alignment.center,
              child: Icon(Icons.search, size: 30, color: Colors.white),
            ),
            Align(
              alignment: Alignment.bottomRight,
              child: Icon(Icons.settings_applications,
                  size: 60, color: Colors.white),
            ),
          ],
        ),
      ),
    );
  }
}

效果图

* Stack+Positioned

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        height: 400,
        width: 300,
        color: Colors.red,
        child: Stack(
          children: <Widget>[
            Positioned(
              left: 10, //距离左边10
              child: Icon(Icons.home, size: 40, color: Colors.white),
            ),
            Positioned(
              bottom: 0, //距离左边0
              left: 100, //距离左边100
              child: Icon(Icons.search, size: 30, color: Colors.white),
            ),
            Positioned(
              right: 0, //距离右边0
              child: Icon(Icons.settings_applications,
                  size: 60, color: Colors.white),
            ),
          ],
        ),
      ),
    );
  }
}

效果图

* AspectRatio宽高比组件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 3.0 / 1.0, //宽高比
      child: Container(
        color: Colors.red,
      ),
    );
  }
}

效果图

* 卡片

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        Card(
          margin: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              ListTile(
                title: Text(
                  '张三',
                  style: TextStyle(fontSize: 28),
                ),
                subtitle: Text('高级工程师'),
              ),
              ListTile(
                title: Text("电话:xxxxx"),
              ),
              ListTile(
                title: Text("地址:xxx"),
              )
            ],
          ),
        ),
        Card(
          margin: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              ListTile(
                title: Text(
                  '王五',
                  style: TextStyle(fontSize: 28),
                ),
                subtitle: Text('高级工程师'),
              ),
              ListTile(
                title: Text("电话:xxxxx"),
              ),
              ListTile(
                title: Text("地址:xxx"),
              )
            ],
          ),
        ),
      ],
    );
  }
}

效果图

* 图文卡片

listData.dart

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
List listData = [
  {
    "title": 'zhangsan',
    "author": 'alibaba',
    "imageUrl": 'https://www.itying.com/images/flutter/1.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'lisi',
    "author": 'huawei',
    "imageUrl": 'https://www.itying.com/images/flutter/2.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'wangwu',
    "author": 'wangyi',
    "imageUrl": 'https://www.itying.com/images/flutter/3.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'zhaoliu',
    "author": 'jinritoutiao',
    "imageUrl": 'https://www.itying.com/images/flutter/4.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'qixi',
    "author": 'dajiang',
    "imageUrl": 'https://www.itying.com/images/flutter/5.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'batiao',
    "author": 'tengxun',
    "imageUrl": 'https://www.itying.com/images/flutter/6.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
  {
    "title": 'jiubing',
    "author": 'weixin',
    "imageUrl": 'https://www.itying.com/images/flutter/7.png',
    "discription": 'flutter is google‘s mobile UI framework',
  },
];
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Flutter Demo')),
        body: HomeContent(),
      ),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: listData.map((value) {
        return Card(
          margin: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              AspectRatio(
                aspectRatio: 20 / 9,
                child: Image.network(value["imageUrl"], fit: BoxFit.cover),
              ),
              ListTile(
                leading: CircleAvatar(
                    //头像组件
                    backgroundImage: NetworkImage(value["imageUrl"])),
                title: Text(value["title"]),
                subtitle: Text(
                  value["discription"],
                  overflow: TextOverflow.ellipsis,
                ),
              )
            ],
          ),
        );
      }).toList(),
    );
  }

效果图很好看

* Wrap 流布局

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 10,
      runSpacing: 10,
      // direction: Axis.vertical,//竖直排列
      alignment: WrapAlignment.start,
      children: <Widget>[
        MyButton("斗罗大陆"),
        MyButton("遮天"),
        MyButton("盗墓笔记"),
        MyButton("天龙八部"),
        MyButton("凡人修仙传"),
        MyButton("大主宰"),
        MyButton("仙逆"),
        MyButton("斗鱼"),
        MyButton("校花的贴身高手"),
        MyButton("酒神"),
        MyButton("最好的我们"),
      ],
    );
  }
}

class MyButton extends StatelessWidget {
  final String text;
  MyButton(this.text);
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text(this.text),
      textColor: Theme.of(context).accentColor,
      onPressed: () {},
    );
  }
}

效果图

欢迎关注我的微信公众号:安卓圈

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 安卓圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Centos7.6搭建LNMP环境
将图中的user = xxx和group = xxx改为user = nginx group = nginx
Dreamy.TZK
2020/04/09
1.2K0
CentOS的lnmp环境配置
yum install php php-fpm service php-fpm start
治电小白菜
2020/08/25
9380
CentOS的lnmp环境配置
CentOS 7 安装 LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)
Webtatic:https://webtatic.com MySQL:https://dev.mysql.com/downloa…
全栈程序员站长
2022/07/07
3470
CentOS 7 安装 LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)
CentOS 7 搭建 LNMP 环境
LNMP 环境是指在 Linux 系统下,由 Nginx + MySQL/MariaDB + PHP 组成的网站服务器架构。本文档介绍如何在腾讯云云服务器(CVM)上手动搭建 LNMP 环境。
Lemon黄
2020/04/01
1.5K0
CentOS 7 搭建 LNMP 环境
搭建 LNMP 环境
LNMP是建立web应用的平台,是Linux、NGINX,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的简称。
剧终
2020/08/27
1.3K0
搭建 LNMP 环境
CentOS 7源码安装最新版LNMP环境
首先做一些准备工作,先把centos7的防火墙更换成iptables,可以参见如下链接
botkenni
2022/01/21
5780
手把手教你,嘴对嘴传达------源码编译LNMP部署及应用 , 手动搭建discuz论坛
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python
不吃小白菜
2020/09/03
9900
手把手教你,嘴对嘴传达------源码编译LNMP部署及应用   , 手动搭建discuz论坛
运维之LNMP环境安装与配置
描述:1)LNMP是目前互联网主流的WEB服务器架构,主要是由Linux+Nginx+MYSQL|Mariadb+PHP组合,可以发布企业门户网站代码(PHP动态网页),运维人员要能够独立构建LNMP平台。
全栈工程师修炼指南
2022/09/28
9300
运维之LNMP环境安装与配置
CentOS 7配置LNMP开发环境及配置文件管理详解
本篇文章主要介绍了CentOS 7配置LNMP开发环境及配置文件管理,详细的介绍了MySQL 5.6,PHP 5.6,Nginx的安装与配置,有兴趣的可以了解一下。
习惯说一说
2019/07/08
5670
CentOS7快速安装搭建LNMP环境
源码编译安装 LNMP 环境虽然便于自定义,但是对于小型服务器来说,漫长的编译时间让人无法等待。如果能在 10 分钟内搞定环境安装,那就很好了。
星哥玩云
2022/07/24
1.6K0
CentOS7快速安装搭建LNMP环境
源码编译安装 LNMP 环境虽然便于自定义,但是对于小型服务器来说,漫长的编译时间让人无法等待。如果能在 10 分钟内搞定环境安装,那就很好了。
会长君
2023/04/26
1.2K0
Linux搭建开源企业邮箱系统EwoMail
EwoMail是基于Linux的开源邮件服务器软件,集成了众多优秀稳定的组件,是一个快速部署、简单高效、多语言、安全稳定的邮件解决方案,帮助你提升运维效率,降低 IT 成本,兼容主流的邮件客户端,同时支持电脑和手机邮件客户端。
星哥玩云
2022/09/15
7.5K0
Linux搭建开源企业邮箱系统EwoMail
CentOS 8.1下搭建LAMP(Linux+Apache+MySQL+PHP)环境
LAMP是Linux,Apache,MySQL和PHP的首字母缩写,是网站管理员和开发人员用来测试和托管动态网站的一种流行的免费开源堆栈。
星哥玩云
2022/08/13
2.4K0
CentOS 8.1下搭建LAMP(Linux+Apache+MySQL+PHP)环境
Linux学习13-CentOS安装mysql5.6环境
安装完成后重启mysql服务,查看状态是 Active: active (running) ,说明启动成功
上海-悠悠
2019/05/06
1.1K0
Linux学习13-CentOS安装mysql5.6环境
Linux搭建开源企业邮箱系统EwoMail
EwoMail是基于Linux的开源邮件服务器软件,集成了众多优秀稳定的组件,是一个快速部署、简单高效、多语言、安全稳定的邮件解决方案,帮助你提升运维效率,降低 IT 成本,兼容主流的邮件客户端,同时支持电脑和手机邮件客户端。
星哥玩云
2022/06/09
4.7K0
Linux搭建开源企业邮箱系统EwoMail
LNMP环境搭建
Nginx的PHP安装和LAMP环境搭建中的PHP安装是有区别的。因为Nginx中的PHP是以fastcgi的方式结合Nginx的,而httpd是把PHP作为自己的模块来调用的。
刘銮奕
2019/07/22
2.4K0
CentOS7编译安装L(A|N)MP环境
跟多详细配置参考:https://www.zybuluo.com/phper/note/89081
小柒吃地瓜
2020/04/23
1.8K0
Linux系统服务神器:systemctl的简单配置与使用
以前使用Ubuntu和CentOS,一般使用SysV init(就是以前使用的service)进行进程的开机自启和进程守护。
Mintimate
2022/04/08
7.6K1
Linux系统服务神器:systemctl的简单配置与使用
Linux基础(day48)
12.1 LNMP架构介绍 LNMP架构介绍目录概要 和LAMP不同的是,提供web服务的是Nginx 并且php是作为一个独立服务存在的,这个服务叫做php-fpm Nginx直接处理静态请求,动态
运维小白
2018/02/06
8340
Linux基础(day48)
40张步骤截图教你用腾讯云服务器配置LNMP环境并安装wordpress
作者:liuxinig 来源: http://www.cnblogs.com/liuxinig/p/5928361.html 本文有点长,原因是每一步都有截图占的篇幅较长,其实并不麻烦,并不复杂。所有
小小科
2018/05/02
4.5K4
40张步骤截图教你用腾讯云服务器配置LNMP环境并安装wordpress
推荐阅读
相关推荐
Centos7.6搭建LNMP环境
更多 >
交个朋友
加入HAI高性能应用服务器交流群
探索HAI应用新境界 共享实践心得
加入架构与运维学习入门群
系统架构设计入门 运维体系构建指南
加入架构与运维工作实战群
高并发系统设计 运维自动化实践
换一批
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档