1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'package:dio/dio.dart';
- class TokenPage extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- return _TokenPageState();
- }
- }
- class _TokenPageState extends State<TokenPage> {
- TextEditingController usernameController = TextEditingController();
- TextEditingController passwordController = TextEditingController();
- Future<String> authorization() async {
- var request = http.MultipartRequest(
- 'GET', Uri.parse('http://localhost:8080/api/v1/projects'));
- request.fields.addAll({
- 'grant_type': 'info',
- 'client_id': 'info',
- 'username': usernameController.text,
- 'password': passwordController.text,
- });
- http.StreamedResponse response = await request.send();
- if (response.statusCode == 200) {
- Map<String, dynamic> auth =
- jsonDecode(await response.stream.bytesToString());
- return auth['access_token'];
- } else {
- return "";
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Login'),
- backgroundColor: Colors.green,
- ),
- body: Column(
- children: [
- Padding(
- padding: EdgeInsets.all(8.0),
- child: TextField(
- decoration: InputDecoration(labelText: 'username'),
- controller: usernameController,
- ),
- ),
- Padding(
- padding: EdgeInsets.all(8.0),
- child: TextField(
- decoration: InputDecoration(labelText: 'password'),
- controller: passwordController,
- ),
- ),
- ElevatedButton(
- onPressed: () async {
- String token = await authorization();
- print(token); //This will be your token.
- },
- child: Text('Login'),),
- ],
- ),
- );
- }
- }
|