我试图这样做,如果我点击 SpeedDialChild(按钮),它的颜色就会改变,但即使 fun.switchTakePhoto 的值从 true 变为 false(反之亦然),颜色仍然是红色出于某种原因。
class MainPage extends StatefulWidget {
Home createState() => Home();
}
@immutable
class Home extends State<MainPage> {
//Home({super.key});
var fun = FunctionManager();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChangePWD()),
);
},
child: const Text('Change password'),
),
),
floatingActionButton: SpeedDial(
icon: Icons.settings,
backgroundColor: Colors.amber,
children: [
SpeedDialChild(
child: const Icon(Icons.photo_camera),
label: 'Activate/Deactivate: Take Photo',
backgroundColor: fun.switchSendPhoto == true
? const Color.fromARGB(255, 109, 255, 64)
: const Color.fromARGB(255, 255, 64, 64),
onTap: () {
setState(() {
fun.switchTakePhoto = !fun.switchTakePhoto;
});
},
),
]),
);
}
}
class FunctionManager {
bool switchTakePhoto = true;
bool switchSendPhoto = false;
bool switchRec = true;
bool switchPlay = true;
bool switchNotifications = true;
}
最佳答案
问题可能与数据类有关。我是这样解决的。
class FunctionManager {
final bool switchSendPhoto;
FunctionManager({
this.switchSendPhoto = false,
});
FunctionManager copyWith({
bool? switchTakePhoto,
}) {
return FunctionManager(
switchSendPhoto: switchTakePhoto ?? switchSendPhoto,
);
}
}
像这样的状态级变量
FunctionManager fun = FunctionManager()
并更新数据
onTap: () {
fun = fun.copyWith(switchTakePhoto: !fun.switchSendPhoto);
setState(() {});
},
其他人也一样
关于flutter - 快速拨号 : change color on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73263743/