token.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:dio/dio.dart';
  5. class TokenPage extends StatefulWidget {
  6. @override
  7. State<StatefulWidget> createState() {
  8. return _TokenPageState();
  9. }
  10. }
  11. class _TokenPageState extends State<TokenPage> {
  12. TextEditingController usernameController = TextEditingController();
  13. TextEditingController passwordController = TextEditingController();
  14. Future<String> authorization() async {
  15. var request = http.MultipartRequest(
  16. 'GET', Uri.parse('http://localhost:8080/api/v1/projects'));
  17. request.fields.addAll({
  18. 'grant_type': 'info',
  19. 'client_id': 'info',
  20. 'username': usernameController.text,
  21. 'password': passwordController.text,
  22. });
  23. http.StreamedResponse response = await request.send();
  24. if (response.statusCode == 200) {
  25. Map<String, dynamic> auth =
  26. jsonDecode(await response.stream.bytesToString());
  27. return auth['access_token'];
  28. } else {
  29. return "";
  30. }
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. title: Text('Login'),
  37. backgroundColor: Colors.green,
  38. ),
  39. body: Column(
  40. children: [
  41. Padding(
  42. padding: EdgeInsets.all(8.0),
  43. child: TextField(
  44. decoration: InputDecoration(labelText: 'username'),
  45. controller: usernameController,
  46. ),
  47. ),
  48. Padding(
  49. padding: EdgeInsets.all(8.0),
  50. child: TextField(
  51. decoration: InputDecoration(labelText: 'password'),
  52. controller: passwordController,
  53. ),
  54. ),
  55. ElevatedButton(
  56. onPressed: () async {
  57. String token = await authorization();
  58. print(token); //This will be your token.
  59. },
  60. child: Text('Login'),),
  61. ],
  62. ),
  63. );
  64. }
  65. }