flutter - 快速拨号 : change color on click

标签 flutter dart flutter-layout flutter-state

我试图这样做,如果我点击 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/

相关文章:

android - 应该使用什么作为 url_launcher 中的 url 来使 flutter 应用程序打开设备上的内置摄像头?

dart - 为什么使用 List<T> 作为堆栈返回 _GrowableList

android - 在 Android Studio 中 Dart 。没有运行应用程序的设备

flutter - Flutter如何获取有状态窗口小部件字段的值?

dart - Dart编辑器无法使用Pub检索依赖项

flutter - 在 Flutter 中对齐多行 TextField 中的提示文本

flutter - 如何扩展使“行”子级在列表项内达到最大高度?

flutter - 在此主页小部件上方找不到正确的 Provider<FirebaseUser>

firebase - Dart/Flutter Firestore 允许查询中的字段有多个值

flutter - 如何设置带有圆锥形边框的按钮的形状