颤振:我正在尝试用扩展的类包装文本小部件,但出现"the named parameter 'child' isn't defined"错误



当我把"child:文本(…(";在扩展类中,它告诉我孩子没有定义,我不知道该怎么办

class _AppBarButton extends StatelessWidget {
final String title;
final Function onTap;
const _AppBarButton({
Key key,
this.title,
this.onTap,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Expanded(
child: Text(  // this is where the child isn't defined.
title,
style: const TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
),
);
}
}

您收到的错误是由于扩展小部件造成的。

通常,扩展的小部件直接放置在Flex小部件中。

移除扩展的小部件,或者像下面的代码一样用列或行包装扩展的小组件:

class _AppBarButton extends StatelessWidget {
final String title;
final Function onTap;
const _AppBarButton({
Key key,
this.title,
this.onTap,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
children: [
Expanded(
child: Text(
title,
style: const TextStyle(
color: Colors.black,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
),
],
),
);
}
}

相关内容

最新更新