为什么 `DateTime.add(const Duration(days: 1)` 添加 *秒* 而不是天?
我有一个 Text()
小部件,它将 DateTime
显示为 String
。此 Text()
小部件用 GestureDetecture()
包装,其中 onTap
应将 DateTime
更新一 < em>day:
GestureDetector(
child: File2(
dateTime: date,
key: Key(date.toString()),
),
onTap: () {
setState(() {
// This is adding seconds not days
date = date.add(const Duration(days: 1));
});
},
),
但是,虽然我正在这样做,但日期并没有更新一天:
date = date.add(const Duration(days: 1));
但是它会更新几秒钟。为什么1天还没更新?
我尝试过的: 使用Key()
代码:
file_1.dart
:
import 'package:flutter/material.dart';
import 'file_2.dart';
void main() {
runApp(File1());
}
class File1 extends StatefulWidget {
const File1({Key? key}) : super(key: key);
@override
State<File1> createState() => _File1State();
}
class _File1State extends State<File1> {
@override
Widget build(BuildContext context) {
DateTime date = DateTime.now();
return MaterialApp(
home: Scaffold(
body: Center(
child: GestureDetector(
child: File2(
dateTime: date,
key: Key(date.toString()),
),
onTap: () {
setState(() {
// This is adding seconds not days
date = date.add(const Duration(days: 1));
});
},
),
),
),
);
}
}
file_2.dart
:
import 'package:flutter/material.dart';
class File2 extends StatelessWidget {
DateTime dateTime;
File2({Key? key, required this.dateTime}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(dateTime.toString());
}
}
如何更新单击
GestureDetector()
时,File2
中的Text()
会增加 1 天吗?除了
setState()
之外,我还需要使用状态管理吗?
I have a Text()
widget that displays a DateTime
as a String
. This Text()
widget is wrapped with a GestureDetecture()
where the onTap
should update the DateTime
by one day:
GestureDetector(
child: File2(
dateTime: date,
key: Key(date.toString()),
),
onTap: () {
setState(() {
// This is adding seconds not days
date = date.add(const Duration(days: 1));
});
},
),
However, the date is not updating by one day although I'm doing:
date = date.add(const Duration(days: 1));
It is however updating by a few seconds. Why isn't it updating by 1 day?
What I tried:
Using a Key()
Code:
file_1.dart
:
import 'package:flutter/material.dart';
import 'file_2.dart';
void main() {
runApp(File1());
}
class File1 extends StatefulWidget {
const File1({Key? key}) : super(key: key);
@override
State<File1> createState() => _File1State();
}
class _File1State extends State<File1> {
@override
Widget build(BuildContext context) {
DateTime date = DateTime.now();
return MaterialApp(
home: Scaffold(
body: Center(
child: GestureDetector(
child: File2(
dateTime: date,
key: Key(date.toString()),
),
onTap: () {
setState(() {
// This is adding seconds not days
date = date.add(const Duration(days: 1));
});
},
),
),
),
);
}
}
file_2.dart
:
import 'package:flutter/material.dart';
class File2 extends StatelessWidget {
DateTime dateTime;
File2({Key? key, required this.dateTime}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(dateTime.toString());
}
}
How can I update the
Text()
inFile2
by 1 day when clicking on theGestureDetector()
?Do I need to use a statemanagement other then
setState()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题
发生此错误的原因很简单,因为您在构建方法的开头有这一行:
DateTime date = DateTime.now();
。这是代码中发生的工作流程:
DateTime.now()
。第 5 步的结果是,日期值仅更新了一会儿,但随后
DateTime.now()
在 UI 中呈现。解决方案
在 State 类中而不是在构建方法中启动
date
值。这样,您的date
值就不会被 Widget 重建覆盖:Problem
This error occurs simply because you have this line at the beginning of the build method:
DateTime date = DateTime.now();
.This is the workflow of what is happening in your code:
DateTime.now()
at the beginning of the build method.As a result of Step 5, the date value just gets updated for a moment, but then the
DateTime.now()
is rendered in UI.Solution
Initiate the
date
value in your State class and not in the build method. This way, yourdate
value won't be overwritten by the Widget rebuild: