我想实现制作一个带有不同项目的抽屉,所以我正在为 DrawerItems 创建一个单独的文件,并使用构造函数将数据传递给主文件。但是我在“onPressed”函数上收到以下错误:
“不能将参数类型‘Function’分配给参数类型‘void Function()’”
class DrawerItem extends StatelessWidget {
final String text;
final Function onPressed;
const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18.0,
),
),
onPressed: onPressed,
);
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?
我正在尝试使用 flutter 渲染小部件,但收到以下错误:
“无法为具有非最终字段的类定义 const 构造函数”
“常量构造函数不能调用 State 的非常量超级构造函数”
“名称参数‘Key’未定义”
出现此错误的代码如下:
class ContainerButton extends StatefulWidget {
@override
ContainerButtonState createState() => ContainerButtonState();
}
class ContainerButtonState extends State<ContainerButton> {
final ButtonType buttonType;
const CustomButton({Key key, this.buttonType}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(21),
color: Color(0xfff4f5f9),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
child: CustomButton(buttonType: ButtonType.download),
),
Flexible(
child: CustomButton(buttonType: ButtonType.share),
),
Flexible(
child: CustomButton(buttonType: ButtonType.problem),
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何提示。谢谢你,
我有一个简单的应用程序,正在ListView
使用gestureDetector
. 我想要实现的是,当页面加载到应用程序上时,项目会出现淡入效果,因此它会显示第一个项目,然后显示第二个项目,依此类推。(具有平滑效果)。
我有以下代码:
enum _AniProps { opacity, translateX }
class FadeIn extends StatelessWidget {
final double delay;
final Widget child;
FadeIn(this.delay, this.child);
@override
Widget build(BuildContext context) {
final tween = MultiTween<_AniProps>()
..add(_AniProps.opacity, 0.0.tweenTo(1.0))
..add(_AniProps.translateX, 130.0.tweenTo(0.0));
return PlayAnimation<MultiTweenValues<_AniProps>>(
delay: (300 * delay).round().milliseconds,
duration: 500.milliseconds,
tween: tween,
child: child,
builder: (context, child, value) => Opacity(
opacity: value.get(_AniProps.opacity),
child: Transform.translate(
offset: Offset(value.get(_AniProps.translateX), 0),
child: child,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我用这个 FadeIn 小部件包装每个 ListItems,每个 ListItems 都有不同的延迟。
前任:
淡入(1.0,GestureDector(.................)),
我得到的错误如下: …