cpd/lib/pages/homepage.dart

210 lines
6.5 KiB
Dart
Raw Normal View History

import 'package:cpd/widgets/listview.dart';
import 'package:flutter/cupertino.dart';
2024-05-09 09:46:23 +02:00
import 'package:flutter/material.dart';
import 'package:cpd/widgets/addhabit_popup.dart';
import '../database/habit.dart';
import 'package:cpd/database/db.dart';
import '../database/todo_db.dart';
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 {
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(
title: 'homepage',
2024-05-09 09:46:23 +02:00
theme: ThemeData(
useMaterial3: true,
),
home: MyHomePage(database: database, title: 'homepage'),
2024-05-09 09:46:23 +02:00
);
}
}
class MyHomePage extends StatefulWidget {
final HabitDatabase database;
2024-05-09 09:46:23 +02:00
final String title;
int counterCompleted = 0;
MyHomePage({super.key, required this.title, required this.database});
2024-05-09 09:46:23 +02:00
@override
State<MyHomePage> createState() => _MyHomePageState();
void updateCounter(int newCounterValue) {
counterCompleted = newCounterValue;
}
2024-05-09 09:46:23 +02:00
}
class _MyHomePageState extends State<MyHomePage> {
Future<List<Habit>>? futureTodos;
final todoDB = TodoDB();
// int counterCompleted = 0;
@override
void initState() {
super.initState();
fetchTodos();
}
void fetchTodos() async {
List<Habit>? habits = await todoDB.fetchAll();
setState(() {
futureTodos = Future.value(habits);
});
}
2024-05-09 09:46:23 +02:00
int countHabits(List<Habit> habits) {
return habits.length;
}
/*int getCounterCompleted(){
return counterCompleted;
} */
2024-05-09 09:46:23 +02:00
@override
Widget build(BuildContext context) {
//double progressValue = 0.0;
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: 'Arial',
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);
double progressValue = widget.counterCompleted / habitCount;
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,
),
),
MyListView(
habits: todos,
todoDB: todoDB,
fetchTodos: fetchTodos,
updateCounter: widget.updateCounter,
),
],
);
}
},
),
)
],
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: () {
showDialog(
context: context,
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
);
},
),
);
}
}