flutter 从父屏幕阅读开关值

rnmwe5a2  于 2023-03-19  发布在  Flutter
关注(0)|答案(2)|浏览(95)

我是Flutter的新手,我已经创建了一个带有Statefull小部件的开关小部件,我试图从屏幕上读取开关的值来完成某些任务。
我已经建立了自定义开关。这是我的开关小部件。

class SwitchGlobal extends StatefulWidget {
  final bool currentValue;
  final String controlText;
  final double fontSize;
  final FontWeight fontWeight;

  const SwitchGlobal({
    super.key,
    required this.currentValue,
    required this.controlText,
    required this.fontSize,
    required this.fontWeight,
  });

  @override
  State<SwitchGlobal> createState() =>
      _SwitchGlobalState(currentValue, controlText, fontSize, fontWeight);
}

class _SwitchGlobalState extends State<SwitchGlobal> {
  bool isSwitched = false;
  bool currentValue = false;
  String labelText = "";
  double fontSize = 8;
  FontWeight fontWeight = FontWeight.bold;

  _SwitchGlobalState(
      this.currentValue, this.labelText, this.fontSize, this.fontWeight);

  void toggleSwitch(bool value) {
    setState(() {
      currentValue = !currentValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            labelText,
            style: TextStyle(fontSize: fontSize, fontWeight: fontWeight),
          ),
          Switch(
            onChanged: toggleSwitch,
            value: currentValue,
            activeColor: Colors.green,
            activeTrackColor: Colors.greenAccent,
            inactiveThumbColor: Colors.grey,
            inactiveTrackColor: Colors.grey.shade200,
          ),
        ],
      ),
    );
  }
}

我只想在用户单击按钮时读取屏幕中这个开关小部件的值。

uelo1irk

uelo1irk1#

尝试使用SwitchGlobal小部件属性,如

Switch(
   value: widget.currentValue,
fwzugrvs

fwzugrvs2#

请使用此更新您的交换机代码;

class SwitchGlobal extends StatefulWidget {
  final bool currentValue;
  final String controlText;
  final double fontSize;
  final FontWeight fontWeight;
  final Function(bool) voidCallback;

  const SwitchGlobal({
    super.key,
    required this.currentValue,
    required this.controlText,
    required this.fontSize,
    required this.fontWeight,
    required this.voidCallback,
  });

  @override
  State<SwitchGlobal> createState() => _SwitchGlobalState();
}

class _SwitchGlobalState extends State<SwitchGlobal> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            widget.controlText,
            style: TextStyle(
                fontSize: widget.fontSize,
                fontWeight: widget.fontWeight,
                color: Colors.white),
          ),
          Switch(
            // onChanged:  voidCallback,
            onChanged: (v) {
              widget.voidCallback(v);
            },
            value: widget.currentValue,
            activeColor: Colors.green,
            activeTrackColor: Colors.greenAccent,
            inactiveThumbColor: Colors.grey,
            inactiveTrackColor: Colors.grey.shade200,
          ),
        ],
      ),
    );
  }
}

用途:

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool currentValue = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      body: Column(
        children: [
          const SizedBox(height: 100),
          SwitchGlobal(
            voidCallback: (v) {
              setState(() {
                currentValue = !currentValue;
              });
            },
            currentValue: currentValue,
            fontSize: 24,
            controlText: "hello",
            fontWeight: FontWeight.bold,
          ),
        ],
      ),
    );
  }
}

相关问题