我目前正在考虑什么是最好的方法,有一个可重用的按钮,不必重建其小部件,但将有能力传递参数时,我调用它的statefulwidget?
目前,我有基于类的widget...
class DynamicButtonStyle {
static customButtonStyle({
String? buttonTitle,
TextStyle? buttonStyle,
VoidCallback? onPressedFunction,
}) {
return ElevatedButton(
onPressed: onPressedFunction,
child: Text(
buttonTitle ?? '',
style: buttonStyle ?? const TextStyle(),
));
}
}
这个基于类的小部件,我在一个StatefulWidget
中调用它,其中我可以传递一个参数,以便在调用我的Flutter应用程序的每个屏幕页面时可以自定义...
login_page.dart <--//在此statefulwidget文件内部调用...
DynamicButtonStyle.customButtonStyle(
buttonTitle: "Register now",
onPressedFunction: testFunction,
),
或者最好只是创建一个StatelessWidget
按钮?还是一样的如果最好创建一个StatelessWidget
的按钮,我可以在调用它时传递一个参数使其动态(大小,高度,颜色)吗?你能提供一个示例代码吗?谢谢大家。
1条答案
按热度按时间eulz3vhy1#
下面是一个示例,说明如何实现具有可定制属性的无状态按钮小部件:
这是我如何使用它的例子: