cpd/lib/pages/homepage.dart

227 lines
8.2 KiB
Dart
Raw Permalink Normal View History

import 'package:cpd/widgets/listview.dart';
2024-05-09 09:46:23 +02:00
import 'package:flutter/material.dart';
import 'package:cpd/widgets/addhabit_popup.dart';
import '../database/db_interface.dart';
import '../database/habit.dart';
import '../database/todo_db.dart';
import '../database/todo_interface.dart';
2024-05-09 09:46:23 +02:00
class MyHomePage extends StatefulWidget {
final HabitDbInterface database;
2024-05-09 09:46:23 +02:00
final String title;
const MyHomePage({super.key, required this.title, required this.database});
2024-05-09 09:46:23 +02:00
@override
State<MyHomePage> createState() => MyHomePageState();
2024-05-09 09:46:23 +02:00
}
class MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
Future<List<Habit>>? futureTodos;
late ToDoInterface todoDB;
2024-06-19 16:25:05 +02:00
int counterCompleted = 0;
@override
void initState() {
super.initState();
todoDB = TodoDB();
fetchTodos();
}
//lädt alle Gewohnheiten aus der DB und aktualisiert den Zustand
void fetchTodos() async {
List<Habit> habits = await todoDB.fetchAll();
setState(() {
futureTodos = Future.value(habits);
counterCompleted = habits.where((habit) => habit.isComplete).length;
});
}
2024-05-09 09:46:23 +02:00
int countHabits(List<Habit> habits) {
return habits.length;
}
void updateCounter(int newCounterValue) {
setState(() {
counterCompleted = newCounterValue;
});
}
2024-05-09 09:46:23 +02:00
@override
Widget build(BuildContext context) {
2024-05-09 09:46:23 +02:00
return Scaffold(
appBar: AppBar(
toolbarHeight: 180,
flexibleSpace: ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(30),
),
child: Stack(
children: [
Image.asset(
'assets/appbar_background.png',
width: double.infinity,
fit: BoxFit.cover,
),
const Positioned(
2024-05-09 09:46:23 +02:00
top: 20,
left: 20,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
2024-05-09 09:46:23 +02:00
'step by step, day by day',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
fontSize: 15,
2024-05-09 09:46:23 +02:00
),
),
SizedBox(height: 10),
2024-05-09 09:46:23 +02:00
],
),
),
],
),
),
),
body: Padding(
2024-05-09 09:46:23 +02:00
padding: const EdgeInsets.only(top: 20),
child: Center(
child: Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.green[100],
borderRadius: const BorderRadius.all(Radius.circular(25)),
),
child: Column(
children: <Widget> [
const Text(
"Keep Going!",
style: TextStyle(color: Colors.black,
fontSize: 17,
fontFamily: "Arial"),
),
Expanded(child: FutureBuilder<List<Habit>>(
future: futureTodos,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.data == null || snapshot.data!.isEmpty) {
return const Text(
'No habits',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
);
} else {
final todos = snapshot.data;
final habitCount = countHabits(todos!);
//Fortschritt der abgeschlossenen Geowhnheiten
double progressValue = counterCompleted / habitCount;
return Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height * 0.04,
child: Stack(
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.85,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(10)),
child: AnimatedContainer(
duration: const Duration(seconds: 2),
curve: Curves.fastLinearToSlowEaseIn,
child: LinearProgressIndicator(
minHeight: 12,
color: Colors.green,
backgroundColor: Colors.white54,
value: progressValue,
),
),
),
),
Positioned(
top: 0,
right: 0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${(progressValue * 100).toInt()}%',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15,
),
),
),
),
],
),
),
const Padding(
padding: EdgeInsets.only(top: 0),
child: Divider(thickness: 3,
color: Colors.deepPurpleAccent),
),
Text(
'You have $habitCount habits for today',
style: const TextStyle(
color: Colors.black,
fontFamily: 'Playfair',
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
Expanded(child: MyListView(
habits: todos,
todoDB: todoDB,
fetchTodos: fetchTodos,
updateCounter: (completedCounter) {
setState(() {
counterCompleted = completedCounter;
});
},
),
),
]
);
}
},
),
)
],
2024-05-09 09:46:23 +02:00
),
),
),
2024-05-09 09:46:23 +02:00
),
2024-05-09 09:46:23 +02:00
floatingActionButton: FloatingActionButton(
foregroundColor: Colors.deepPurpleAccent,
child: const Icon(Icons.add),
2024-05-09 09:46:23 +02:00
onPressed: () {
if (!mounted) return;
//BuildContext currentContext = context;
2024-05-09 09:46:23 +02:00
showDialog(
context: context,
builder: (_) => AddHabitPopup(
onSubmit: (title, subtitle, icon) async {
await todoDB.insert(title: title, subtitle: subtitle, icon: icon);
if (!context.mounted) return;
fetchTodos();
Navigator.of(context).pop();
},
),
2024-05-09 09:46:23 +02:00
);
},
),
);
}
}