Home > Mobile >  FlutterError (No Material widget found. IconButton widgets require a Material widget ancestor
FlutterError (No Material widget found. IconButton widgets require a Material widget ancestor

Time:06-21

When I used

onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) =>
                      GroupAddPosts(groupType: widget.groupType),
                ),
              );
            },

to change page then I got this error FlutterError (No Material widget found. IconButton widgets require a Material widget ancestor.

But I had did added it, my IconButton has Center on above

@override
  Widget build(BuildContext context) {
    final user = Provider.of<UserProvider>(context).getUser;
    final width = MediaQuery.of(context).size.width;
    String value;
    return _selected == "0"
        ? Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 32),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      IconButton(
                        icon: const Icon(
                          Icons.photo,
                          size: 40,
                        ),
                        onPressed: () => selectImage(),
                      ),
                      Text(
                        'max: 9 photos',
                        style: TextStyle(color: Colors.white),
                      ),
                    ],
                  ),
                ),

So, where has issues?

And I also set

 @override
  void initState() {
    super.initState();
    setState(() {
      _selected = "0";
    });
  }

Still got error.... where has error step and how to fix this problem?

CodePudding user response:

just wrap it with Scaffold or Material widget.

Scaffold(body:Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 32),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      IconButton(
                        icon: const Icon(
                          Icons.photo,
                          size: 40,
                        ),
                        onPressed: () => selectImage(),
                      ),
                      Text(
                        'max: 9 photos',
                        style: TextStyle(color: Colors.white),
                      ),
                    ],
                  ),
                ),

  • Related