error: The argument type ‘CustomAppBar’ can’t be assigned to the parameter type ‘PreferredSizeWidget’. (argument_type_not_assignable at [christii_flutter] lib/screens/boards_screen.dart:17)

「error: The argument type ‘CustomAppBar’ can’t be assigned to the parameter type ‘PreferredSizeWidget’. (argument_type_not_assignable at [christii_flutter] lib/screens/boards_screen.dart:17)」のアイキャッチ画像

Appbarを共通化しようとしたらエラー発生

こんな感じで実装してエラー回避しましたー

import 'package:flutter/material.dart';

class CustomAppBar extends StatelessWidget with PreferredSizeWidget {
  CustomAppBar(this.title);
  
  final String title;

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text(title),
      leading: GestureDetector(
        onTap: () {
          /* Write listener code here */
        },
        child: const Icon(
          Icons.menu, // add custom icons also
        ),
      ),
      actions: <Widget>[
        Padding(
            padding: const EdgeInsets.only(right: 20),
            child: GestureDetector(
              onTap: () {},
              child: const Icon(
                Icons.edit,
                size: 26,
              ),
            )),
        Padding(
            padding: const EdgeInsets.only(right: 20),
            child: GestureDetector(
              onTap: () {},
              child: const Icon(Icons.more_vert),
            )),
      ],
    );
  }
}

参考記事

The AppBarDesign can’t be assigned to the parameter type ‘PreferredSizeWidget’