diff --git a/lib/Views/add_exercise.dart b/lib/Views/add_exercise.dart new file mode 100644 index 0000000..f45c1a7 --- /dev/null +++ b/lib/Views/add_exercise.dart @@ -0,0 +1,126 @@ +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; +import 'dart:async'; +import 'dart:convert'; + +class AddExercise extends StatefulWidget{ + int workoutId; + + AddExercise(this.workoutId); + + @override + State createState() { + return new AddExerciseState(); + } +} + +class WorkoutType{ + String id, name, description, type, unit; + WorkoutType(this.id, this.name, this.description, this.type, this.unit); +} + +class AddExerciseState extends State{ + + int anzahl, sets, exerciseID = 0; + List data; + + void speichern(String id) async { + if(this.anzahl != 0){ + var url = Uri.parse("http://10.16.17.18/api/add_exercise_to_workout.php?token=satbwertwhbertnwertwertghwertgwertg"); + var request = http.MultipartRequest("post", url); + Map test = { + "workout": this.widget.workoutId.toString(), + "exercise": id, + "amount": anzahl.toString(), + "sets": sets.toString() + }; + print(test); + request.fields.addAll(test); + var response = await request.send(); + var test2 = await response.stream.bytesToString(); + print(test2); + Navigator.pop(context); + } + } + + Future getWorkouts() async { + var response = await http.get( + "http://10.16.17.18/api/exercises.php?token=satbwertwhbertnwertwertghwertgwertg", + headers: { + "Accept": "application/json" + } + ); + + this.setState(() { + data = JsonDecoder().convert(response.body); + }); + return data; + } + + @override + void initState() { + // TODO: implement initState + getWorkouts(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("Füge eine neue Übung hinzu"), + ), + body: Column( + mainAxisSize: MainAxisSize.max, + children: [ + Padding( + padding: EdgeInsets.all(8), + child: TextField( + autocorrect: true, + keyboardType: TextInputType.number, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: "anzahl" + ), + onChanged: (String s){ + setState(() { + anzahl = int.parse(s); + }); + }, + ), + ), + Padding( + padding: EdgeInsets.all(8), + child: TextField( + autocorrect: true, + keyboardType: TextInputType.number, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: "sets" + ), + onChanged: (String s){ + setState(() { + sets = int.parse(s); + }); + }, + ), + ), + Expanded( + child: ListView.builder( + itemCount: data== null? 0 : data.length, + itemBuilder: (BuildContext context, int index){ + return new ListTile( + title: Text(data[index]['name']), + subtitle: Text(data[index]['description']), + onTap: (){ + speichern(data[index]['id']); + }, + ); + }, + ), + ) + ], + ), + ); + } +} \ No newline at end of file diff --git a/lib/Views/exercises_view.dart b/lib/Views/exercises_view.dart index c7a3736..21b42ff 100644 --- a/lib/Views/exercises_view.dart +++ b/lib/Views/exercises_view.dart @@ -1,3 +1,4 @@ +import 'package:fitnessapp/Views/add_exercise.dart'; import 'package:fitnessapp/Views/exercise_details_view.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; @@ -23,7 +24,7 @@ class ExerciseViewState extends State{ Future getWorkouts() async { var response = await http.get( - "http://10.16.17.65/api/workout.php?token=satbwertwhbertnwertwertghwertgwertg&workout="+this.widget.id, + "http://10.16.17.18/api/workout.php?token=satbwertwhbertnwertwertghwertgwertg&workout="+this.widget.id, headers: { "Accept": "application/json" } @@ -53,21 +54,31 @@ class ExerciseViewState extends State{ ), title: Text(this.widget.title), ), - body: ListView.builder( - itemCount: data == null ? 0 : data.length, - itemBuilder: (BuildContext context, int index){ - return new ListTile( - leading: Icon(data[index]['type'] == 'duration' ? Icons.access_alarm : (data[index]['type'] == 'count' ? Icons.filter_1 : Icons.transfer_within_a_station)), - title: Text(data[index]['name']), - subtitle: Text(data[index]['amount'] + ' ' + data[index]['unit']), - trailing: Icon(Icons.add_box), - onTap: (){ - Navigator.push(context, MaterialPageRoute( - builder: (context) => ExerciseDetailsView(data[index]['name'], data[index]['description'], data[index]['amount'], data[index]['type'], data[index]['unit']) - )); - }, - ); + body: RefreshIndicator( + child: ListView.builder( + itemCount: data == null ? 0 : data.length, + itemBuilder: (BuildContext context, int index){ + return new ListTile( + leading: Icon(data[index]['type'] == 'duration' ? Icons.access_alarm : (data[index]['type'] == 'count' ? Icons.filter_1 : Icons.transfer_within_a_station)), + title: Text((index+1).toString() + ': ' + data[index]['name']), + subtitle: Text(data[index]['sets'] + ' x ' + data[index]['amount'] + ' ' + data[index]['unit']), + onTap: (){ + Navigator.push(context, MaterialPageRoute( + builder: (context) => ExerciseDetailsView(data[index]['name'], data[index]['description'], data[index]['amount'], data[index]['type'], data[index]['unit']) + )); + }, + ); + }, + ), + onRefresh: getWorkouts, + ), + floatingActionButton: FloatingActionButton( + onPressed: (){ + Navigator.push(context, MaterialPageRoute( + builder: (context) => AddExercise(int.parse(this.widget.id)) + )); }, + child: Icon(Icons.add), ), ); } diff --git a/lib/Views/new_workout_view.dart b/lib/Views/new_workout_view.dart index 935f4c5..ea23efc 100644 --- a/lib/Views/new_workout_view.dart +++ b/lib/Views/new_workout_view.dart @@ -1,3 +1,4 @@ +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:async'; @@ -16,42 +17,28 @@ class WorkoutType{ } class NewWorkoutViewState extends State{ - List data; - List selectedExercises = new List(); - String selectedExercisesString = "Derzeit ausgewählte übungen: "; - Future getWorkouts() async { - var response = await http.get( - "http://10.16.17.65/api/exercises.php?token=satbwertwhbertnwertwertghwertgwertg", - headers: { - "Accept": "application/json" - } - ); + String name = ""; - List results = JsonDecoder().convert(response.body); - List templist = []; - for(var i = 0; i < results.length; i++){ - templist.add(new WorkoutType(results[i]['id'], results[i]['name'], results[i]['description'], results[i]['type'], results[i]['unit'])); + void speichern() async { + if(this.name != ''){ + FormData data = new FormData.fromMap({ + "name": "Hallo Welt" + }); + var url = Uri.parse("http://10.16.17.18/api/new_workout.php?token=satbwertwhbertnwertwertghwertgwertg"); + var request = http.MultipartRequest("post", url); + Map test = {"name": name}; + request.fields.addAll(test); + var response = await request.send(); + Navigator.pop(context); } - - this.setState(() { - data = templist; - }); - print(data); - return data; - } - - @override - void initState() { - // TODO: implement initState - getWorkouts(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text("Erstelle ein neues Workout"), + title: Text("Füge eine Übung hinzu"), ), body: Column( mainAxisSize: MainAxisSize.max, @@ -59,39 +46,24 @@ class NewWorkoutViewState extends State{ Padding( padding: EdgeInsets.all(8), child: TextField( + keyboardType: TextInputType.number, autocorrect: true, decoration: InputDecoration( border: OutlineInputBorder(), labelText: "Name des Workouts" ), + onChanged: (String s){ + setState(() { + name = s; + }); + }, ), ), - Text(selectedExercisesString), - Expanded( - child: ListView.builder( - itemCount: data == null ? 0 : data.length, - itemBuilder: (BuildContext context, int index){ - return new ListTile( - title: Text(data[index].name), - subtitle: Text(data[index].description), - onTap: (){ - setState(() { - if(selectedExercises == null){ - selectedExercises = [data[index]]; - }else{ - selectedExercises.add(data[index]); - } - }); - String temp = "Ausgewählte übungen: "; - for (var i = 0; i < selectedExercises.length; i++){ - temp = temp + selectedExercises[i].name + ', '; - } - setState(() { - selectedExercisesString = temp; - }); - }, - ); - }, + Padding( + padding: EdgeInsets.all(8), + child: RaisedButton( + onPressed: speichern, + child: Text("Workout erstellen"), ), ) ], diff --git a/lib/Views/workouts_view.dart b/lib/Views/workouts_view.dart index b83d94e..b38d11e 100644 --- a/lib/Views/workouts_view.dart +++ b/lib/Views/workouts_view.dart @@ -18,7 +18,7 @@ class WorkoutsViewState extends State{ Future getWorkouts() async { var response = await http.get( - "http://10.16.17.65/api/index.php?token=satbwertwhbertnwertwertghwertgwertg", + "http://10.16.17.18/api/index.php?token=satbwertwhbertnwertwertghwertgwertg", headers: { "Accept": "application/json" } @@ -39,20 +39,23 @@ class WorkoutsViewState extends State{ Widget build(BuildContext context) { // TODO: implement build return Scaffold( - body: ListView.builder( - itemCount: data == null ? 0 : data.length, - itemBuilder: (BuildContext context, int index){ - return new ListTile( - leading: Icon(Icons.add), - title: Text(data[index]['title']), - subtitle: Text(data[index]['creator']), - onTap: (){ - Navigator.push(context, MaterialPageRoute( - builder: (context) => ExerciseView(data[index]['title'], data[index]['id']) - )); - }, - ); - }, + body: RefreshIndicator( + child: ListView.builder( + itemCount: data == null ? 0 : data.length, + itemBuilder: (BuildContext context, int index){ + return new ListTile( + leading: Icon(Icons.add), + title: Text(data[index]['title']), + subtitle: Text(data[index]['creator']), + onTap: (){ + Navigator.push(context, MaterialPageRoute( + builder: (context) => ExerciseView(data[index]['title'], data[index]['id']) + )); + }, + ); + }, + ), + onRefresh: getWorkouts, ), floatingActionButton: FloatingActionButton( onPressed: (){ diff --git a/pubspec.lock b/pubspec.lock index 63dbbc8..bcb4ebd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -64,6 +64,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3" + dio: + dependency: "direct main" + description: + name: dio + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.9" flutter: dependency: "direct main" description: flutter @@ -206,4 +213,4 @@ packages: source: hosted version: "3.5.0" sdks: - dart: ">=2.4.0 <3.0.0" + dart: ">2.4.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index a224d9f..639bae0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 http: + dio: dev_dependencies: flutter_test: