217 lines
6.9 KiB
Dart
217 lines
6.9 KiB
Dart
import 'package:cpd/widgets/listview.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:cpd/widgets/addhabit_popup.dart';
|
|
import '../database/db_interface.dart';
|
|
import '../database/habit.dart';
|
|
import 'package:cpd/database/db.dart';
|
|
import '../database/todo_db.dart';
|
|
import '../database/todo_interface.dart';
|
|
|
|
void main() async {
|
|
//WidgetsFlutterBinding.ensureInitialized();
|
|
//final HabitDatabase database = HabitDatabase();
|
|
final database = HabitDatabase();
|
|
runApp(MyApp(database: database));
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
final HabitDbInterface 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 HabitDbInterface database;
|
|
final String title;
|
|
const MyHomePage({super.key, required this.title, required this.database});
|
|
|
|
@override
|
|
State<MyHomePage> createState() => MyHomePageState();
|
|
}
|
|
|
|
class MyHomePageState extends State<MyHomePage> {
|
|
Future<List<Habit>>? futureTodos;
|
|
late ToDoInterface todoDB;
|
|
int counterCompleted = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
todoDB = TodoDB();
|
|
fetchTodos();
|
|
}
|
|
|
|
void fetchTodos() async {
|
|
List<Habit> habits = await todoDB.fetchAll();
|
|
setState(() {
|
|
futureTodos = Future.value(habits);
|
|
counterCompleted = habits.where((habit) => habit.isComplete).length;
|
|
});
|
|
}
|
|
|
|
int countHabits(List<Habit> habits) {
|
|
return habits.length;
|
|
}
|
|
|
|
void updateCounter(int newCounterValue) {
|
|
setState(() {
|
|
counterCompleted = newCounterValue;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
//double progressValue = 0.0;
|
|
|
|
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: 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: <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 = 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,
|
|
),
|
|
),
|
|
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: () {
|
|
showDialog(
|
|
context: context,
|
|
builder: (_) => AddHabitPopup(
|
|
onSubmit: (title, subtitle, icon) async {
|
|
await todoDB.create(title: title, subtitle: subtitle, icon: icon);
|
|
if (!mounted) return;
|
|
fetchTodos();
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
} |