Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.26 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:receive_sharing_intent/receive_sharing_intent.dart';
  3. import 'dart:async';
  4. import 'dart:convert';
  5. import 'package:http/http.dart' as http;
  6.  
  7. void main() {
  8.   runApp(MyApp());
  9. }
  10.  
  11. class MyApp extends StatelessWidget {
  12.   @override
  13.   Widget build(BuildContext context) {
  14.     return MaterialApp(
  15.       title: 'Flutter Demo',
  16.       theme: ThemeData(
  17.         primarySwatch: Colors.blue,
  18.       ),
  19.       home: MyHomePage(title: 'Flutter Demo Home Page'),
  20.     );
  21.   }
  22. }
  23.  
  24. class MyHomePage extends StatefulWidget {
  25.   MyHomePage({Key key, this.title}) : super(key: key);
  26.  
  27.   final String title;
  28.  
  29.   @override
  30.   _MyHomePageState createState() => _MyHomePageState();
  31. }
  32.  
  33. class TokenResponse {
  34.   final String token;
  35.  
  36.   TokenResponse({this.token});
  37.  
  38.   factory TokenResponse.fromJson(Map<String, dynamic> json) {
  39.     return TokenResponse(token: json['token']);
  40.   }
  41. }
  42.  
  43. class _MyHomePageState extends State<MyHomePage> {
  44.   StreamSubscription streamSubscription;
  45.   String sharedText;
  46.  
  47.   @override
  48.   void initState() {
  49.     super.initState();
  50.     // For sharing or opening urls/text coming from outside the app while the app is in the memory
  51.     streamSubscription =
  52.         ReceiveSharingIntent.getTextStream().listen((String value) {
  53.       setState(() {
  54.         sharedText = value;
  55.         print("Shared: $sharedText");
  56.       });
  57.     }, onError: (err) {
  58.       print("getLinkStream error: $err");
  59.     });
  60.  
  61.     // For sharing or opening urls/text coming from outside the app while the app is closed
  62.     ReceiveSharingIntent.getInitialText().then((String value) {
  63.       setState(() {
  64.         sharedText = value;
  65.         print("Shared: $sharedText");
  66.       });
  67.     });
  68.   }
  69.  
  70.   @override
  71.   void dispose() {
  72.     super.dispose();
  73.     streamSubscription.cancel();
  74.   }
  75.  
  76.   Future<TokenResponse> login(String username, String password) async {
  77.     var client = http.Client();
  78.     try {
  79.       final response = await http.post('http://192.168.1.241:5000/tokens',
  80.           headers: {"Content-type": "application/json"},
  81.           body: jsonEncode(
  82.               <String, String>{'username': username, 'password': password}));
  83.  
  84.       if (response.statusCode == 200) {
  85.         return TokenResponse.fromJson(jsonDecode(response.body));
  86.       } else {
  87.         throw Exception('Couldn\'t log in');
  88.       }
  89.     } finally {
  90.       client.close();
  91.     }
  92.   }
  93.  
  94.   void fetchSource() {
  95.     final tokenFuture = login('nikolaenchev@gmail.com', 'password123');
  96.     setState(() {
  97.       tokenFuture.then((value) {
  98.         debugPrint(value.token);
  99.         sharedText = value.token;
  100.       });
  101.     });
  102.   }
  103.  
  104.   @override
  105.   Widget build(BuildContext context) {
  106.     return Scaffold(
  107.       appBar: AppBar(
  108.         title: Text(widget.title),
  109.       ),
  110.       body: Center(
  111.         child: Column(
  112.           mainAxisAlignment: MainAxisAlignment.center,
  113.           children: <Widget>[
  114.             Text('$sharedText', style: Theme.of(context).textTheme.bodyText1),
  115.           ],
  116.         ),
  117.       ),
  118.       floatingActionButton: FloatingActionButton(
  119.         onPressed: fetchSource,
  120.         tooltip: 'Login',
  121.         child: Icon(Icons.account_box),
  122.       ), // This trailing comma makes auto-formatting nicer for build methods.
  123.     );
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement