2023-04-07 11:50:02 +12:00
|
|
|
import 'package:flutter/material.dart';
|
2023-04-11 11:41:10 +12:00
|
|
|
import 'package:fmassive/best_page.dart';
|
|
|
|
import 'package:fmassive/database.dart';
|
2023-04-07 11:50:02 +12:00
|
|
|
import 'package:fmassive/edit_set.dart';
|
2023-04-11 11:41:10 +12:00
|
|
|
import 'package:fmassive/main.dart';
|
|
|
|
import 'package:fmassive/plans_page.dart';
|
|
|
|
import 'package:fmassive/settings_page.dart';
|
|
|
|
import 'package:fmassive/timer_page.dart';
|
|
|
|
import 'package:fmassive/workouts_page.dart';
|
|
|
|
import 'package:moor_flutter/moor_flutter.dart';
|
2023-04-07 11:50:02 +12:00
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
createState() => RootPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
class RootPage extends State<HomePage> {
|
|
|
|
bool showSearch = false;
|
|
|
|
int selected = 0;
|
|
|
|
String search = '';
|
|
|
|
final focusNode = FocusNode();
|
2023-04-07 11:50:02 +12:00
|
|
|
|
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},
|
|
|
|
];
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
void toggleSearch() {
|
|
|
|
setState(() {
|
|
|
|
showSearch = !showSearch;
|
|
|
|
});
|
|
|
|
focusNode.requestFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget getBody() {
|
|
|
|
switch (routes[selected]['title']) {
|
|
|
|
case 'Settings':
|
|
|
|
return const SettingsPage();
|
|
|
|
case 'Timer':
|
|
|
|
return const TimerPage();
|
|
|
|
case 'Workouts':
|
|
|
|
return const WorkoutsPage();
|
|
|
|
case 'Best':
|
|
|
|
return const BestPage();
|
|
|
|
case 'Plans':
|
|
|
|
return const PlansPage();
|
|
|
|
default:
|
|
|
|
return _HomePageWidget(search: search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:50:02 +12:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
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-11 11:41:10 +12:00
|
|
|
selected: selected == index,
|
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: () {
|
2023-04-11 11:41:10 +12:00
|
|
|
setState(() {
|
|
|
|
selected = index;
|
|
|
|
});
|
2023-04-07 11:50:02 +12:00
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-04-11 11:41:10 +12:00
|
|
|
appBar: AppBar(
|
|
|
|
title: showSearch
|
|
|
|
? TextField(
|
|
|
|
focusNode: focusNode,
|
|
|
|
cursorColor: Colors.white,
|
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {
|
|
|
|
search = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: 'Search...',
|
|
|
|
border: InputBorder.none,
|
|
|
|
hintStyle: TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: Text(routes[selected]['title']),
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
onPressed: toggleSearch,
|
|
|
|
),
|
|
|
|
],
|
2023-04-07 11:50:02 +12:00
|
|
|
),
|
2023-04-11 11:41:10 +12:00
|
|
|
body: getBody(),
|
2023-04-07 11:50:02 +12:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
class _HomePageWidget extends StatefulWidget {
|
|
|
|
final String search;
|
|
|
|
|
|
|
|
const _HomePageWidget({Key? key, required this.search}) : super(key: key);
|
2023-04-07 11:50:02 +12:00
|
|
|
|
|
|
|
@override
|
2023-04-11 11:41:10 +12:00
|
|
|
createState() => _HomePage();
|
2023-04-07 11:50:02 +12:00
|
|
|
}
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
class _HomePage extends State<_HomePageWidget> {
|
|
|
|
bool showSearch = false;
|
|
|
|
late Stream<List<GymSet>> stream;
|
2023-04-07 11:50:02 +12:00
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
|
|
|
setStream();
|
|
|
|
}
|
2023-04-07 11:50:02 +12:00
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
void setStream() {
|
|
|
|
stream = (db.select(db.gymSets)
|
|
|
|
..where((gymSet) => gymSet.name.contains(widget.search))
|
|
|
|
..limit(10, offset: 0))
|
|
|
|
.watch();
|
|
|
|
}
|
2023-04-07 11:50:02 +12:00
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
@override
|
|
|
|
didUpdateWidget(covariant _HomePageWidget oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
setStream();
|
2023-04-07 15:56:17 +12:00
|
|
|
}
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
void toggleSearch() {
|
2023-04-07 15:56:17 +12:00
|
|
|
setState(() {
|
2023-04-11 11:41:10 +12:00
|
|
|
showSearch = !showSearch;
|
2023-04-07 11:50:02 +12:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2023-04-11 11:41:10 +12:00
|
|
|
body: StreamBuilder<List<GymSet>>(
|
|
|
|
stream: stream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final gymSets = snapshot.data;
|
|
|
|
|
|
|
|
if (gymSets == null)
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
|
|
|
return ListView.builder(
|
|
|
|
itemCount: gymSets.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ListTile(
|
|
|
|
title: Text(gymSets[index].name),
|
|
|
|
onTap: () async {
|
|
|
|
await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => EditGymSetPage(
|
|
|
|
gymSet: gymSets[index].toCompanion(false)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
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(
|
2023-04-11 11:41:10 +12:00
|
|
|
gymSet: GymSetsCompanion(
|
|
|
|
name: const Value(''),
|
|
|
|
reps: const Value(0),
|
|
|
|
weight: const Value(0),
|
|
|
|
image: const Value(''),
|
|
|
|
created: Value(DateTime.now().toString()))),
|
2023-04-07 11:50:02 +12:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.add)));
|
|
|
|
}
|
|
|
|
}
|