为什么我的 Gesturedetector 没有触发我给它的功能?

勒莱克

我编写了一个自定义主题系统,并创建了一个菜单来更改主题。我的 ThemeSettingsPage 有一个自定义 ThemeListTile,它包含一个单选按钮。当我按下单选按钮时,它会改变主题。当我按下包含 GestureDetector 的 Container 时,我得到了 gestureDetector 已被触发的输出(请参阅 参考资料log("Gesturedetector has triggered")),但主题没有改变。

为什么功能会触发,但我的主题只有在我按下单选按钮时才会改变?

我的 ThemeSettings 屏幕类:

class ThemeSettingsScreen extends StatefulWidget {
  const ThemeSettingsScreen({Key? key}) : super(key: key);

  @override
  State<ThemeSettingsScreen> createState() => _ThemeSettingsScreenState();
}

class _ThemeSettingsScreenState extends State<ThemeSettingsScreen> {
  CustomThemeMode? themeValue;

  @override
  void initState() {
    super.initState();
  }

  void updateRadioValue(CustomThemeMode customThemeMode){
    
  }


  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      appBar: const CustomAppBar(title: Text("Themes")),
      body: ListView(children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(
              width: 150,
              height: 150,
              child: ThemeSettingsListTile(
                onChanged: (void value) {setState(() {
                  
                  themeValue = CustomThemeMode.defaultLight;
                  themeManager.setTheme(themeValue!);
                });  },
                themeName: 'Default light',
                groupValue: themeValue,
                value: CustomThemeMode.defaultLight,
                child: Stack(
                  clipBehavior: Clip.antiAlias,
                  children: const [
                  Image(
                    image: CustomUi.backgroundPatternDot,
                    repeat: ImageRepeat.repeat,
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Image(
                      image: CustomUi.backgroundWelleFront,
                    ),
                  ),
                ]),
              ),
            ),
            SizedBox(
              width: 150,
              height: 150,
              child: ThemeSettingsListTile(
                onChanged: (void value) {setState(() {
                  themeValue = CustomThemeMode.defaultDark;
                  themeManager.setTheme(themeValue!);
                });  },
                themeName: 'Default dark',
                groupValue: themeValue,
                value: CustomThemeMode.defaultDark,
                child: Stack(children: const [
                  Image(
                    image: CustomUi.backgroundPatternDotDark,
                    repeat: ImageRepeat.repeat,
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Image(
                      image: CustomUi.backgroundWelleFront,
                    ),
                  ),
                ]),
              ),
            ),
          ],
        )
      ]),
    );
  }
}

我的 ThemeTile 类:

class ThemeSettingsListTile extends StatelessWidget {
  final Widget child;
  final double? width;
  final double? height;
  final String themeName;
  final CustomThemeMode? value;
  final CustomThemeMode? groupValue;
  final Function(void) onChanged;

  const ThemeSettingsListTile(
      {Key? key,
      required this.child,
      this.width,
      this.height,
      this.value,
      this.groupValue,
      required this.onChanged,
      required this.themeName})
      : super(key: key);

  @override
  Widget build(BuildContext context) {

    bool isActive = (value == groupValue);

    return Padding(
      padding: const EdgeInsets.all(10),
      child: GestureDetector(
        onTap: (() {
          onChanged;
          log("Gesturedetector has triggered");
        }),
        child: Material(
          color: Colors.transparent,
          elevation: 10,
          child: Container(
            width: width,
            height: height,
            decoration: BoxDecoration(
                color: CustomColor.uibrightBeige,
                borderRadius: BorderRadius.circular(10),
                border: Border.all(color: isActive ? Theme.of(context).scaffoldBackgroundColor : CustomColor.uiBrightBrown)),
            child: Stack(children: [
              child,
              Row(
                children: [Radio(value: value, groupValue: groupValue, onChanged: onChanged), Text(themeName)],
              )
            ]),
          ),
        ),
      ),
    );
  }
}
沃伊泰克·苏罗卡

您需要在 onTap 中的 onChanged 之后使用括号:

onChanged();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在 GestureDetector 下使用 setState

为什么我的输入没有触发我的功能

Flutter:InkWell与GestureDetector:有什么区别?

Ink和GestureDetector有什么区别?

为什么我的crontab没有触发?

为什么没有触发我的ValueConverter?

为什么我的 onNext 没有触发我的 rx 订阅?

为什么我的带有生成器功能的 setTimeout 没有触发?

为什么我的字段没有初始化为我给它的值

Flutter - 我想使用 GestureDetector 而不是按鈕

为什么单击或更改事件都没有触发我的功能?

为什么我的 Jersey 过滤器没有被触发?

为什么我的警报没有在Android Studio中触发?

为什么我的jQuery聚焦(或模糊)事件没有被触发?

为什么我的redux-observable史诗没有被触发?

为什么我的调整大小事件没有触发?

为什么我的ListView ItemClick事件没有被触发?

为什么我的动作事件没有被触发?

为什么我的最小和最大验证没有触发?

为什么我的表单验证没有触发?

为什么我的onClick事件没有在React上触发?

为什么我的jquery change事件没有触发?

为什么侦听器没有绑定到我分配给它的对象?

为什么在Node中没有调用我的功能代理?

为什么我的功能没有指代正确的路线?

为什么我的拍子里没有padLeft功能?

为什么我的搜索功能没有通过?

为什么我的功能没有停止在“返回”行?

为什么我的按钮 onclick 功能没有弹出确认?