import 'package:cpd/widgets/listview.dart'; 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'; class MyHomePage extends StatefulWidget { final HabitDbInterface database; final String title; const MyHomePage({super.key, required this.title, required this.database}); @override State createState() => MyHomePageState(); } class MyHomePageState extends State with SingleTickerProviderStateMixin { Future>? futureTodos; late ToDoInterface todoDB; 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 habits = await todoDB.fetchAll(); setState(() { futureTodos = Future.value(habits); counterCompleted = habits.where((habit) => habit.isComplete).length; }); } int countHabits(List habits) { return habits.length; } void updateCounter(int newCounterValue) { setState(() { counterCompleted = newCounterValue; }); } @override Widget build(BuildContext context) { 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( top: 20, left: 20, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'step by step, day by day', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontFamily: 'Roboto', fontSize: 15, ), ), SizedBox(height: 10), ], ), ), ], ), ), ), body: Padding( 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: [ const Text( "Keep Going!", style: TextStyle(color: Colors.black, fontSize: 17, fontFamily: "Arial"), ), Expanded(child: FutureBuilder>( 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; }); }, ), ), ] ); } }, ), ) ], ), ), ), ), floatingActionButton: FloatingActionButton( foregroundColor: Colors.deepPurpleAccent, child: const Icon(Icons.add), onPressed: () { if (!mounted) return; //BuildContext currentContext = context; 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(); }, ), ); }, ), ); } }