如何制作自定义FlutterFlow复选框?排查编译错误
自定义FlutterFlow复选框编译错误解决求助
我需要实现一个圆形容器式自定义复选框:未选中时仅显示带边框的圆形,选中时圆形容器填充背景色,内部显示指定图标;点击复选框可切换选中状态,同时触发状态更新逻辑。
在FlutterFlow中开发时,无论是使用第三方复选框包还是自行编写自定义组件代码,只要给组件传入onChange函数参数,就会触发编译错误:
Unknown error compiling custom code. A common cause is a custom widget or action whose name in the code does not match the name provided in the editor.
移除onChange参数后编译正常,但复选框失去交互能力。我尝试了自行编写代码(未依赖第三方包),仍出现相同错误,代码如下:
// Automatic FlutterFlow imports import '/flutter_flow/flutter_flow_theme.dart'; import '/flutter_flow/flutter_flow_util.dart'; import '/custom_code/widgets/index.dart'; // Imports other custom widgets import '/flutter_flow/custom_functions.dart'; // Imports custom functions import 'package:flutter/material.dart'; // Begin custom widget code // DO NOT REMOVE OR MODIFY THE CODE ABOVE! class IngridentsCheckBox extends StatefulWidget { const IngridentsCheckBox({ Key? key, this.width, this.height, required this.onChange, required this.isChecked, required this.size, required this.iconSize, required this.selectedColor, required this.selectedIconColor, required this.borderColor, required this.checkIcon, }) : super(key: key); final double? width; final double? height; final Function onChange; final bool isChecked; final double size; final double iconSize; final Color selectedColor; final Color selectedIconColor; final Color borderColor; final Icon checkIcon; @override _IngridentsCheckBoxState createState() => _IngridentsCheckBoxState(); } class _IngridentsCheckBoxState extends State<IngridentsCheckBox> { bool _isSelected = false; @override Widget build(BuildContext context) { return GestureDetector( onTap: () { setState(() { _isSelected = !_isSelected; widget.onChange(_isSelected); }); }, child: AnimatedContainer( margin: EdgeInsets.all(4), duration: Duration(milliseconds: 500), curve: Curves.fastLinearToSlowEaseIn, decoration: BoxDecoration( color: _isSelected ? widget.selectedColor ?? Colors.blue : Colors.transparent, borderRadius: BorderRadius.circular(3.0), border: Border.all( color: widget.borderColor ?? Colors.black, width: 1.5, )), width: widget.size ?? 18, height: widget.size ?? 18, child: _isSelected ? Icon( Icons.check, color: widget.selectedIconColor ?? Colors.white, size: widget.iconSize ?? 14, ) : null, ), ); } }
我在FlutterFlow编辑器中给自定义组件传入的参数如下:
onChange:绑定了用于更新状态的自定义函数isChecked:关联了对应的布尔类型状态变量size:设置为24iconSize:设置为16selectedColor:指定了选中状态下的容器背景色selectedIconColor:指定了选中状态下的图标颜色borderColor:指定了未选中状态下的边框颜色checkIcon:指定了选中时显示的图标
我尝试过移除onChange参数(编译正常但无交互),也尝试使用Flutter状态管理方案,但都没能解决问题,希望能得到帮助修复这个编译错误,实现可正常交互的自定义复选框。
内容的提问来源于stack exchange,提问作者Ahmed El Sayed
相关产品推荐
相关产品推荐

