fmassive/lib/home_page.dart

149 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:fmassive/edit_set.dart';
import 'package:fmassive/gym_set.dart';
class HomePage extends StatelessWidget {
HomePage({super.key});
final List<Map<String, dynamic>> routes = [
{'title': 'Home', 'icon': Icons.home},
{'title': 'Plans', 'icon': Icons.calendar_today},
{'title': 'Best', 'icon': Icons.star},
{'title': 'Workouts', 'icon': Icons.fitness_center},
{'title': 'Timer', 'icon': Icons.timer},
{'title': 'Settings', 'icon': Icons.settings},
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(routes[0]['title']),
),
drawer: Drawer(
child: ListView.builder(
itemCount: routes.length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(routes[index]['icon']),
title: Text(routes[index]['title']),
onTap: () {
Navigator.pop(context);
Navigator.pushNamed(
context, '/${routes[index]['title'].toLowerCase()}');
},
);
},
),
),
body: const Center(
child: GymSetPage(),
),
);
}
}
class GymSetPage extends StatefulWidget {
const GymSetPage({Key? key}) : super(key: key);
@override
createState() => GymSetPageState();
}
class GymSetPageState extends State<GymSetPage> {
late GymSetDatabaseHelper db;
List<GymSet> sets = [];
List<GymSet> searchResults = [];
final TextEditingController searchController = TextEditingController();
void search(String searchQuery) {
List<GymSet> results = [];
if (searchQuery.isEmpty) {
results = sets;
} else {
for (int i = 0; i < sets.length; i++) {
if (sets[i].reps.toString().contains(searchQuery) ||
sets[i].weight.toString().contains(searchQuery) ||
sets[i].created.toString().contains(searchQuery) ||
sets[i].name.contains(searchQuery)) {
results.add(sets[i]);
}
}
}
setState(() {
searchResults = results;
});
}
void reset() async {
print('Resetting...');
final data = await db.getAll();
setState(() {
sets = data;
searchResults = data;
});
}
@override
void initState() {
super.initState();
db = GymSetDatabaseHelper.instance;
reset();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
controller: searchController,
decoration: const InputDecoration(
hintText: 'Search Gym Sets',
border: InputBorder.none,
),
onChanged: (searchQuery) {
search(searchQuery);
},
),
),
body: ListView.builder(
itemCount: searchResults.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
'${searchResults[index].name}: ${searchResults[index].reps}x${searchResults[index].weight}kg'),
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
EditGymSetPage(gymSet: searchResults[index]),
),
);
reset();
});
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditGymSetPage(
gymSet: GymSet(
name: '',
reps: 0,
weight: 0,
created: DateTime.now())),
),
);
reset();
},
child: const Icon(Icons.add)));
}
}