zoukankan      html  css  js  c++  java
  • 12Flutter页面布局 AspectRatio和Cart卡片组件

    /*
    Flutter AspectRatio、Cart卡片组件:
    AspectRatio的作用是根据设置调整子元素child的宽高比。
    AspectRatio首先会在布局限制条件允许的范围内尽可能的扩展,widget的高度是由宽度和比率决定的。
    类似于BoxFit中的contain,按照固定比率去尽量占满区域。
    如果在满足所有限制条件过后无法找到一个可行的尺寸,AspectRatio最终将会去优先适应布局限制条件,而忽略所设置的比率。
    */
    main.dart
    import 'package:flutter/material.dart';
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text(' AspectRatio、Cart卡片组件'),
            ),
            body: HomeContent(),
          ),
        );
      }
    }
    
    class HomeContent extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        //Stack结合align实现布局:
        return AspectRatio(
          aspectRatio: 2.0/1.0,
          child: Container(
            color: Colors.red,
          ),
        );
      }
    }

    Cart卡片组件:

    demo1:

    main.dart

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text(' AspectRatio、Cart卡片组件'),
            ),
            body: HomeContent(),
          ),
        );
      }
    }
    
    class HomeContent extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        //Stack结合align实现布局:
        return ListView(
          children: <Widget>[
            Card(
              child: Column(
                children: <Widget>[
                  ListTile(title: Text('中国'), subtitle: Text('山东')),
                  ListTile(
                    title: Text('青岛'),
                  ),
                  ListTile(
                    title: Text('烟台'),
                  )
                ],
              ),
            ),
            Card(
              child: Column(
                children: <Widget>[
                  ListTile(title: Text('中国'), subtitle: Text('山东')),
                  ListTile(
                    title: Text('青岛'),
                  ),
                  ListTile(
                    title: Text('烟台'),
                  )
                ],
              ),
            )
          ],
        );
      }
    }

    demo2:

    main.dat

    import 'package:flutter/material.dart';

    void main() {
      runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text(' AspectRatio、Cart卡片组件'),
            ),
            body: HomeContent(),
          ),
        );
      }
    }

    class HomeContent extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        //Stack结合align实现布局:
        return ListView(
          children: <Widget>[
            Card(
              margin: EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
                  AspectRatio(
                    aspectRatio: 16 / 9,
                    child: Image.network(
                        'https://www.itying.com/images/flutter/1.png',fit: BoxFit.cover),
                  ),
                  ListTile(
                      leading: ClipOval(
                        child: Image.network('https://www.itying.com/images/flutter/1.png',fit: BoxFit.cover, 50,height: 50),
                      ),
                      title: Text('data'),
                      subtitle: Text('data'))
                ],
              ),
            ),
            Card(
              margin: EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
                  AspectRatio(
                    aspectRatio: 16 / 9,
                    child: Image.network(
                        'https://www.itying.com/images/flutter/1.png',fit: BoxFit.cover),
                  ),
                  ListTile(
                      leading: CircleAvatar(
                        backgroundImage: NetworkImage(
                            'https://www.itying.com/images/flutter/1.png'),
                      ),
                      title: Text('data'),
                      subtitle: Text('data'))
                ],
              ),
            )
          ],
        );
      }
    }

     demo3:

    main.dart

    import 'package:flutter/material.dart';
    import 'res/listData.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text(' AspectRatio、Cart卡片组件'),
            ),
            body: HomeContent(),
          ),
        );
      }
    }
    
    class HomeContent extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        //Stack结合align实现布局:
        return ListView(
          children: listData.map((value) {
            return Card(
              margin: EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
                  AspectRatio(
                    aspectRatio: 16 / 9,
                    child: Image.network(
                        "${value['imageUrl']}",
                        fit: BoxFit.cover),
                  ),
                  ListTile(
                      leading: CircleAvatar(
                        backgroundImage: NetworkImage(
                            "${value['imageUrl']}"),
                      ),
                      title: Text("${value['title']}"),
                      subtitle: Text("${value['descripts']}",maxLines:2,overflow: TextOverflow.ellipsis,))
                ],
              ),
            );
          }).toList(),
        );
      }
    }

    listData.dart

    List listData=[
      {
        "title":"Candy Shop",
        "author":"Mohamed Chahin",
        "imageUrl":"https://www.itying.com/images/flutter/1.png",
        "descripts":"MohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahin"
      },
      {
        "title":"Candy Shop",
        "author":"Mohamed Chahin",
        "imageUrl":"https://www.itying.com/images/flutter/2.png",
        "descripts":"MohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahin"
      
      },
      {
        "title":"Candy Shop",
        "author":"Mohamed Chahin",
        "imageUrl":"https://www.itying.com/images/flutter/3.png",
        "descripts":"MohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahin"
      
      },
      {
        "title":"Candy Shop",
        "author":"Mohamed Chahin",
        "imageUrl":"https://www.itying.com/images/flutter/4.png",
        "descripts":"MohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahin"
      
      },{
        "title":"Candy Shop",
        "author":"Mohamed Chahin",
        "imageUrl":"https://www.itying.com/images/flutter/5.png",
        "descripts":"MohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahinMohamedChahin"
      
      }
    ];
  • 相关阅读:
    centos6.8 搭建nginx+uwsgi+Flask
    mysql root密码
    mysql查询最近一小时的数据
    linux-ssh远程后台执行脚本-放置后台执行问题(转)
    (转,有改动)测试网页响应时间的shell脚本[需要curl支持]
    shell将标准错误输出重定向到 其他地方
    CentOS6 Shell脚本/bin/bash^M: bad interpreter错误解决方法
    linux文件分发脚本
    Lucas定理及应用
    [SHOI2015]超能粒子炮·改
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/11454423.html
Copyright © 2011-2022 走看看