2024-05-12 22:55:54 +02:00
|
|
|
import 'package:cpd/widgets/listview.dart';
|
2024-05-13 23:31:03 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-05-09 09:46:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-09 23:13:43 +02:00
|
|
|
import 'package:cpd/widgets/addhabit_popup.dart';
|
2024-05-12 00:08:54 +02:00
|
|
|
import '../database/habit.dart';
|
|
|
|
import 'package:cpd/database/db.dart';
|
|
|
|
import '../database/todo_db.dart';
|
2024-05-12 22:55:54 +02:00
|
|
|
|
2024-05-12 00:08:54 +02:00
|
|
|
void main() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
final HabitDatabase database = HabitDatabase();
|
|
|
|
runApp(MyApp(database: database));
|
2024-05-09 09:46:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
2024-05-12 00:08:54 +02:00
|
|
|
final HabitDatabase database;
|
|
|
|
const MyApp({super.key, required this.database});
|
2024-05-09 09:46:23 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2024-05-12 00:08:54 +02:00
|
|
|
title: 'homepage',
|
2024-05-09 09:46:23 +02:00
|
|
|
theme: ThemeData(
|
|
|
|
useMaterial3: true,
|
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
home: MyHomePage(database: database, title: 'homepage'),
|
2024-05-09 09:46:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
2024-05-12 00:08:54 +02:00
|
|
|
final HabitDatabase database;
|
2024-05-09 09:46:23 +02:00
|
|
|
final String title;
|
2024-05-13 23:31:03 +02:00
|
|
|
MyHomePage({super.key, required this.title, required this.database});
|
2024-05-09 09:46:23 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
2024-05-13 23:31:03 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
2024-05-12 17:56:36 +02:00
|
|
|
Future<List<Habit>>? futureTodos;
|
2024-05-12 00:08:54 +02:00
|
|
|
final todoDB = TodoDB();
|
2024-05-16 21:27:52 +02:00
|
|
|
int counterCompleted = 0;
|
2024-05-12 00:08:54 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
fetchTodos();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetchTodos() async {
|
|
|
|
List<Habit>? habits = await todoDB.fetchAll();
|
|
|
|
setState(() {
|
2024-05-12 17:56:36 +02:00
|
|
|
futureTodos = Future.value(habits);
|
2024-05-16 21:27:52 +02:00
|
|
|
counterCompleted = habits!.where((habit) => habit.isComplete).length;
|
2024-05-12 00:08:54 +02:00
|
|
|
});
|
|
|
|
}
|
2024-05-09 09:46:23 +02:00
|
|
|
|
2024-05-12 22:55:54 +02:00
|
|
|
int countHabits(List<Habit> habits) {
|
|
|
|
return habits.length;
|
|
|
|
}
|
|
|
|
|
2024-05-16 21:27:52 +02:00
|
|
|
void updateCounter(int newCounterValue) {
|
|
|
|
setState(() {
|
|
|
|
counterCompleted = newCounterValue;
|
|
|
|
});
|
|
|
|
}
|
2024-05-13 23:31:03 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-14 23:51:15 +02:00
|
|
|
//double progressValue = 0.0;
|
2024-05-13 23:31:03 +02:00
|
|
|
|
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,
|
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
const Positioned(
|
2024-05-09 09:46:23 +02:00
|
|
|
top: 20,
|
|
|
|
left: 20,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2024-05-14 23:51:15 +02:00
|
|
|
Text(
|
2024-05-09 09:46:23 +02:00
|
|
|
'step by step, day by day',
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontFamily: 'Arial',
|
2024-05-13 20:08:39 +02:00
|
|
|
fontSize: 15,
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
SizedBox(height: 10),
|
2024-05-09 09:46:23 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
body: Padding(
|
2024-05-09 09:46:23 +02:00
|
|
|
padding: const EdgeInsets.only(top: 20),
|
|
|
|
child: Center(
|
2024-05-12 00:08:54 +02:00
|
|
|
child: Container(
|
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.green[100],
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(25)),
|
|
|
|
),
|
|
|
|
child: Column(
|
2024-05-12 22:55:54 +02:00
|
|
|
children: <Widget> [
|
2024-05-13 23:31:03 +02:00
|
|
|
const Text(
|
|
|
|
"Keep Going!",
|
|
|
|
style: TextStyle(color: Colors.black, fontSize: 17, fontFamily: "Arial"),
|
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
|
|
|
|
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);
|
2024-05-16 21:27:52 +02:00
|
|
|
print("Anzahl der Habits: $habitCount");
|
|
|
|
|
|
|
|
double progressValue = counterCompleted / habitCount;
|
|
|
|
print("Progressvalue: $progressValue");
|
|
|
|
|
2024-05-14 23:51:15 +02:00
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.85,
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(10)),
|
|
|
|
child: LinearProgressIndicator(
|
|
|
|
minHeight: 12,
|
|
|
|
color: Colors.green,
|
|
|
|
backgroundColor: Colors.white54,
|
|
|
|
value: progressValue,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.only(top: 10),
|
|
|
|
child: Divider(thickness: 4),
|
|
|
|
),
|
|
|
|
|
|
|
|
Text(
|
|
|
|
'You have $habitCount habits for today',
|
|
|
|
style: const TextStyle(
|
|
|
|
color: Colors.black,
|
|
|
|
fontFamily: 'Roboto',
|
|
|
|
fontSize: 18,
|
2024-05-13 23:31:03 +02:00
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
MyListView(
|
|
|
|
habits: todos,
|
|
|
|
todoDB: todoDB,
|
|
|
|
fetchTodos: fetchTodos,
|
2024-05-16 21:27:52 +02:00
|
|
|
updateCounter: (completedCounter) {
|
|
|
|
setState(() {
|
|
|
|
counterCompleted = completedCounter;
|
|
|
|
});
|
|
|
|
},
|
2024-05-14 23:51:15 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2024-05-12 00:08:54 +02:00
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
)
|
2024-05-12 00:08:54 +02:00
|
|
|
],
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
|
|
|
),
|
2024-05-12 00:08:54 +02:00
|
|
|
),
|
2024-05-09 09:46:23 +02:00
|
|
|
),
|
2024-05-14 23:51:15 +02:00
|
|
|
|
2024-05-09 09:46:23 +02:00
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
foregroundColor: Colors.deepPurpleAccent,
|
2024-05-12 00:08:54 +02:00
|
|
|
child: const Icon(Icons.add),
|
2024-05-09 09:46:23 +02:00
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
2024-05-12 00:08:54 +02:00
|
|
|
builder: (_) => AddHabitPopup(
|
|
|
|
onSubmit: (title, subtitle) async {
|
|
|
|
await todoDB.create(title: title, subtitle: subtitle);
|
|
|
|
if (!mounted) return;
|
|
|
|
fetchTodos();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
2024-05-09 09:46:23 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-14 23:51:15 +02:00
|
|
|
}
|