Flutter基础widgets教程-Transform篇

举报
青年码农 发表于 2022/08/24 22:23:22 2022/08/24
【摘要】 1 Transform 在绘制子widget之前应用转换的widget 2 构造函数 Transform({ Key key, @required this.transform, this.origin, this.alignment, this.transformHitTests =...

1 Transform

在绘制子widget之前应用转换的widget

2 构造函数


   
  1. Transform({
  2. Key key,
  3. @required this.transform,
  4. this.origin,
  5. this.alignment,
  6. this.transformHitTests = true,
  7. Widget child,
  8. })

3 常用属性

3.1 origin:指定子组件做平移、旋转、缩放时的中心点

origin: Offset(50, 50),
  

3.2 alignment:对齐方式

alignment:Alignment.center,
  

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 transformHitTests:点击区域是否也做相应的变换,为true时执行相应的变换,为false不执行

transformHitTests:true,
  

3.4 transform:控制子组件的平移、旋转、缩放、倾斜变换

transform: Matrix4.rotationX(radian),
  

3.4.1 旋转


   
  1. transform: Matrix4.rotationX(radian),
  2. transform: Matrix4.rotationY(radian),
  3. transform: Matrix4.rotationZ(radian),

3.4.2 平移


   
  1. transform:Matrix4.translation(Vector3(x, y, z)),
  2. transform:Matrix4.translation(Vector3.all(val)),
  3. transform:Matrix4.translationValues(x, y, z),

3.4.3 缩放


   
  1. transform:Matrix4.diagonal3(Vector3(x, y, z)),
  2. transform:Matrix4.diagonal3(Vector3.all(val)),
  3. transform:Matrix4.diagonal3Values(x, y, z),

3.4.4 倾斜


   
  1. transform:Matrix4.skewX(alpha),
  2. transform:Matrix4.skewY(double beta),
  3. transform:Matrix4.skew(alpha, beta),

3.5 child:子widget

child: Text('你好 Flutter'),
  

4.显示效果

5a2f1190f4836e2a74adfa253f251d97.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 ListTransform extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return DefaultTabController(
  10. length: 2,
  11. child: Scaffold(
  12. appBar: AppBar(
  13. title: Text('Transform'),
  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("Transform"),
  50. ),
  51. body: Center(
  52. child: Container(
  53. color: Colors.black,
  54. child: new Transform(
  55. alignment: Alignment.topRight, //相对于坐标系原点的对齐方式
  56. transform: new Matrix4.skewY(0.3), //沿Y轴倾斜0.3弧度
  57. child: new Container(
  58. padding: const EdgeInsets.all(8.0),
  59. color: Colors.deepOrange,
  60. child: const Text('转换的widget'),
  61. ),
  62. ),
  63. ),
  64. ),
  65. ),
  66. );
  67. }
  68. }
  69. // 属性
  70. class ShowAttribute extends StatelessWidget {
  71. @override
  72. Widget build(BuildContext context) {
  73. return Container(
  74. child: FutureBuilder(
  75. future: rootBundle.loadString('lib/markdown/transform.md'),
  76. builder: (BuildContext context, AsyncSnapshot snapshot) {
  77. if (snapshot.hasData) {
  78. return Markdown(
  79. data: snapshot.data,
  80. selectable: true,
  81. );
  82. } else {
  83. return Center(
  84. child: Text("加载中..."),
  85. );
  86. }
  87. },
  88. ),
  89. );
  90. }
  91. }

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

https://gitee.com/nmgwap/flutter_app

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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