做网站送的企业邮箱能用吗,网推是干嘛的,广州网络推广平台,沈阳seoFlutter里扩展函数可以用简化代码写法#xff0c;关键字为extension#xff0c;伪代码写法如下#xff1a;
extension 扩展类名 on 扩展类型 { //扩展方法 }
在Flutter页面里实现控件间距会常用到SizedBox#xff0c;可使用扩展函数封装来达到简化代码的目的#xff0…Flutter里扩展函数可以用简化代码写法关键字为extension伪代码写法如下
extension 扩展类名 on 扩展类型 { //扩展方法 }
在Flutter页面里实现控件间距会常用到SizedBox可使用扩展函数封装来达到简化代码的目的基本步骤如下
1、创建num_extension.dart文件扩展类名为SizedBox类型Double后缀Extension代码如下
import package:flutter/material.dart;extension SizedBoxDoubleExtension on double {SizedBox get width SizedBox(width: this);SizedBox get height SizedBox(height: this);SizedBox withHeight(double height) SizedBox(width: this, height: height);
}
2、使用示例对照节选代码如下 ///此为不使用扩展函数的写法Widget _buildRow() {return Row(children: [_buildText(1),const SizedBox(width: 10,),_buildText(2),const SizedBox(width: 10,),_buildText(3),],);}///此为使用扩展函数的写法要简洁些Widget _buildRowExtension() {return Row(children: [_buildText(1),10.0.width,_buildText(2),10.0.width,_buildText(3),],);}Widget _buildText(String text) {return Text(text,style: const TextStyle(color: Colors.blue),);}