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()inFile2by 1 day when clicking on theGestureDetector()?Do I need to use a statemanagement other then
setState()?