在Flutter中构建布局

成品效果

在这里插入图片描述

图片

在这里插入图片描述

源码

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget titleSection = new Container(
      padding: const EdgeInsets.all(32.0),
      child: new Row(
        children: [
          new Expanded(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                new Container(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: new Text(
                    'Oeschinen Lake Campground',
                    style: new TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                new Text(
                  'Kandersteg, Switzerland',
                  style: new TextStyle(
                    color: Colors.grey[500],
                  ),
                ),
              ],
            ),
          ),
          new Icon(
            Icons.star,
            color: Colors.red[500],
          ),
          new Text('41'),
        ],
      ),
    );
    Column buildButtonColumn(IconData icon, String label) {
      Color color = Theme.of(context).primaryColor;
      return new Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          new Icon(icon, color: color,),
          new Container(
            margin: const EdgeInsets.only(top: 8.0),
            child: new Text(
              label,
              style: new TextStyle(
                fontSize: 12.0,
                fontWeight: FontWeight.w400,
                color: color,
              ),
            ),
          )
        ],
      );
    }
    Widget buttonSection = new Container(
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          buildButtonColumn(Icons.call, 'Call'),
          buildButtonColumn(Icons.near_me, 'Route'),
          buildButtonColumn(Icons.share, 'Share'),
        ],
      ),
    );
    Widget textSection = new Container(
      padding: const EdgeInsets.all(32.0),
      child: new Text(
        '''
Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese Alps. Situated 1,578 meters above sea level, it is one of the larger Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour walk through pastures and pine forest, leads you to the lake, which warms to 20 degrees Celsius in the summer. Activities enjoyed here include rowing, and riding the summer toboggan run.
        ''',
        softWrap: true,
      ),
    );
    return new MaterialApp(
      title: 'Demo',
      home: Scaffold(
        body: new ListView(
          children: [
            new Image.asset(
              'images/lake.jpg',
              height: 240.0,
              fit: BoxFit.cover,
            ),
            titleSection,
            buttonSection,
            textSection,
          ],
        ),
      ),
      theme: new ThemeData(
        primaryColor: Colors.blue,
      ),
    );
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter可以使用`StaggeredGridView`来实现瀑布流布局。`StaggeredGridView`是一个可以根据内容动态调整大小的网格布局。下面是一个简单的示例代码: 首先,需要在`pubspec.yaml`文件添加`flutter_staggered_grid_view`依赖: ```yaml dependencies: flutter: sdk: flutter flutter_staggered_grid_view: ^0.4.0 ``` 然后,在Dart文件导入依赖: ```dart import 'package:flutter/material.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; ``` 接下来,可以使用`StaggeredGridView.countBuilder`构建瀑布流布局: ```dart class MyStaggeredGridView extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Staggered Grid View'), ), body: StaggeredGridView.countBuilder( crossAxisCount: 4, // 列数 itemCount: 20, // 子项数量 itemBuilder: (BuildContext context, int index) => Container( color: Colors.blueGrey, child: Center( child: CircleAvatar( backgroundColor: Colors.white, child: Text('$index'), ), ), ), staggeredTileBuilder: (int index) => StaggeredTile.count(2, index.isEven ? 2 : 1), // 控制子项的大小 mainAxisSpacing: 4.0, // 主轴间距 crossAxisSpacing: 4.0, // 横轴间距 ), ); } } ``` 在上面的示例,`StaggeredTile.count`用于控制子项的大小,通过判断`index`的奇偶性来实现交错布局。`mainAxisSpacing`和`crossAxisSpacing`用于设置主轴和横轴的间距。 这只是一个简单的示例,你可以根据自己的需求进行更多的定制和样式修改。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值