Skip to content

Commit

Permalink
new Button + Change Date
Browse files Browse the repository at this point in the history
  • Loading branch information
Sooftyy committed Sep 21, 2024
1 parent d40859a commit ff49abc
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 102 deletions.
272 changes: 170 additions & 102 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; // For date formatting

void main() {
runApp(MyApp());
Expand All @@ -8,124 +9,121 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: WhiteScreen(),
title: 'Water Tracker',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class WhiteScreen extends StatefulWidget {
class MyHomePage extends StatefulWidget {
@override
_WhiteScreenState createState() => _WhiteScreenState();
_MyHomePageState createState() => _MyHomePageState();
}

class _WhiteScreenState extends State<WhiteScreen> {
List<Map<String, dynamic>> drinks = []; // List to store drinks
class _MyHomePageState extends State<MyHomePage> {
// Store current date
DateTime selectedDate = DateTime.now();
// Store drinks per day
Map<String, List<Map<String, dynamic>>> drinksPerDay = {};

void _showAddDrinkDialog() {
String drinkName = '';
String mlCount = '';
// Controller for drink name and ml
final TextEditingController drinkNameController = TextEditingController();
final TextEditingController drinkMlController = TextEditingController();

showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Add Drink'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
decoration: InputDecoration(labelText: 'Drink Name'),
onChanged: (value) {
drinkName = value;
},
),
TextField(
decoration: InputDecoration(labelText: 'ML Count'),
keyboardType: TextInputType.number,
onChanged: (value) {
mlCount = value;
},
),
],
),
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
ElevatedButton(
child: Text('Add Drink'),
onPressed: () {
if (drinkName.isNotEmpty && mlCount.isNotEmpty) {
setState(() {
drinks.add({
'name': drinkName,
'ml': int.tryParse(mlCount) ?? 0,
});
});
Navigator.of(context).pop(); // Close dialog
}
},
),
],
);
},
);
// Format the date
String getFormattedDate(DateTime date) {
return DateFormat('dd-MM-yyyy').format(date);
}

void _showDeleteConfirmationDialog(int index) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Drink'),
content: Text('Are you sure you want to delete this drink?'),
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Delete'),
onPressed: () {
setState(() {
drinks.removeAt(index);
});
Navigator.of(context).pop();
},
),
],
);
},
);
// Get the total ml for the current date
int getTotalMlForDate() {
String formattedDate = getFormattedDate(selectedDate);
if (drinksPerDay.containsKey(formattedDate)) {
return drinksPerDay[formattedDate]!
.fold(0, (sum, drink) => sum + (drink['ml'] as int));
}
return 0;
}

int get totalMl {
return drinks.fold(0, (sum, drink) => sum + (drink['ml'] as int));
// Add a drink
void addDrink(String name, int ml) {
String formattedDate = getFormattedDate(selectedDate);
setState(() {
if (!drinksPerDay.containsKey(formattedDate)) {
drinksPerDay[formattedDate] = [];
}
drinksPerDay[formattedDate]!.add({'name': name, 'ml': ml});
});
}

// Delete a drink
void deleteDrink(int index) {
String formattedDate = getFormattedDate(selectedDate);
setState(() {
drinksPerDay[formattedDate]!.removeAt(index);
});
}

// Go to the previous day
void previousDay() {
setState(() {
selectedDate = selectedDate.subtract(Duration(days: 1));
});
}

// Go to the next day
void nextDay() {
setState(() {
selectedDate = selectedDate.add(Duration(days: 1));
});
}

@override
Widget build(BuildContext context) {
String formattedDate = getFormattedDate(selectedDate);
List<Map<String, dynamic>> drinks = drinksPerDay[formattedDate] ?? [];

return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('WaterMinder'),
title: Text('Water Tracker'),
),
body: Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Text(
'Total ML: $totalMl',
children: <Widget>[
SizedBox(height: 20),

// Date with Left and Right buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.arrow_left),
onPressed: previousDay,
),
Text(
formattedDate,
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
IconButton(
icon: Icon(Icons.arrow_right),
onPressed: nextDay,
),
],
),

SizedBox(height: 20),

// Total ML Count
Text(
'Total ML: ${getTotalMlForDate()}',
style: TextStyle(fontSize: 24),
),

SizedBox(height: 20),

// List of drinks
Expanded(
child: ListView.builder(
itemCount: drinks.length,
Expand All @@ -134,21 +132,91 @@ class _WhiteScreenState extends State<WhiteScreen> {
title: Text(drinks[index]['name']),
subtitle: Text('${drinks[index]['ml']} ml'),
trailing: IconButton(
icon: Icon(Icons.close, color: Colors.red),
onPressed: () => _showDeleteConfirmationDialog(index),
icon: Icon(Icons.delete),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Delete Drink"),
content: Text("Are you sure you want to delete this drink?"),
actions: [
TextButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text("Delete"),
onPressed: () {
deleteDrink(index);
Navigator.of(context).pop();
},
),
],
);
},
);
},
),
);
},
),
),

// Add Drink Button
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Add Drink"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: drinkNameController,
decoration: InputDecoration(labelText: 'Drink Name'),
),
TextField(
controller: drinkMlController,
decoration: InputDecoration(labelText: 'ML'),
keyboardType: TextInputType.number,
),
],
),
actions: [
TextButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text("Add"),
onPressed: () {
String name = drinkNameController.text;
int ml = int.parse(drinkMlController.text);
addDrink(name, ml);
drinkNameController.clear();
drinkMlController.clear();
Navigator.of(context).pop();
},
),
],
);
},
);
},
child: Text('Add Drink'),
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _showAddDrinkDialog,
child: Icon(Icons.add),
backgroundColor: Colors.blue,
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.3.0"
intl:
dependency: "direct main"
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.1"
json_annotation:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ environment:
dependencies:
flutter:
sdk: flutter
intl: ^0.18.0 # Add this line



# The following adds the Cupertino Icons font to your application.
Expand Down

0 comments on commit ff49abc

Please sign in to comment.