fmassive/lib/home_page.dart

149 lines
4.0 KiB
Dart
Raw Normal View History

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