Search code examples
flutterdartflutter-layouticonbutton

Flutter error - The specific widget that could not find a Material ancestor was:


I am using the Align widget to place an Icon button at the bottom center of the screen.

However, I get the following error and I'm unable to resolve it:

The specific widget that could not find a Material ancestor was: IconButton

My code:

return Stack(
  children: <Widget>[
    Container(
      child: GoogleMap(
        initialCameraPosition:
        CameraPosition(target: LatLng(1,1), zoom: 15),
        onMapCreated: (map) {
          mapReady;
        },),
    ),
    Align(
      alignment:Alignment.bottomCenter,
      child: IconButton(
          icon: Icon(Icons.next_week), onPressed: (){}),
    )
  ],

If I replace the IconButton widget with for example a Text widget it works well.

Could you please explain why it doesn't work, why the IconButton needs a Material ancestor?


Solution

  • Because as per documentation of IconButton (https://api.flutter.dev/flutter/material/IconButton-class.html)

    An icon button is a picture printed on a Material widget that reacts to touches by filling with color (ink).

    [..]

    Requires one of its ancestors to be a Material widget.

    IconButton uses most likely ThemeData as well as other things which MaterialApp normally provides.

    Is there a reason you dont use MaterialApp as ancestor?