cpd/lib/pages/homepage.dart

220 lines
7.4 KiB
Dart

import 'package:flutter/cupertino.dart';
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';
import '../widgets/todo_widget.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final HabitDatabase database = HabitDatabase();
runApp(MyApp(database: database));
}
class MyApp extends StatelessWidget {
final HabitDatabase database;
const MyApp({super.key, required this.database});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'homepage',
theme: ThemeData(
useMaterial3: true,
),
home: MyHomePage(database: database, title: 'homepage'),
);
}
}
class MyHomePage extends StatefulWidget {
final HabitDatabase database;
final String title;
const MyHomePage({Key? key, required this.title, required this.database}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Habit> futureTodos;
final todoDB = TodoDB();
@override
void initState() {
super.initState();
fetchTodos();
}
void fetchTodos() async {
List<Habit>? habits = await todoDB.fetchAll();
setState(() {
futureTodos = habits;
});
}
@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: 'Arial',
fontSize: 14,
),
),
SizedBox(height: 10),
Text(
'You have #habitCounter for today',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontSize: 16,
),
),
],
),
),
],
),
),
),
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: <Widget>[
const Text(
'Keep going queen',
style: TextStyle(
fontSize: 16.0,
color: Colors.black,
fontFamily: 'Roboto',
),
),
const SizedBox(height: 8.0),
const ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(30)),
child: SizedBox(
width: double.infinity,
child: LinearProgressIndicator(
minHeight: 10,
color: Colors.green,
backgroundColor: Colors.white24,
value: 0.3, // Hier kannst du den Fortschritt des Indikators anpassen
),
),
),
const Padding(
padding: EdgeInsets.only(top: 10),
child: Divider(thickness: 3),
),
Expanded(
child: FutureBuilder<List<Habit>>(
future: futureTodos,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else {
final todos = snapshot.data ?? [];
return todos.isEmpty ? const Center(
child: Text(
'No habits',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
) : ListView.separated(
separatorBuilder: (context, index) => const SizedBox(height: 12),
itemCount: todos.length,
itemBuilder: (context, index) {
final habit = todos[index];
return ListTile(
title: Text(
habit.title,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(habit.subtitle ?? ''),
trailing: IconButton(
onPressed: () async {
await todoDB.delete(habit.id ?? 0);
fetchTodos();
},
icon: const Icon(Icons.delete, color: Colors.red),
),
onTap: () {
showDialog(
context: context,
builder: (context) => CreateTodoWidget(
todo: habit,
onSubmit: (title, subtitle) async {
await todoDB.update(id: habit.id, title: title);
fetchTodos();
if (!mounted) return;
Navigator.of(context).pop();
},
),
);
},
);
},
);
}
},
),
),
],
),
),
),
),
floatingActionButton: FloatingActionButton(
foregroundColor: Colors.deepPurpleAccent,
child: const Icon(Icons.add),
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();
},
),
);
},
),
);
}
}