我有一个自定义小部件,可以选择传递一个size属性。如果存在,则应将该值传递给我自己的小部件内的小部件size的属性Icon()。
有没有办法仅在存在时传递该值?
class MyWidget extends StatelessWidget {
final double size;
MyWidget({this.size});
Widget build(BuildContext context) {
return Icon(
iconData: IconData(),
size: // Don't pass size here if not present
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
我遇到了类似的问题,我必须选择在按钮中显示图标。
有了零安全性,你想要做的事情就可以通过这种方式实现
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
final double? size;
MyWidget ({this.size});
Widget build(BuildContext context) {
return Icon(
iconData:IconData(),
size: size, // Don't pass size here if not present
);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用
StackIcon()
Run Code Online (Sandbox Code Playgroud)
或者像这样
StackIcon(size: 100.0)
If you supply the size parameter the icon will make use of it, otherwise it uses the default size of the icon.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1939 次 |
| 最近记录: |