为什么我的 Gesturedetector 不触发我给它的功能?
Why does my Gesturedetector not trigger the Function I give to it?
我编写了自定义主题系统,并创建了一个菜单来更改主题。
我的 ThemeSettingsPage 有一个自定义 ThemeListTile,它包含一个单选按钮。当我按下单选按钮时,它会改变主题。当我按下包含 Gesturedetector 的 Container 时,我得到了 gestureDetector 已被触发的输出(参见 log("Gesturedetector has triggered")
),但主题没有改变。
为什么功能触发了,但我的主题只有在我按下单选按钮时才会改变?
我的主题设置屏幕Class:
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:
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();
我编写了自定义主题系统,并创建了一个菜单来更改主题。
我的 ThemeSettingsPage 有一个自定义 ThemeListTile,它包含一个单选按钮。当我按下单选按钮时,它会改变主题。当我按下包含 Gesturedetector 的 Container 时,我得到了 gestureDetector 已被触发的输出(参见 log("Gesturedetector has triggered")
),但主题没有改变。
为什么功能触发了,但我的主题只有在我按下单选按钮时才会改变?
我的主题设置屏幕Class:
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:
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();