栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Flutter——最详细的Container(容器)使用教程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter——最详细的Container(容器)使用教程

Container(容器)
属性作用
child配置容器内部的布局
padding设置容器内部的距离
alignment设置容器内容的对齐方式
color设置容器背景颜色
margin设置容器外部的距离
decoration配置容器的外部的样式
constraints配置容器的外部的约束,最大小宽、最大小高度
width设置容器的宽
height设置容器的高
  • 创建一个指定宽高的容器
import 'package:flutter/material.dart';
class CustomContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.topLeft,
      width: 200,
      height: 200,
      color: Colors.red,
    );
  }
}

  • 容器里面放一个子组件
import 'package:flutter/material.dart';
class ContainerWithChild extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.topLeft,
      padding: EdgeInsets.all(20),
      margin: EdgeInsets.all(10),
      width: 200,
      height: 200,
      color: Colors.grey,
      child: Icon(Icons.android),
    );
  }
}

  • 子组件容器底部对齐
import 'package:flutter/material.dart';
class ContainerAlignment extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomRight,
      width: 200,
      height: 200,
      color: Colors.grey,
      child: Icon(
        Icons.android,
        color: Colors.green,
      ),
    );
  }
}

  • 添加一个装饰容器的布局
    需要注意的是,当装饰器里面设置了Color背景颜色,则Container的Color属性不能使用,二者只取其一
import 'package:flutter/material.dart';
class ContainerDecoration extends StatelessWidget {

  final List rainbow = [
    0xffff0000,
    0xffFF7F00,
    0xffFFFF00,
    0xff00FF00,
    0xff00FFFF,
    0xff0000FF,
    0xff8B00FF
  ];
 final List stops = [0.0, 1 / 6, 2 / 6, 3 / 6, 4 / 6, 5 / 6, 1.0];
  @override
  Widget build(BuildContext context) {

    return Container(//容器
      alignment: Alignment.center,
      width: 200,
      height: 200,
      margin: EdgeInsets.all(20),
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(//添加渐变色
          gradient: LinearGradient(
              stops: stops,
              colors: rainbow.map((e) => Color(e)).toList()),
          borderRadius: BorderRadius.only(//添加圆角
              topLeft: Radius.circular(50),
              bottomRight: Radius.circular(50)),
          boxShadow: [
            BoxShadow(
                color: Colors.grey,
                offset: Offset(1, 1),
                blurRadius: 10,
                spreadRadius: 1),
          ]),
      child: Text(
        "Container",
        style: TextStyle(fontSize: 20),
      ),
    );
  }
}

  • 约束宽高的布局
import 'package:flutter/material.dart';
class ContainerConstraints extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      //容器
      color: Colors.blue,
      width: 200,
      height: 200,
      constraints: BoxConstraints(
          minWidth: 100, maxWidth: 150,
          minHeight: 20, maxHeight: 100),
    );
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/603794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号