2

I've conducted a long research on the internet about this (what I think is mischievous) use of Material widget in flutter, Any flutter developer faced the No Material widget found. exception even when using cupertino design widgets.

A simple example to produce is

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

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

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Example'),
        leading: IconButton(
          onPressed: () {},
          icon: Icon(
            Icons.close,
            color: Colors.black54,
          ),
        ),
      ),
      child: Center(
        child: IconButton(
          onPressed: () {},
          icon: Icon(
            Icons.close,
            color: Colors.black54,
          ),
        ),
      ),
    );
  }
}

Neither the navigationBar nor the body IconButton will work resulting in the same exception.

When facing this exception I always wrap it inside a Material to bypass it, but I think am doing it wrong.IconButton is not the only widget and I've searched a lot with no avail.

Please tell me what am I missing ? and thanks in advance.

OverLoadedBurden
  • 606
  • 1
  • 5
  • 14
  • @Faizan Darwesh and @JaffaKetchup no body knows flutter doesn't know about `MaterialApp` the question is the replacement of it and it's descendants like `Material` because there are a lot of non-material widgets depends on them like `IconButton` – OverLoadedBurden Aug 11 '21 at 21:17
  • I also have the same question about this. When I am using `MaterialApp` => `CupertinoTabScaffold` and using `TextField`, it show error like this **TextField widgets require MaterialLocalizations to be provided by a Localizations widget ancestor. The material library uses Localizations to generate messages, labels, and abbreviations.**. Anyone has any solution ? Thanks. – Vexal Dec 15 '21 at 07:25
  • 1
    @Vexal in case you are still searching for optimal solutions, you can replace the text field with the cupertino version `CupertinoTextField`. – OverLoadedBurden Jan 31 '22 at 11:50

3 Answers3

1

You need to wrap your root Widget in MaterialApp() so that you can access Material components.

For example,

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
Faizan Darwesh
  • 301
  • 1
  • 5
0

Not sure about the Cupertino part, but (afaik) you need to wrap your app in MaterialApp() to access Material widgets and other methods etc.

JaffaKetchup
  • 1,151
  • 10
  • 26
0

For MaterialApp you have to use IconButton, for CupertinoApp you have to use CupertinoButton but instead of icon use child

Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37