Flutter基础widgets教程-SizedBox篇

1 SizedBox

两种用法:一是可用来设置两个widget之间的间距,二是可以用来限制子组件的大小

2 构造函数

SizedBox({ 
    Key key, 
    this.width, 
    this.height, 
    Widget child 
})

3 常用属性

3.1 width:SizedBox的宽

width: 250,

3.2 height:SizedBox的高

height: 250,

3.3 child:SizedBox的子widget

child: Text("内容"),

4.显示效果

3798b875e3b89178250a3be7c471dc79.png

5.代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
// 字体适配
import '../../utils/app_size.dart';

class ListSizedBox extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          title: Text('SizedBox'),
          backgroundColor: Color(0xFFfafcff),
          bottom: TabBar(labelColor: Color(0xff030303), tabs: [
            Tab(
              text: "效果",
            ),
            Tab(
              text: "属性",
            )
          ]),
        ),
        body: TabBarView(children: [
          Container(
              decoration: new BoxDecoration(
                color: new Color(0xffffffff),
                borderRadius: new BorderRadius.circular((AppSize.width(20))),
              ),
              child: ShowEffect()),
          Container(
              decoration: new BoxDecoration(
                color: new Color(0xffffffff),
                borderRadius: new BorderRadius.circular((AppSize.width(20))),
              ),
              child: ShowAttribute()),
        ]),
      ),
    );
  }
}

// 效果
class ShowEffect extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(children: <Widget>[
      SizedBox(
        width: 150.0,
        height: 150.0,
        child: Container(
          margin: EdgeInsets.all(20.0),
          width: 200.0,
          height: 200.0,
          color: Colors.blue,
        ),
      ),
      SizedBox(
        width: 100.0,
        height: 100.0,
        child: Container(
          margin: EdgeInsets.all(20.0),
          width: 200.0,
          height: 200.0,
          color: Colors.yellow,
        ),
      ),
      SizedBox(
        width: 100.0,
        height: 100.0,
        child: Container(
          margin: EdgeInsets.all(20.0),
          width: 200.0,
          height: 200.0,
          color: Colors.red,
        ),
      )
    ]);
  }
}

// 属性
class ShowAttribute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: rootBundle.loadString('lib/markdown/sizedBox.md'),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return Markdown(
              data: snapshot.data,
              selectable: true,
            );
          } else {
            return Center(
              child: Text("加载中..."),
            );
          }
        },
      ),
    );
  }
}

此代码已共享到码云,已对60多种widget做演示及解释。一直更新中

https://gitee.com/nmgwap/flutter_app

posted @   青年码农  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示