Reputation: 734
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.
Upvotes: 3
Views: 1335
Reputation: 3730
For MaterialApp
you have to use IconButton
, for CupertinoApp
you have to use CupertinoButton
but instead of icon
use child
Upvotes: 0
Reputation: 341
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'),
);
}
}
Upvotes: 1
Reputation: 1611
Not sure about the Cupertino part, but (afaik) you need to wrap your app in MaterialApp()
to access Material widgets and other methods etc.
Upvotes: 0