navigator push

This commit is contained in:
Valentin CZERYBA 2022-09-04 20:09:09 +02:00
parent 327b18049b
commit 595e63c4a7

View File

@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'MyHomePage.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
void main() {
runApp(MyApp());
}
@ -21,6 +25,21 @@ class LoginDemo extends StatefulWidget {
}
class _LoginDemoState extends State<LoginDemo> {
Future<void> _login() async {
var url = Uri.parse("http://localhost:8083/api/token");
String credentials = "peter93:toto";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);
var response = await http.get(url, headers: {
HttpHeaders.authorizationHeader: 'Basic $encoded',
});
if ((response.statusCode == 200) || (response.statusCode == 201)) {
Navigator.push(context,
MaterialPageRoute(builder: (_) => MyHomePage(title: 'Flutter Demo')));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -81,10 +100,7 @@ class _LoginDemoState extends State<LoginDemo> {
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
child: FlatButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo')));
_login();
},
child: Text(
'Login',