Why the function setState() is not defined eventhough I have created a Stateful widget?

Tanvir Alam

I am trying to change position of one component by clicking an icon. While trying to change the state of the icon by setState() method,I am getting an error which is telling setState() method is not defined in widget dashboard.

Code

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
final Color backgroundColor = Color(0xFF4A4A58);

class MenuDashBoard extends StatefulWidget {
  @override
  _MenuDashBoardState createState() => _MenuDashBoardState();
}

class _MenuDashBoardState extends State<MenuDashBoard> {
  bool isCollapsed = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: backgroundColor,
      body: Stack(
        children: <Widget>[
          menu(context),
          dashboard(context)
        ],
      ),

    );
  }
}

Widget menu(context){
  return Padding(
    padding: const EdgeInsets.all(8.0),
    child: Align(
      alignment: Alignment.centerLeft,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            "Dashboard ",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          SizedBox(height: 10,),
          Text(
            "Messages ",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          SizedBox(height: 10,),
          Text(
            "Utility Bills ",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          SizedBox(height: 10,),
          Text(
            "Funds Transfer",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          SizedBox(height: 10,),
          Text(
            "Branches",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
        ],),
    ),
  );
}

Widget dashboard (context){
  return Material(
    elevation: 8,
    color: backgroundColor,
    child: Container(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            mainAxisSize:MainAxisSize.max,
            children: <Widget>[
            InkWell(
              child: Icon(Icons.menu, color: Colors.white),
              onTap: (){
                setState()// Here is the error with a red underline, The function setState is not defined.
              },
            ),

            Text("My Cards", style: TextStyle(fontSize: 24,color: Colors.white),),
            Icon(Icons.settings, color: Colors.white)
          ],),
        ],
      ),
    ),
  );
}
John Joe

You should put all your widget function inside widget build.

final Color backgroundColor = Color(0xFF4A4A58);

class MenuDashBoard extends StatefulWidget {
  @override
  _MenuDashBoardState createState() => _MenuDashBoardState();
}

class _MenuDashBoardState extends State<MenuDashBoard> {
  bool isCollapsed = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: backgroundColor,
      body: Stack(
        children: <Widget>[menu(context), dashboard(context)],
      ),
    );
  }

  Widget dashboard(context) {
    return Material(
      elevation: 8,
      color: backgroundColor,
      child: Container(
        padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                InkWell(
                  child: Icon(Icons.menu, color: Colors.white),
                  onTap: () {
                    setState(() {
                        // here
                    });
                  },
                ),
                Text(
                  "My Cards",
                  style: TextStyle(fontSize: 24, color: Colors.white),
                ),
                Icon(Icons.settings, color: Colors.white)
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget menu(context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Align(
        alignment: Alignment.centerLeft,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              "Dashboard ",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              "Messages ",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              "Utility Bills ",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              "Funds Transfer",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            SizedBox(
              height: 10,
            ),
            Text(
              "Branches",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          ],
        ),
      ),
    );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flutter: The function setState isn't defined and I am using a Stateful widget

setState() for stateful widget

setState Not Update StateFul Widget

Cannot have function with generic parameter in generic stateful widget

Why I'm not able to get the updated variable even using setstate inside the stateful widget. As I want to update my Container on new TabBar option

Socket is not defined when I have created one

Flutter - setState not updating inner Stateful Widget

How do I add Items to a List with a button I created in another stateful widget?

Set state the value of variable in a function that returns function which doesn't have stateful widget

Uncaught ReferenceError: function is not defined eventhough scope seems right Javascript

Why I have defined a js function,but the brower prompt that can not find the function?

Where is an array defined in C, and why don't I have to define it again when I pass it as a function parameter?

NoMethodError eventhough action is defined?

I have added a local storage function but it doesnt seem to work on my Scheduler i have created and i cant figure out why

The function setState is not defined

Have I created a memory leak in this function?

requirejs: Why does a function defined in a module not have access to local variables inside require() and how can i achieve this?

Unable to call setState of Flutter Stateful Widget from a static method

Flutter setState() not refreshing widget when called from stateful child

how to access an object created in one stateful widget in another stateful widget in flutter

Passing function as named argument in flutter stateful widget

Why is it saying a isnt defined even though i have defined you

Why are stateful widgets defined as two classes in flutter?

Flutter : No SuchMethodError when I called Stateful Widget

Can I change locale on Stateful widget?

how to have a button build a widget and have it setstate

Why is my function receiving an error that it is not defined in this scope? I am simply trying to call the function and have it run as of right now

How to run a function inside a child stateful widget from parent widget?

Why is Wordpress not recognising the function I created?