Flutter进阶—通用布局控件

119 篇文章 9 订阅
82 篇文章 1417 订阅

Flutter有一个丰富的布局控件库,但我们只学习最常用的一些,目的是使你可以尽快开始开发,而不是按步就班的学习完整的课程。

控件分为两类:控件库(Widgets Library)中的标准控件和质感设计库(Material Library)中的专用控件。任何应用程序都可以使用控件库(Widgets Library),但只有质感设计应用程序可以使用质感设计库(Material Library)。

标准控件

Container(容器)

许多布局可以自由地使用容器来分隔带有填充的控件,或添加边框、边距。您可以通过将整个布局放入容器并更改其背景颜色或图像来更改设备的背景。

下面的布局由2行2列组成,每列包含2个图像(pic1.jpgpic2.jpgpic3.jpgpic4.jpg)。每个图像使用容器来添加一个圆形的灰色边框和边距,包含图像行的列使用容器将背景颜色更改为较浅的灰色。

这里写图片描述

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
  //...
  var container = new Container(
        decoration: new BoxDecoration(
            backgroundColor: Colors.black26,
        ),
        child: new Column(
            children: [
              new Row(
                  children: [
                    new Expanded(
                        child: new Container(
                            decoration: new BoxDecoration(
                                border: new Border.all(width: 10.0, color: Colors.black38),
                                borderRadius: const BorderRadius.all(const Radius.circular(8.0))
                            ),
                            margin: const EdgeInsets.all(4.0),
                            child: new Image.asset('images/pic1.jpg'),
                        )
                    ),
                    new Expanded(
                        child: new Container(
                            decoration: new BoxDecoration(
                                border: new Border.all(width: 10.0, color: Colors.black38),
                                borderRadius: const BorderRadius.all(const Radius.circular(8.0))
                            ),
                            margin: const EdgeInsets.all(4.0),
                            child: new Image.asset('images/pic2.jpg'),
                            )
                    ),
                  ]
              ),
              new Row(
                  children: [
                    new Expanded(
                        child: new Container(
                            decoration: new BoxDecoration(
                                border: new Border.all(width: 10.0, color: Colors.black38),
                                borderRadius: const BorderRadius.all(const Radius.circular(8.0))
                            ),
                            margin: const EdgeInsets.all(4.0),
                            child: new Image.asset('images/pic3.jpg'),
                            )
                    ),
                    new Expanded(
                        child: new Container(
                            decoration: new BoxDecoration(
                                border: new Border.all(width: 10.0, color: Colors.black38),
                                borderRadius: const BorderRadius.all(const Radius.circular(8.0))
                            ),
                            margin: const EdgeInsets.all(4.0),
                            child: new Image.asset('images/pic4.jpg'),
                            )
                    ),
                  ]
              ),
            ]
        )
    );
  //...
  }
}

GridView(网格视图)

使用GridView将控件作为二维列表放置。 GridView提供了两个预制列表,或者您可以构建自己的自定义网格。当GridView检测到其内容太长,无法适应渲染框时,它会自动滚动。

下面的布局使用GridView.extent创建一个最大150像素宽的图块,图像保存名称为pic1.jpg、pic2.jpg … pic30.jpg,List.generate构造函数允许创建一个简单的方法。

这里写图片描述

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //...
    List<Container> _buildGridTileList(int count) {
      return new List<Container>.generate(
          count,
          (int index) => new Container(child: new Image.asset('images/pic${index+1}.jpg'))
      );
    }
    Widget buildGrid() {
      return new GridView.extent(
          maxCrossAxisExtent: 150.0,
          padding: const EdgeInsets.all(4.0),
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          children: _buildGridTileList(20)
      );
    }
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: buildGrid(),
      ),
    );
    //...
  }
}

ListView(列表视图)

ListView是一个类列表的控件,当其内容对于其渲染框太长时,它会自动提供滚动。

下面的布局使用ListTiles的业务列表,用分隔线将剧院与餐厅分开。

这里写图片描述

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //...
    List<Widget> list = <Widget>[
      new ListTile(
          title: new Text('深圳万达海雅广场店', style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
          subtitle: new Text('深圳市宝安区建安一路海雅缤纷城4楼'),
          leading: new Icon(
              Icons.theaters,
              color: Colors.blue[500],
          )
      ),
      //...
      new Divider(),
      new ListTile(
          title: new Text('同仁四季音乐主题餐厅(海岸城店)', style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
          subtitle: new Text('海岸城保利文化广场2楼保利来觅C6(近地铁后海站E出口)'),
          leading: new Icon(
              Icons.restaurant,
              color: Colors.blue[500],
              )
      ),
      //...
    ];
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new ListView(
            children: list,
        )
      ),
    );
    //...
  }
}

Stack(层叠)

使用Stack在基本控件顶部排列控件,通常像图像,控件可以完全或部分地重叠基本控件。

下面的布局使用Stack覆盖一个容器(在半透明黑色背景上显示其文本)在圆形头像顶部。Stack使用alignment属性和FractionalOffsets来偏移文本。

这里写图片描述

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //...
    var stack = new Stack(
        alignment: const FractionalOffset(0.8, 0.8),
        children: [
          new CircleAvatar(
              backgroundImage: new AssetImage('images/pic4.jpg'),
              radius: 100.0,
          ),
          new Container(
              decoration: new BoxDecoration(
                  backgroundColor: Colors.black45,
              ),
              child: new Text(
                  'Hekaiyou',
                  style: new TextStyle(
                      fontSize: 20.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                  )
              )
          )
        ]
    );
    //...
  }
}

质感设计控件

Card(卡片)

Card包含相关的信息块,可以由大多数任何控件组成,但经常与ListTile一起使用。Card只有一个子控件,但它的子控件可以是支持多个子控件的列、行、列表、网格或其他控件。默认情况下,Card将其大小缩小为0像素。您可以使用SizedBox限制Card的大小。

在Flutter中,Card具有略微圆角和阴影,给予3D效果。更改Card的elevation属性可以控制阴影效果。例如,将elevation设置为24,可以将Card从表面进一步提升,并使阴影变得更加分散。

下面的布局是一张包含3个ListTile的卡片,并用SizedBox包装大小。分隔符分隔第一个和第二个ListTile。

这里写图片描述

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //...
    var card = new SizedBox(
        height: 210.0,
        child: new Card(
            child: new Column(
                children: [
                  new ListTile(
                      title: new Text('北京市复兴路10号', style: new TextStyle(fontWeight: FontWeight.w500)),
                      subtitle: new Text('北京市,100844'),
                      leading: new Icon(
                          Icons.business,
                          color: Colors.blue[500],
                      )
                  ),
                  new Divider(),
                  new ListTile(
                      title: new Text('12306', style: new TextStyle(fontWeight: FontWeight.w500)),
                      leading: new Icon(
                          Icons.contact_phone,
                          color: Colors.blue[500],
                          )
                  ),
                  new ListTile(
                      title: new Text('12306gw@china-mor.gov.cn', style: new TextStyle(fontWeight: FontWeight.w500)),
                      leading: new Icon(
                          Icons.contact_mail,
                          color: Colors.blue[500],
                          )
                  ),
                ]
            )
        )
    );
    //...
  }
}

ListTile(列表平铺)

ListTile是质感设计库中专门的行控件,可以轻松创建一行,其中最多包含3行文本和可选的前导和尾随图标。 ListTile最常用于Card或ListView,还可以在别的地方使用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

何小有

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

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

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

打赏作者

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

抵扣说明:

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

余额充值