Giorgino Romagnolo
Giorgino Romagnolo

Reputation: 11

Flutter throws an error that I don't know how to fix someone can help me?

I'm watching a tutorial to do a chat app and gives me the error

The argument type 'Widget' can't be assigned to the parameter type

This is the signIn screen file

import 'package:chat_app/widgets/widgets.dart';
import 'package:flutter/material.dart';

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarMain(context),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 24),
        child: Column(
          children: [
            TextField(
              style: simpleTextStyle(),
              decoration: textFieldInputDecoration("email"),
            ),
            TextField(
              style: simpleTextStyle(),
              decoration: textFieldInputDecoration("password"),
            ),
            SizedBox(
              height: 8,
            ),
            Container(
              alignment: Alignment.centerRight,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                child: Text(
                  "Password Dimenticata?",
                  style: simpleTextStyle(),
                ),
              ),
            ),
            SizedBox(height: 8),
            Container(
              alignment: Alignment.center,
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.symmetric(vertical: 20),
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [const Color(0xff007EF4), const Color(0xff2A75BC)],
                  ),
                  borderRadius: BorderRadius.circular(30)),
              child: Text(
                "Sign In",
                style: TextStyle(color: Colors.white, fontSize: 17),
              ),
            )
          ],
        ),
      ),
    );
  }
}

and this is the widget file

import 'package:flutter/material.dart';

Widget AppBarMain(BuildContext context) {
  return AppBar(
    title: Image.asset(
      "assets/images/logo.png",
      height: 50,
    ),
  );
}

InputDecoration textFieldInputDecoration(String hintText) {
  return InputDecoration(
    hintText: hintText,
    hintStyle: TextStyle(color: Colors.white),
    focusedBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.white),
    ),
    enabledBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.white),
    ),
  );
}

TextStyle simpleTextStyle() {
  return TextStyle(color: Colors.white, fontSize: 16);
}

PS: I know that the file is incomplete but I'm writing it.

I tried some methods to resolve but no one of them worked, even on the flutter official website. It tells me that I defined a PreferredSizeWidget but I don't did it.

Upvotes: 0

Views: 673

Answers (1)

h8moss
h8moss

Reputation: 5020

The problem is that you are trying to assign a Widget to an appbar, which is a widget, All appbars are widgets, but not all widgets are appbars so when you declare your function AppBarMain, you say you will return some widget, but the appbar needs to be sure you will only return an appbar, so it gives you an error.

The solution? Just change Widget AppBarMain(BuildContext context) to AppBar AppBarMain(BuildContext context) so flutter knows you will only return an Appbar from that function.

PS

You are violating several style rules with your widget file, of course this is just my personal recommendation but, if you intend to use those functions in different files, you should turn them into classes, if you don't, you should turn them into methods, and if you want to keep them as functions or methods, at least rename AppBarMain to appBarMain

Upvotes: 2

Related Questions