Flutter基础widgets教程-SizedOverflowBox篇

举报
青年码农 发表于 2022/08/25 00:06:19 2022/08/25
【摘要】 1 SizedOverflowBox SizedOverflowBox主要的布局行为有两点:1 尺寸部分。通过将自身的固定尺寸,传递给child,来达到控制child尺寸的目的;undefined 2 超出部分。可以突破父节点尺寸的限制,超出部分也可以被渲染显示,与OverflowBox类似。 2 构造函数 S...

1 SizedOverflowBox

SizedOverflowBox主要的布局行为有两点:1 尺寸部分。通过将自身的固定尺寸,传递给child,来达到控制child尺寸的目的;undefined

2 超出部分。可以突破父节点尺寸的限制,超出部分也可以被渲染显示,与OverflowBox类似。

2 构造函数


   
  1. SizedOverflowBox({
  2. Key key,
  3. @required this.size,
  4. this.alignment = Alignment.center,
  5. Widget child,
  6. })

3 常用属性

3.1 size:固定的尺寸

size: Size(100.0, 200.0),
  

3.2 alignment:对齐方式

alignment:Alignment.topLeft,
  

3.2.1 顶部左边

alignment:Alignment.topLeft,
  

3.2.2 顶部中间

alignment:Alignment.topCenter,
  

3.2.3 顶部右边

alignment:Alignment.topRight,
  

3.2.4 中部左边

alignment:Alignment.centerLeft,
  

3.2.5 中部中间

alignment:Alignment.center,
  

3.2.6 中部右边

alignment:Alignment.centerRight,
  

3.2.7 底部左边

alignment:Alignment.bottomLeft,
  

3.2.8 底部中间

alignment:Alignment.bottomCenter,
  

3.2.9 底部右边

alignment:Alignment.bottomRight,
  

3.3 child:子控件

child: Text("内容"),
  

4.显示效果

964d08d5ee6a7c662c7b611bf4e92406.png

5.代码


   
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_markdown/flutter_markdown.dart';
  4. // 字体适配
  5. import '../../utils/app_size.dart';
  6. class ListSizedOverflowBox extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return DefaultTabController(
  10. length: 2,
  11. child: Scaffold(
  12. appBar: AppBar(
  13. title: Text('SizedOverflowBox'),
  14. backgroundColor: Color(0xFFfafcff),
  15. bottom: TabBar(labelColor: Color(0xff030303), tabs: [
  16. Tab(
  17. text: "效果",
  18. ),
  19. Tab(
  20. text: "属性",
  21. )
  22. ]),
  23. ),
  24. body: TabBarView(children: [
  25. Container(
  26. decoration: new BoxDecoration(
  27. color: new Color(0xffffffff),
  28. borderRadius: new BorderRadius.circular((AppSize.width(20))),
  29. ),
  30. child: ShowEffect()),
  31. Container(
  32. decoration: new BoxDecoration(
  33. color: new Color(0xffffffff),
  34. borderRadius: new BorderRadius.circular((AppSize.width(20))),
  35. ),
  36. child: ShowAttribute()),
  37. ]),
  38. ),
  39. );
  40. }
  41. }
  42. // 效果
  43. class ShowEffect extends StatelessWidget {
  44. @override
  45. Widget build(BuildContext context) {
  46. return MaterialApp(
  47. home: Scaffold(
  48. appBar: AppBar(
  49. title: Text("SizedOverflowBox"),
  50. ),
  51. body: Container(
  52. color: Colors.green,
  53. alignment: Alignment.topRight,
  54. width: 200.0,
  55. height: 200.0,
  56. padding: EdgeInsets.all(5.0),
  57. child: SizedOverflowBox(
  58. size: Size(100.0, 200.0),
  59. child: Container(
  60. color: Colors.red,
  61. width: 200.0,
  62. height: 100.0,
  63. ),
  64. ),
  65. ),
  66. ),
  67. );
  68. }
  69. }
  70. // 属性
  71. class ShowAttribute extends StatelessWidget {
  72. @override
  73. Widget build(BuildContext context) {
  74. return Container(
  75. child: FutureBuilder(
  76. future: rootBundle.loadString('lib/markdown/sizedOverflowBox.md'),
  77. builder: (BuildContext context, AsyncSnapshot snapshot) {
  78. if (snapshot.hasData) {
  79. return Markdown(
  80. data: snapshot.data,
  81. selectable: true,
  82. );
  83. } else {
  84. return Center(
  85. child: Text("加载中..."),
  86. );
  87. }
  88. },
  89. ),
  90. );
  91. }
  92. }

此代码已共享到码云,已对60多种widget做演示及解释。一直更新中https://gitee.com/nmgwap/flutter_app

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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