需要从另一个地方而不是小部件本身更改小部件的状态

Need to change state of widget from another place not widget itself

我有同一个小部件的两个实例(w1 和 w2),带有一个按钮和一个 onPressed 函数。我知道通过将 onPress 设置为空值来禁用它,到目前为止一切顺利。手头的问题是解决如何在点击一次w1时禁用w2 onPress,如果再次点击则再次启用。

即使我发送了一个包含小部件是否被按下的变量,禁用也永远不会发生,因为我无法从外部(包含我的 2 个实例的小部件)分别触发每个小部件的 setState。

您有几个解决方案。您可以使用更复杂的状态管理系统(例如 Provider 或 Bloc),但尝试“提升状态”可能更简单。

“提升状态”是指将状态从子级中拉出并将其移动到父级。

与其让子项成为有状态的,不如让它们成为无状态的,包含它们的小部件将成为有状态的,并将跟踪哪些按钮已启用,哪些未启用。

// example button, actual implementation may be different
class MyButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final String text;

  const MyButton({Key? key, this.onPressed, required this.text,}) : super(key: key);

  @override
  Widget build(BuildContext context) => ElevatedButton(
    onPressed: onPressed,
    child: Text(text),
  );
}

class ButtonContainer extends StatefulWidget {
  // boilerplate
}

class ButtonContainerState extends State<ButtonContainer> {
  bool isSecondButtonEnabled = true;

  void toggleSecondButton() => setState(() => isSecondButtonEnabled = !isSecondButtonEnabled);

  @override
  Widget build(BuildContext context) => Row(
    children: [
      MyButton(
        text: "Button 1",
        onPressed: toggleSecondButton,
      ),
      MyButton(
        text: "Button 2",
        onPressed: isSecondButtonEnabled ? someOtherFunction : null,
      )
    ],
  );
}