I think I have a simple question but I don’t know why its not work
Simple:
main.dart
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const Welcome(),
);
}
}
Second file:
welcome.dart
class Welcome extends StatelessWidget {
const Welcome({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
appBar: AppBarNav(
title: 'JobFinder',
),
);
}
}
AppBar file:
appbar.dart
import 'package:flutter/material.dart';
class AppBarNav extends StatelessWidget {
final String title;
const AppBarNav({super.key, required this.title});
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(title),
);
}
}
Debbuger say:
The argument type ‘AppBarNav’ can’t be assigned to the parameter type ‘PreferredSizeWidget?’
Why?
Do you know why the debugger tells me that I can’t use AppBar in the Welcome.dart file?
I know it’s probably simple but I can’t find a solution to it.
Generally I want to place an appBar on every screen (same Appbar)
>Solution :
the argument appBar needs to be directly an AppBar. You are giving it an AppBarNav which is not an AppBar even if it only consists of one.
Technically it doesn’t need to be an AppBar but a PreferredSizeWidget which an AppBar implements. So alternatively you could also let your AppBarNav implement PreferredSizeWidget like this
class AppBarNav extends StatelessWidget implements PreferredSizeWidget {
final String title;
const AppBarNav({super.key, required this.title});
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(title),
);
}
@override
// TODO: implement preferredSize
Size get preferredSize => throw UnimplementedError();
}
But then you need to provide an implementation for preferredSize also