Flutter基础widgets教程-SizedBox篇

举报
青年码农 发表于 2022/08/26 00:12:59 2022/08/26
1.1k+ 0 0
【摘要】 1 SizedBox 两种用法:一是可用来设置两个widget之间的间距,二是可以用来限制子组件的大小 2 构造函数 SizedBox({ Key key, this.width, this.height, Widget child }) 3 常用属性 3.1 width:Siz...

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

文章来源: blog.csdn.net,作者:NMGWAP,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/NMGWAP/article/details/125067322

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。