flutter 简单封装一个widget控件
2023-09-14 09:04:28 时间
简单来说就是定义一个布局,在里面的build做好接收widget的写法,要用到时传进来即可
WPopupMenu(
onValueChanged: (int value) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(actions[value]),
duration: Duration(milliseconds: 500),
));
},
pressType: PressType.longPress,
actions: actions,
key: null,
child: UnconstrainedBox(//有传一个child进去
child: Container(
height: 40,
color: Colors.cyan,
alignment: Alignment.center,
child: Text(
'我是Title $index',
style: TextStyle(color: Colors.white),
),
),
),
),
class WPopupMenu extends StatefulWidget {
WPopupMenu({
Key? key,
required this.onValueChanged,
required this.actions,
required this.child,
this.pressType = PressType.longPress,
this.pageMaxChildCount = 5,
this.backgroundColor = Colors.black,
this.menuWidth = 250,
this.menuHeight = 42,
});
final ValueChanged<int> onValueChanged;
final List<String> actions;
final Widget child;
final PressType pressType; // 点击方式 长按 还是单击
final int pageMaxChildCount;
final Color backgroundColor;
final double menuWidth;
final double menuHeight;
@override
_WPopupMenuState createState() => _WPopupMenuState();
}
class _WPopupMenuState extends State<WPopupMenu> {
double? width;
double? height;
var button;
var overlay;
OverlayEntry? entry;
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((call) {
width = context.size?.width??0;
height = context.size?.height??0;
button = context.findRenderObject();
overlay = Overlay.of(context)?.context.findRenderObject();
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
removeOverlay();
return Future.value(true);
},
child: GestureDetector(
behavior: HitTestBehavior.translucent,
child: widget.child,//在这里接收传进来的那个child就可以了
onTap: () {
if (widget.pressType == PressType.singleClick) {
onTap();
}
},
onLongPress: () {
if (widget.pressType == PressType.longPress) {
onTap();
}
},
),
);
}
}
相关文章
- .NET 同步与异步之封装成Task(五)
- Java面向对象基础---名词概念的理解:方法的重载、构造函数、封装性
- 对MBProgressHUD进行二次封装并精简使用
- 【C#】工具类-FTP操作封装类FTPHelper
- django基于restframework的CBV封装
- 使用密封类封装用户信息
- Android SQLite封装sql语句、查看数据库
- ML之FE:利用pandas的pd.cut函数对【数字型】字段进行分箱处理函数封装并统计个数
- WebRTC之RTP封装与解封装
- 图片视频组件封装
- jsonp promise 封装
- pymysql封装
- Web端自动化测试selenium对元素定位和操作方法的封装
- UI自动化测试框架搭建 —— 常用操作封装(一)
- (8)Lightweight OpenPose轻量化模型之 模型封装 + 摔倒检测
- mybatis group by 分组查询:将返回结果封装为map