Home > Software engineering >  The argument type 'HomePageAppBar' can't be assigned to the parameter type 'Obst
The argument type 'HomePageAppBar' can't be assigned to the parameter type 'Obst

Time:10-31

The code was working well till when I tried Extract CupertinoNavigationBar to a widget.

May Anyone please give some information why this issue happening?

It gives me this error:

Error: The argument type 'HomePageAppBar' can't be assigned to the parameter type 'ObstructingPreferredSizeWidget?'. package:flutter_todo_app/main.dart:27

  • 'HomePageAppBar' is from 'package:flutter_todo_app/main.dart' ('lib/main.dart'). package:flutter_todo_app/main.dart:1
  • 'ObstructingPreferredSizeWidget' is from 'package:flutter/src/cupertino/page_scaffold.dart' ('../../development/flutter/packages/flutter/lib/src/cupertino/page_scaffold.dart'). package:flutter/…/cupertino/page_scaffold.dart:1 navigationBar: HomePageAppBar(), ^
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'widgets/body.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: HomePageAppBar(),
      child: HomePageBody(),
    );
  }
}

class HomePageAppBar extends StatelessWidget {
  const HomePageAppBar({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoNavigationBar(
      middle: const Text('app bar'),
    );
  }
}

CodePudding user response:

wrap your HomePageAppBar() with CupertinoNavigationBar() like this :

    CupertinoNavigationBar(
     child: HomePageAppBar(),
    )

CodePudding user response:

Observe that CupertinoPageScaffold navigationBar accepts ObstructingPreferredSizeWidget but you are providing a Stateless Widget that's what the error says. Try implementing HomePageAppBar with ObstructingPreferredSizeWidget (refer to the below code). I ran your code on my machine it solved the error.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'widgets/body.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: HomePageAppBar(),
      child: HomePageBody(),
    );
  }
}

// <<<<<<<------ CHANGES ------>>>>>>
class HomePageAppBar extends StatelessWidget implements ObstructingPreferredSizeWidget {


const HomePageAppBar({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const CupertinoNavigationBar(
      middle: Text('app bar'),
    );
  }

  @override
  Size get preferredSize => const Size(double.infinity, 55);

  @override
  bool shouldFullyObstruct(BuildContext context) {
    return false;
  }
}
  • Related