Skip to content

Commit 6815026

Browse files
committed
feat: Add increasing counter. #1
1 parent d002ed3 commit 6815026

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [2.3 Forcing `24-hour` type input in `TimePicker`](#23-forcing-24-hour-type-input-in-timepicker)
2828
- [3. (Option 2) `Cupertino` Pickers](#3-option-2-cupertino-pickers)
2929
- [3.1 Fixing scrolling behaviour on `Flutter Web`](#31-fixing-scrolling-behaviour-on-flutter-web)
30+
- [4. (Optional) Number of times the `dateTime` field has been updated](#4-optional-number-of-times-the-datetime-field-has-been-updated)
3031

3132

3233
# Why? 🤷‍
@@ -772,3 +773,79 @@ If you run the app in your browser,
772773
it should work properly now! 😀
773774

774775

776+
## 4. (Optional) Number of times the `dateTime` field has been updated
777+
778+
In our specific case,
779+
for analytical purposes,
780+
we are interested in knowing
781+
*how many times a person has updated their timers*.
782+
This feedback can be used to better understand
783+
how people are using our app and how to make it *better*.
784+
785+
Let's implement a simple timer to show this is easy to add!
786+
In a real-world scenario,
787+
you'd make an API call instead of doing everything locally.
788+
We're doing so for simplification purposes.
789+
790+
We'll implement this on the `Material` page,
791+
since the same procedure occurs in the `Cupertino` one, as well.
792+
793+
Go to `lib/material.dart`
794+
and add a new `counter` field.
795+
796+
```dart
797+
int counter = 0;
798+
```
799+
800+
Inside the `Column` `child` parameter,
801+
add this new widget on top of the array.
802+
803+
```dart
804+
child: Column(
805+
mainAxisAlignment: MainAxisAlignment.center,
806+
children: [
807+
808+
// Add this
809+
Padding(
810+
padding: const EdgeInsets.only(bottom: 32.0),
811+
child: Row(
812+
mainAxisAlignment: MainAxisAlignment.center,
813+
children: [
814+
Text(
815+
"You've updated this $counter time(s).",
816+
style: const TextStyle(fontSize: 25),
817+
),
818+
],
819+
),
820+
),
821+
822+
//...
823+
]
824+
)
825+
```
826+
827+
We are simply adding a text that shows the current `counter` value.
828+
829+
Next, all we need to do now
830+
is to change every `setState` instance
831+
(where the `dateTime` field is being updated)
832+
to **increment this counter**.
833+
834+
```dart
835+
setState(() {
836+
dateTime = newDateTime;
837+
counter = counter + 1;
838+
});
839+
```
840+
841+
And you're all done! 😎
842+
843+
Super easy, right?
844+
845+
<p align='center'>
846+
<img width="250" src="https://github.com/dwyl/flutter-date-time-tutorial/assets/17494745/dd12cbf8-7a94-4d5a-bb52-7bb0f2e1787d">
847+
</p>
848+
849+
850+
851+

lib/material.dart

+25-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MaterialExamplePage extends StatefulWidget {
1616

1717
class _MaterialExamplePageState extends State<MaterialExamplePage> {
1818
DateTime dateTime = DateTime.now();
19+
int counter = 0;
1920

2021
/// Opens date picker and returns possible `DateTime` object.
2122
Future<DateTime?> pickDate() => showDatePicker(context: context, initialDate: dateTime, firstDate: DateTime(1900), lastDate: DateTime(2100));
@@ -41,9 +42,10 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
4142

4243
// Update datetime object that's shown with new date
4344
final newDateTime = DateTime(date.year, date.month, date.day, time.hour, time.minute);
44-
setState(
45-
() => dateTime = newDateTime,
46-
);
45+
setState(() {
46+
dateTime = newDateTime;
47+
counter = counter + 1;
48+
});
4749
}
4850

4951
@override
@@ -54,6 +56,18 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
5456
child: Column(
5557
mainAxisAlignment: MainAxisAlignment.center,
5658
children: [
59+
Padding(
60+
padding: const EdgeInsets.only(bottom: 32.0),
61+
child: Row(
62+
mainAxisAlignment: MainAxisAlignment.center,
63+
children: [
64+
Text(
65+
"You've updated this $counter time(s).",
66+
style: const TextStyle(fontSize: 25),
67+
),
68+
],
69+
),
70+
),
5771
Row(
5872
mainAxisAlignment: MainAxisAlignment.center,
5973
children: [
@@ -85,9 +99,10 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
8599

86100
// Update datetime object that's shown with new date
87101
final newDateTime = DateTime(newDate.year, newDate.month, newDate.day, dateTime.hour, dateTime.minute);
88-
setState(
89-
() => dateTime = newDateTime,
90-
);
102+
setState(() {
103+
dateTime = newDateTime;
104+
counter = counter + 1;
105+
});
91106
}),
92107
ElevatedButton(
93108
key: materialTimeButtonKey,
@@ -102,9 +117,10 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
102117

103118
// Update datetime object that's shown with new time
104119
final newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day, newTime.hour, newTime.minute);
105-
setState(
106-
() => dateTime = newDateTime,
107-
);
120+
setState(() {
121+
dateTime = newDateTime;
122+
counter = counter + 1;
123+
});
108124
})
109125
],
110126
),

0 commit comments

Comments
 (0)