我是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,
),
],
),
);
}
}
我只想在用户单击按钮时读取屏幕中这个开关小部件的值。
2条答案
按热度按时间uelo1irk1#
尝试使用
SwitchGlobal
小部件属性,如fwzugrvs2#
请使用此更新您的交换机代码;
用途: