Search code examples
flutteruser-interfacewidgetflutter-layoutuser-experience

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


When I put "child: Text(...)" inside the Expanded class, it tells me that child isn't defined and I can't figure out what to do.

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

Solution

  • You are getting the error due to the Expanded widget.

    Typically, Expanded widgets are placed directly inside Flex widgets.

    Either remove the Expanded Widget or Wrap the Expanded Widget with a Column or Row like the following code :

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