Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:receive_sharing_intent/receive_sharing_intent.dart';
- import 'dart:async';
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class TokenResponse {
- final String token;
- TokenResponse({this.token});
- factory TokenResponse.fromJson(Map<String, dynamic> json) {
- return TokenResponse(token: json['token']);
- }
- }
- class _MyHomePageState extends State<MyHomePage> {
- StreamSubscription streamSubscription;
- String sharedText;
- @override
- void initState() {
- super.initState();
- // For sharing or opening urls/text coming from outside the app while the app is in the memory
- streamSubscription =
- ReceiveSharingIntent.getTextStream().listen((String value) {
- setState(() {
- sharedText = value;
- print("Shared: $sharedText");
- });
- }, onError: (err) {
- print("getLinkStream error: $err");
- });
- // For sharing or opening urls/text coming from outside the app while the app is closed
- ReceiveSharingIntent.getInitialText().then((String value) {
- setState(() {
- sharedText = value;
- print("Shared: $sharedText");
- });
- });
- }
- @override
- void dispose() {
- super.dispose();
- streamSubscription.cancel();
- }
- Future<TokenResponse> login(String username, String password) async {
- var client = http.Client();
- try {
- final response = await http.post('http://192.168.1.241:5000/tokens',
- headers: {"Content-type": "application/json"},
- body: jsonEncode(
- <String, String>{'username': username, 'password': password}));
- if (response.statusCode == 200) {
- return TokenResponse.fromJson(jsonDecode(response.body));
- } else {
- throw Exception('Couldn\'t log in');
- }
- } finally {
- client.close();
- }
- }
- void fetchSource() {
- final tokenFuture = login('nikolaenchev@gmail.com', 'password123');
- setState(() {
- tokenFuture.then((value) {
- debugPrint(value.token);
- sharedText = value.token;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text('$sharedText', style: Theme.of(context).textTheme.bodyText1),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: fetchSource,
- tooltip: 'Login',
- child: Icon(Icons.account_box),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement