Skip to content

Commit

Permalink
shared_preferences - save firstDay
Browse files Browse the repository at this point in the history
  • Loading branch information
1day1 committed Feb 22, 2024
1 parent 0151b76 commit 7e950cd
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/screens/home_screen.dart
@@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
Expand All @@ -10,9 +11,27 @@ class HomeScreen extends StatefulWidget {

class _HomeScreenState extends State<HomeScreen> {
DateTime firstDay = DateTime.now();
late SharedPreferences prefs;

void onHeartPressed() {
showCupertinoDialog(
@override
void initState() {
// TODO: implement initState
super.initState();
initPrefs();
}

Future initPrefs() async {
prefs = await SharedPreferences.getInstance();
final String? _firstDay = prefs.getString('firstDay');
if (_firstDay != null) {
setState(() {
firstDay = DateTime.parse(_firstDay);
});
}
}

void onHeartPressed() async {
await showCupertinoDialog(
context: context,
builder: (BuildContext context) {
return Align(
Expand All @@ -22,7 +41,8 @@ class _HomeScreenState extends State<HomeScreen> {
height: 300,
child: CupertinoDatePicker(
initialDateTime: firstDay,
onDateTimeChanged: (DateTime date) {
maximumDate: DateTime.now(),
onDateTimeChanged: (DateTime date) {
setState(() {
firstDay = date;
});
Expand All @@ -34,6 +54,7 @@ class _HomeScreenState extends State<HomeScreen> {
},
barrierDismissible: true,
);
await prefs.setString('firstDay', firstDay.toString());
}

@override
Expand Down

0 comments on commit 7e950cd

Please sign in to comment.