简体   繁体   English

flutter setState 未定义

[英]flutter setState isn't defined

I have a Widget to create a sidebar menu:我有一个小部件来创建侧边栏菜单:

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.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
            InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
              setState(() {
                isCollapsed = !isCollapsed;
              });
            },),
            Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
                Icon(Icons.settings, color: Colors.white),
          ])
        ],
      ),
    )
  );
}

But I keep getting the error that my setState function isn't defined.但我不断收到我的 setState function 未定义的错误。 My main app is a statefullwidget so this is pretty weird.我的主要应用程序是一个 statefullwidget,所以这很奇怪。 Can anyone help me?谁能帮我?

Here is the full code!这是完整的代码!

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.only(left: 16.0),
    child: Align(
      alignment: Alignment.centerLeft,
    child: Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
      Text("Home", style: TextStyle(color: Colors.white, fontSize: 20),),
      SizedBox(height: 10.0),
      Text("Brochures", style: TextStyle(color: Colors.white, fontSize: 20),),
        SizedBox(height: 10.0),
      Text("Visitekaartje", style: TextStyle(color: Colors.white, fontSize: 20),),
        SizedBox(height: 10.0),
      ]
     )
    )
  );
}

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.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
            InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
              setState(() {
                isCollapsed = !isCollapsed;
              });
            },),
            Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
                Icon(Icons.settings, color: Colors.white),
          ])
        ],
      ),
    )
  );
}

with the error message: 'The function setState isn't defined'带有错误消息:'未定义 function setState'

setState is a member of State class, which means it can only be called inside a class that extends a State class of a StatefulWidget . setState is a member of State class, which means it can only be called inside a class that extends a State class of a StatefulWidget . If your class is extending StatelessWidget , you can't called state members/methods provided by the super class, including setState , initState and dispose methods.如果您的 class 扩展StatelessWidget ,则不能调用超级 class 提供的 state 成员/方法,包括setStateinitStatedispose方法。

For the answer, you can't call setState outside of the State class, if possible, put the widget methods inside the class, or pass setState function as a parameter to those methods. For the answer, you can't call setState outside of the State class, if possible, put the widget methods inside the class, or pass setState function as a parameter to those methods.

setState can only called inside StatefulWidget . setState只能在StatefulWidget内部调用。 So if I got it right, you use setState in Widget dashboard or menu widgets.因此,如果我做对了,您可以在Widget dashboard or menu小部件中使用setState It can't work, because those widgets in outside of class.它无法工作,因为 class 之外的那些小部件。 Try to put widgets inside the class尝试将widgets放入 class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM