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

Flutter: I'm trying to wrap a Text widget with the expanded class but I get "the named parameter 'child' isn't defined" error

当我将“child: Text(...)”放入 Expanded class 时,它告诉我未定义 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,
          ),
        ),
      ),
    );
  }
}

由于扩展小部件,您收到错误消息。

Typically, Expanded widgets are placed directly inside Flex widgets.

删除展开的小部件或用列或行包裹展开的小部件,如下面的代码:

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,
              ),
            ),
          ),
        ],
      ),
    );
  }
}