Compare commits

...

1 Commits

Author SHA1 Message Date
619907e3dc token speichern 2020-04-03 13:27:51 +02:00
11 changed files with 207 additions and 46 deletions

View File

@ -5,9 +5,10 @@ import 'dart:async';
import 'dart:convert';
class AddExercise extends StatefulWidget{
String token;
int workoutId;
AddExercise(this.workoutId);
AddExercise(this.token, this.workoutId);
@override
State<StatefulWidget> createState() {
@ -27,7 +28,7 @@ class AddExerciseState extends State<AddExercise>{
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 url = Uri.parse("http://10.16.17.18/api/add_exercise_to_workout.php?token=" + super.widget.token);
var request = http.MultipartRequest("post", url);
Map<String, String> test = {
"workout": this.widget.workoutId.toString(),

View File

@ -1,6 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CustomDrawer extends StatelessWidget{
void logout() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove("api_key");
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}
@override
Widget build(BuildContext context) {
// TODO: implement build
@ -40,7 +48,9 @@ class CustomDrawer extends StatelessWidget{
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('Logout'),
onTap: () => {Navigator.of(context).pop()},
onTap: () {
logout();
},
),
],
),

View File

@ -6,10 +6,11 @@ import 'dart:async';
import 'dart:convert';
class ExerciseView extends StatefulWidget{
String token;
String title;
String id;
ExerciseView(this.title, this.id);
ExerciseView(this.token, this.title, this.id);
@override
State<StatefulWidget> createState() {
@ -24,7 +25,7 @@ class ExerciseViewState extends State<ExerciseView>{
Future<List> getWorkouts() async {
var response = await http.get(
"http://10.16.17.18/api/workout.php?token=satbwertwhbertnwertwertghwertgwertg&workout="+this.widget.id,
"http://10.16.17.18/api/workout.php?token=" + super.widget.token + "&workout="+this.widget.id,
headers: {
"Accept": "application/json"
}
@ -75,7 +76,7 @@ class ExerciseViewState extends State<ExerciseView>{
floatingActionButton: FloatingActionButton(
onPressed: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => AddExercise(int.parse(this.widget.id))
builder: (context) => AddExercise(super.widget.token, int.parse(this.widget.id))
));
},
child: Icon(Icons.add),

View File

@ -2,7 +2,19 @@ import 'package:fitnessapp/Views/custom_drawer.dart';
import 'package:flutter/material.dart';
import 'workouts_view.dart';
class HomescreenView extends StatelessWidget{
class HomescreenView extends StatefulWidget{
String token;
HomescreenView(this.token);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return new HomescreenViewState();
}
}
class HomescreenViewState extends State<HomescreenView>{
@override
Widget build(BuildContext context) {
// TODO: implement build
@ -11,7 +23,7 @@ class HomescreenView extends StatelessWidget{
appBar: AppBar(
title: Text("Workouts"),
),
body: WorkoutsView(),
body: WorkoutsView(super.widget.token),
);
}
}

96
lib/Views/login_view.dart Normal file
View File

@ -0,0 +1,96 @@
import 'package:fitnessapp/Views/custom_drawer.dart';
import 'package:fitnessapp/Views/homescreen.dart';
import 'package:flutter/material.dart';
import 'workouts_view.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
class LoginView extends StatefulWidget{
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return new _LoginViewState();
}
}
class _LoginViewState extends State<LoginView>{
final usernameController = TextEditingController();
final passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
void _saveToken(String token) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("api_key", token);
String test = await prefs.getString("api_key");
print(test);
}
void _getToken() async {
var url = Uri.parse("http://10.16.17.18/api/login.php");
var request = http.MultipartRequest("post", url);
Map<String, String> test = {
"username": usernameController.value.text,
"password": passwordController.value.text
};
request.fields.addAll(test);
var response = await request.send();
if(response.statusCode == 200){
String text = await response.stream.bytesToString();
await _saveToken(text);
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomescreenView(text)
));
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Login"),
),
body: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: "username"
),
controller: usernameController,
validator: (value){
if(value.isEmpty){
return "bitte gib einen Nutzername ein";
}
},
),
TextFormField(
decoration: InputDecoration(
labelText: "password"
),
controller: passwordController,
validator: (value){
if(value.isEmpty){
return "bitte gib Passwort ein";
}
},
obscureText: true,
),
RaisedButton(
child: Text("Login"),
onPressed: (){
if(_formKey.currentState.validate()){
_getToken();
}
},
)
],
),
)
);
}
}

View File

@ -5,6 +5,10 @@ import 'dart:async';
import 'dart:convert';
class NewWorkoutView extends StatefulWidget{
String token;
NewWorkoutView(this.token);
@override
State<StatefulWidget> createState() {
return new NewWorkoutViewState();
@ -22,10 +26,7 @@ class NewWorkoutViewState extends State<NewWorkoutView>{
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 url = Uri.parse("http://10.16.17.18/api/new_workout.php?token=" + super.widget.token);
var request = http.MultipartRequest("post", url);
Map<String, String> test = {"name": name};
request.fields.addAll(test);
@ -38,7 +39,7 @@ class NewWorkoutViewState extends State<NewWorkoutView>{
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Füge eine Übung hinzu"),
title: Text("Neues Workout"),
),
body: Column(
mainAxisSize: MainAxisSize.max,
@ -46,7 +47,6 @@ class NewWorkoutViewState extends State<NewWorkoutView>{
Padding(
padding: EdgeInsets.all(8),
child: TextField(
keyboardType: TextInputType.number,
autocorrect: true,
decoration: InputDecoration(
border: OutlineInputBorder(),

View File

@ -6,6 +6,11 @@ import 'dart:async';
import 'dart:convert';
class WorkoutsView extends StatefulWidget{
String token;
WorkoutsView(this.token);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
@ -18,7 +23,7 @@ class WorkoutsViewState extends State<WorkoutsView>{
Future<List> getWorkouts() async {
var response = await http.get(
"http://10.16.17.18/api/index.php?token=satbwertwhbertnwertwertghwertgwertg",
"http://10.16.17.18/api/index.php?token=" + super.widget.token,
headers: {
"Accept": "application/json"
}
@ -49,7 +54,7 @@ class WorkoutsViewState extends State<WorkoutsView>{
subtitle: Text(data[index]['creator']),
onTap: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => ExerciseView(data[index]['title'], data[index]['id'])
builder: (context) => ExerciseView(super.widget.token, data[index]['title'], data[index]['id'])
));
},
);
@ -60,7 +65,7 @@ class WorkoutsViewState extends State<WorkoutsView>{
floatingActionButton: FloatingActionButton(
onPressed: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => NewWorkoutView()
builder: (context) => NewWorkoutView(super.widget.token)
));
},
child: Icon(Icons.add),

View File

@ -1,33 +1,34 @@
import 'package:fitnessapp/Views/homescreen.dart';
import 'package:fitnessapp/Views/login_view.dart';
import 'package:fitnessapp/Views/new_workout_view.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
String apiKey = await prefs.getString('api_key');
runApp(MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// Define the default brightness and colors.
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[800],
accentColor: Colors.cyan[600],
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// Define the default brightness and colors.
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[800],
accentColor: Colors.cyan[600],
// Define the default font family.
fontFamily: 'Verdana',
// Define the default font family.
fontFamily: 'Verdana',
// Define the default TextTheme. Use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: TextTheme(
headline: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
title: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
body1: TextStyle(fontSize: 14.0, fontFamily: 'Verdana'),
),
// Define the default TextTheme. Use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: TextTheme(
headline: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
title: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
body1: TextStyle(fontSize: 14.0, fontFamily: 'Verdana'),
),
home: HomescreenView(),
);
}
}
),
home: apiKey == null ? LoginView() : HomescreenView(apiKey),
)
);
}

View File

@ -81,6 +81,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
@ -129,7 +134,7 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
version: "1.9.0"
petitparser:
dependency: transitive
description:
@ -144,6 +149,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.6+3"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+6"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2+4"
sky_engine:
dependency: transitive
description: flutter
@ -214,3 +247,4 @@ packages:
version: "3.5.0"
sdks:
dart: ">2.4.0 <3.0.0"
flutter: ">=1.12.13+hotfix.4 <2.0.0"

View File

@ -25,6 +25,7 @@ dependencies:
cupertino_icons: ^0.1.2
http:
dio:
shared_preferences:
dev_dependencies:
flutter_test:

View File

@ -13,7 +13,7 @@ import 'package:fitnessapp/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
/*await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
@ -25,6 +25,6 @@ void main() {
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
expect(find.text('1'), findsOneWidget);*/
});
}