颤振 setState 未定义

flutter setState isn't defined

我有一个用于创建侧边栏菜单的小部件:

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),
          ])
        ],
      ),
    )
  );
}

但我不断收到错误消息,提示我的 setState 函数未定义。我的主要应用程序是一个有状态的小部件,所以这很奇怪。谁能帮帮我?

这是完整的代码!

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),
          ])
        ],
      ),
    )
  );
}

错误消息:“函数 setState 未定义”

setStateState class 的成员,这意味着它只能在扩展 State [=26] 的 class 内部调用=] 的一个 StatefulWidget。如果你的class是扩展StatelessWidget,你就不能调用超级class提供的状态members/methods,包括setStateinitStatedispose 方法。

对于答案,您不能在 State class 之外调用 setState,如果可能,将小部件方法放在 class 内,或者通过setState 作为这些方法的参数。

setState只能在StatefulWidget里面调用。因此,如果我做对了,您可以在 Widget dashboard or menu 小部件中使用 setState。它无法工作,因为那些小部件在 class 之外。尝试将 widgets 放在 class