提问人:Hana.HD 提问时间:11/10/2023 最后编辑:SternKHana.HD 更新时间:11/12/2023 访问量:51
为什么 Flutter 的复选框中 UI 没有改变?
why the UI doesn't change in checkbox in flutter?
问:
我试图在 Flutter 中创建一个一周的复选框。由于没有编写额外的代码,我制作了一个小部件“周”,当我运行代码时,我可以看到复选框,但是当我单击它们时,UI 不会改变......我希望当我单击勾选的框 => 该框变为未勾选,反之亦然。这不会发生,我不知道问题出在哪里。请帮帮我......请。。。
这是我的全部代码:
import "package:flutter/services.dart";
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
MyPage({super.key});
@override
State<MyPage> createState() => _Example();
}
class _Example extends State<MyPage> {
bool mon = true;
bool tue = true;
bool wed = true;
bool thu = true;
bool fri = true;
bool sat = true;
bool sun = true;
Widget week(bool? value, String day) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(day),
Checkbox(
value: value,
onChanged: (bool? new_Value) {
setState(() {
value = new_Value;
});
}),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('HDBOOKS'),
),
body: Center(
child: Row(
children: [
week(mon, 'Mon'),
week(tue, 'Tue'),
week(wed, 'Wed'),
week(thu, 'Thu'),
week(fri, 'fri'),
week(sat, 'sat'),
week(sun, 'sun'),
],
),
));
}
}
...我希望当我单击勾选的框 => 该框变为未勾选,反之亦然。这不会发生,我不知道问题出在哪里。请帮帮我......请。。。
答:
0赞
LazzyCoderr
11/10/2023
#1
您已经为周小部件创建了一个辅助函数,这不是声明能够在运行时更新其状态的小部件的好方法。
在帮助程序函数内部,当您点击时,将调用生成方法,但它不会反映在 UI 部分中,因为函数的行为只是返回一个值或执行函数内的代码。
有 3 种方法可以解决这个问题,
- 在 build 方法中声明所有日期的星期小部件。此解决方案有效,但您必须多次编写重复的代码。
- 用 StatefulBuilder 包装周小部件,该生成器提供更新小部件状态的方法,
Widget week(bool? value, String day) {
return StatefulBuilder(
builder: (context, updatets) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(day),
Checkbox(
value: value,
onChanged: (bool? new_Value) {
updatets(() {
value = new_Value;
});
},
),
],
),
),
);
},
);
}
这将解决您的问题,但效率不高。
- 为一周创建一个单独的有状态小部件。通过创建单独的小组件,小组件会在状态发生变化时管理其状态,并根据小组件的生命周期更新其状态。这是一个高效、清洁和可维护的解决方案。
import 'package:flutter/material.dart';
class Week extends StatefulWidget {
Week({required this.day, this.value = false});
bool? value;
final String day;
@override
State<Week> createState() => _WeekState();
}
class _WeekState extends State<Week> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(widget.day),
Checkbox(
value: widget.value,
onChanged: (bool? new_Value) {
setState(() {
widget.value = new_Value;
});
},
),
],
),
),
);
}
}
评论
0赞
Hana.HD
11/10/2023
非常感谢您的完美解释。知道了。🙏
0赞
Tejaswini Dev
11/10/2023
#2
请试试这个
代码中的问题在于如何更新状态。当您将值传递给 week 函数时,它是一个局部变量,在函数内部更改它不会影响 _Example 类中的状态变量(mon、tue 等)。
要解决此问题,您需要直接在 onChanged 回调中更新状态变量(mon、tue 等)。下面是代码的更新版本:
import "package:flutter/material.dart";
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
MyPage({super.key});
@override
State<MyPage> createState() => _Example();
}
class _Example extends State<MyPage> {
bool mon = true;
bool tue = true;
bool wed = true;
bool thu = true;
bool fri = true;
bool sat = true;
bool sun = true;
Widget week(bool value, String day, Function(bool) onChanged) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(day),
Checkbox(
value: value,
onChanged: (bool? newValue) {
onChanged(newValue ?? false);
}),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('HDBOOKS'),
),
body: Center(
child: Row(
children: [
week(mon, 'Mon', (newValue) {
setState(() {
mon = newValue;
});
}),
week(tue, 'Tue', (newValue) {
setState(() {
tue = newValue;
});
}),
week(wed, 'Wed', (newValue) {
setState(() {
wed = newValue;
});
}),
week(thu, 'Thu', (newValue) {
setState(() {
thu = newValue;
});
}),
week(fri, 'Fri', (newValue) {
setState(() {
fri = newValue;
});
}),
week(sat, 'Sat', (newValue) {
setState(() {
sat = newValue;
});
}),
week(sun, 'Sun', (newValue) {
setState(() {
sun = newValue;
});
}),
],
),
),
);
}
}
这样,onChanged 回调会直接更新状态变量,并且 UI 应该会正确反映更改。
评论
0赞
LazzyCoderr
11/11/2023
与其编写重复的代码和多个 setstate,不如创建单独的小部件并将所有数据包装在数组中,并将 onTap 作为参数传递,这样它使代码更干净、更易于维护......如果我错了,请纠正我..
评论