mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 10:33:54 +01:00
Add widget for rendering a workout day (not working ATM)
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class Muscle {
|
||||
class Day {
|
||||
final int id;
|
||||
final String description;
|
||||
final List<int> daysOfWeek;
|
||||
List<int> daysOfWeek = [];
|
||||
|
||||
Muscle({
|
||||
Day({
|
||||
@required this.id,
|
||||
@required this.description,
|
||||
@required this.daysOfWeek,
|
||||
this.daysOfWeek,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
|
||||
class WorkoutPlan with ChangeNotifier {
|
||||
class WorkoutPlan {
|
||||
final int id;
|
||||
final DateTime creationDate;
|
||||
final String description;
|
||||
DateTime creationDate;
|
||||
String description;
|
||||
List<Day> days = [];
|
||||
|
||||
WorkoutPlan({
|
||||
@required this.id,
|
||||
@required this.description,
|
||||
@required this.creationDate,
|
||||
this.id,
|
||||
this.creationDate,
|
||||
this.description,
|
||||
this.days,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/providers/auth.dart';
|
||||
import 'package:wger/providers/workout_plan.dart';
|
||||
|
||||
@@ -9,8 +10,8 @@ class WorkoutPlans with ChangeNotifier {
|
||||
static const workoutPlansUrl = '/api/v2/workout/';
|
||||
|
||||
String _url;
|
||||
List<WorkoutPlan> _entries = [];
|
||||
Auth _auth;
|
||||
List<WorkoutPlan> _entries = [];
|
||||
|
||||
WorkoutPlans(Auth auth, List<WorkoutPlan> entries) {
|
||||
this._auth = auth;
|
||||
@@ -54,6 +55,34 @@ class WorkoutPlans with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<WorkoutPlan> fetchAndSetFullWorkout(int workoutId) async {
|
||||
String url = _auth.serverUrl + '/api/v2/workout/$workoutId/canonical_representation/';
|
||||
|
||||
final response = await http.get(
|
||||
url,
|
||||
headers: <String, String>{'Authorization': 'Token ${_auth.token}'},
|
||||
);
|
||||
final extractedData = json.decode(response.body) as Map<String, dynamic>;
|
||||
|
||||
try {
|
||||
WorkoutPlan workout = _entries.firstWhere((element) => element.id == workoutId);
|
||||
|
||||
List<Day> days = [];
|
||||
for (final entry in extractedData['day_list']) {
|
||||
days.add(Day(
|
||||
id: entry['obj']['id'],
|
||||
description: entry['obj']['description'],
|
||||
));
|
||||
}
|
||||
workout.days = days;
|
||||
notifyListeners();
|
||||
|
||||
return workout;
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addProduct(WorkoutPlan product) async {
|
||||
final productsUrl =
|
||||
'https://flutter-shop-a2335.firebaseio.com/products.json?auth=${_auth.token}';
|
||||
@@ -82,8 +111,7 @@ class WorkoutPlans with ChangeNotifier {
|
||||
Future<void> updateProduct(String id, WorkoutPlan newProduct) async {
|
||||
final prodIndex = _entries.indexWhere((element) => element.id == id);
|
||||
if (prodIndex >= 0) {
|
||||
final url =
|
||||
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
|
||||
final url = 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
|
||||
await http.patch(url,
|
||||
body: json.encode({
|
||||
'description': newProduct.description,
|
||||
@@ -94,10 +122,8 @@ class WorkoutPlans with ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> deleteProduct(String id) async {
|
||||
final url =
|
||||
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
|
||||
final existingProductIndex =
|
||||
_entries.indexWhere((element) => element.id == id);
|
||||
final url = 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
|
||||
final existingProductIndex = _entries.indexWhere((element) => element.id == id);
|
||||
var existingProduct = _entries[existingProductIndex];
|
||||
_entries.removeAt(existingProductIndex);
|
||||
notifyListeners();
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/providers/workout_plan.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/widgets/app_drawer.dart';
|
||||
import 'package:wger/widgets/workout_plan_detail.dart';
|
||||
import 'package:wger/widgets/workouts/workout_plan_detail.dart';
|
||||
|
||||
class WorkoutPlanScreen extends StatefulWidget {
|
||||
static const routeName = '/workout-plan-detail';
|
||||
@@ -11,6 +13,12 @@ class WorkoutPlanScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _WorkoutPlanScreenState extends State<WorkoutPlanScreen> {
|
||||
Future<WorkoutPlan> _loadWorkoutPlanDetail(BuildContext context, int workoutId) async {
|
||||
var workout =
|
||||
await Provider.of<WorkoutPlans>(context, listen: false).fetchAndSetFullWorkout(workoutId);
|
||||
return workout;
|
||||
}
|
||||
|
||||
Widget getAppBar() {
|
||||
return AppBar(
|
||||
title: Text('Workout plan'),
|
||||
@@ -25,8 +33,8 @@ class _WorkoutPlanScreenState extends State<WorkoutPlanScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final workoutPlan =
|
||||
ModalRoute.of(context).settings.arguments as WorkoutPlan;
|
||||
final workoutPlan = ModalRoute.of(context).settings.arguments as WorkoutPlan;
|
||||
|
||||
return Scaffold(
|
||||
appBar: getAppBar(),
|
||||
drawer: AppDrawer(),
|
||||
@@ -34,7 +42,18 @@ class _WorkoutPlanScreenState extends State<WorkoutPlanScreen> {
|
||||
onPressed: () {},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
body: WorkoutPlansDetail(workoutPlan),
|
||||
body: FutureBuilder<WorkoutPlan>(
|
||||
future: _loadWorkoutPlanDetail(context, workoutPlan.id),
|
||||
builder: (context, AsyncSnapshot<WorkoutPlan> snapshot) =>
|
||||
snapshot.connectionState == ConnectionState.waiting
|
||||
? Center(child: CircularProgressIndicator())
|
||||
: RefreshIndicator(
|
||||
onRefresh: () => _loadWorkoutPlanDetail(context, workoutPlan.id),
|
||||
child: Consumer<WorkoutPlans>(
|
||||
builder: (context, workout, _) => WorkoutPlansDetail(snapshot.data),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/providers/workout_plan.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/widgets/app_drawer.dart';
|
||||
import 'package:wger/widgets/workout_plans_list.dart';
|
||||
import 'package:wger/widgets/workouts/workout_plans_list.dart';
|
||||
|
||||
class WorkoutPlansScreen extends StatefulWidget {
|
||||
static const routeName = '/workout-plans-list';
|
||||
@@ -12,7 +13,7 @@ class WorkoutPlansScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _WorkoutPlansScreenState extends State<WorkoutPlansScreen> {
|
||||
Future<void> _refreshWorkoutPlans(BuildContext context) async {
|
||||
Future<WorkoutPlan> _refreshWorkoutPlans(BuildContext context) async {
|
||||
await Provider.of<WorkoutPlans>(context, listen: false)
|
||||
.fetchAndSetWorkouts();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,6 @@ final ThemeData wgerTheme = ThemeData(
|
||||
textTheme: TextTheme(
|
||||
//headline1: TextStyle(fontSize: 30.0, fontFamily: "OpenSansBold"),
|
||||
headline2: TextStyle(fontSize: 30.0, fontFamily: "OpenSansBold"),
|
||||
headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
|
||||
//headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:wger/providers/workout_plan.dart';
|
||||
|
||||
class WorkoutPlansDetail extends StatelessWidget {
|
||||
WorkoutPlan _workoutPlan;
|
||||
|
||||
WorkoutPlansDetail(this._workoutPlan);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Text(
|
||||
'Workout ${DateFormat('dd.MM.yyyy').format(_workoutPlan.creationDate)}',
|
||||
style: Theme.of(context).textTheme.headline2,
|
||||
),
|
||||
),
|
||||
Divider()
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
32
lib/widgets/workouts/day.dart
Normal file
32
lib/widgets/workouts/day.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
|
||||
class WorkoutDayWidget extends StatelessWidget {
|
||||
Day _day;
|
||||
|
||||
WorkoutDayWidget(this._day);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print('xxxxxxxxxxxxxxxxx');
|
||||
print(_day);
|
||||
print(_day.description);
|
||||
print('xxxxxxxxxxxxxxxxx');
|
||||
Widget a = SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Text(
|
||||
'Day ${_day.id} - ${_day.description}',
|
||||
style: Theme.of(context).textTheme.headline6,
|
||||
),
|
||||
),
|
||||
Divider(),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
return Text(_day.description);
|
||||
}
|
||||
}
|
||||
48
lib/widgets/workouts/workout_plan_detail.dart
Normal file
48
lib/widgets/workouts/workout_plan_detail.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:wger/providers/workout_plan.dart';
|
||||
|
||||
class WorkoutPlansDetail extends StatelessWidget {
|
||||
WorkoutPlan _workoutPlan;
|
||||
WorkoutPlansDetail(this._workoutPlan);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
//print('yyyyyyyyyyyyyyyyyy');
|
||||
//print(_workoutPlan.description);
|
||||
//print(_workoutPlan.days);
|
||||
//print('yyyyyyyyyyyyyyyyyy');
|
||||
return Container(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Text(
|
||||
'Workout ${DateFormat('dd.MM.yyyy').format(_workoutPlan.creationDate)}',
|
||||
style: Theme.of(context).textTheme.headline6,
|
||||
),
|
||||
),
|
||||
Divider(),
|
||||
if (_workoutPlan.description != null)
|
||||
Text(
|
||||
_workoutPlan.description,
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
_workoutPlan.days != null
|
||||
? Container(
|
||||
child: ListView.builder(
|
||||
itemCount: _workoutPlan.days.length,
|
||||
itemBuilder: (context, index) {
|
||||
print(index);
|
||||
return Text("Day");
|
||||
//Day workoutDay = _workoutPlan.days[index];
|
||||
//return WorkoutDayWidget(workoutDay);
|
||||
},
|
||||
),
|
||||
)
|
||||
: Text('No days'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user