Как решить «The superclass 'PreferredSize' doesn't have a zero argument constructor.» при миграции на Flutter 2?
Перевёл проект на свежую версию Флаттера и получил вот такую ошибку:
The superclass 'PreferredSize' doesn't have a zero argument constructor. Try declaring a zero argument constructor in 'PreferredSize', or explicitly invoking a different constructor in 'PreferredSize'.
Гугл предложил такое решение, но оно не сработало:
You may not even have to extend PreferredSize you may just wrap an appBar or any other widget inside a PreferredSize and just specify its height.PreferredSize( preferredSize: Size.fromHeight(72), child: AppBar( title: Text(title, style: TextStyles.h1), centerTitle: false, elevation: 0, brightness: Brightness.light, backwardsCompatibility: false, bottom: bottomAppBarWidget, ))
Вот код файла с ошибкой:
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../../../size_config.dart';
class CustomAppBar extends PreferredSize {
double? rating;
CustomAppBar({@required this.rating});
@override
Size get preferredSize => Size.fromHeight(AppBar().preferredSize.height);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
child: Row(
children: [
SizedBox(
height: getProportionateScreenWidth(40),
width: getProportionateScreenWidth(40),
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60),
),
color: Colors.white,
padding: EdgeInsets.zero,
onPressed: () => Navigator.pop(context),
child: SvgPicture.asset(
"assets/icons/Back ICon.svg",
height: 15,
),
),
),
Spacer(),
Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(14),
),
child: Row(
children: [
Text(
"$rating",
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
),
),
const SizedBox(width: 5),
SvgPicture.asset("assets/icons/Star Icon.svg"),
],
),
)
],
),
),
);
}
}
Ответы (1 шт):
Автор решения: Maxgmer
→ Ссылка
Я бы на вашем месте сделал декларацию класса по-другому:
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {}
Класс PreferredSizeWidget обозначает API PreferredSize виджетов, поэтому он вам и нужен. А сам виджет PreferredSize - это обертка для удобства, которая реализует тот же PreferredSizeWidget, которая в вашем случае не нужна.