Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.73 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4.  
  5. String tapMessage = "Tap";
  6.  
  7. Future<http.Response> fetchPost() async {
  8.   final response = await http.get('http://hrothgar32.pythonanywhere.com/');
  9.   print(response.statusCode);
  10.   tapMessage = response.body;
  11.   return response;
  12. }
  13.  
  14. void main() => runApp(MyApp());
  15.  
  16. class MyApp extends StatelessWidget {
  17.   @override
  18.   Widget build(BuildContext context) {
  19.     final title = 'Gesture Demo';
  20.  
  21.     return MaterialApp(
  22.       title: title,
  23.       home: MyHomePage(title: title),
  24.     );
  25.   }
  26. }
  27.  
  28. class MyHomePage extends StatelessWidget {
  29.   final String title;
  30.  
  31.   MyHomePage({Key key, this.title}) : super(key: key);
  32.  
  33.   @override
  34.   Widget build(BuildContext context) {
  35.     return Scaffold(
  36.       appBar: AppBar(
  37.         title: Text(title),
  38.       ),
  39.       body: Center(child: MyButton()),
  40.     );
  41.   }
  42. }
  43.  
  44. class MyButton extends StatelessWidget {
  45.   @override
  46.   Widget build(BuildContext context) {
  47.     // Our GestureDetector wraps our button
  48.     return GestureDetector(
  49.       // When the child is tapped, show a snackbar
  50.       onTap: () {
  51.         try{
  52.           Future<http.Response> response = fetchPost();
  53.           print("Yeah, boi!");
  54.         }catch(e){
  55.           print(e.toString());
  56.         }
  57.         final snackBar = SnackBar(content: Text(tapMessage));
  58.         Scaffold.of(context).showSnackBar(snackBar);
  59.       },
  60.       // Our Custom Button!
  61.       child: Container(
  62.         padding: EdgeInsets.all(12.0),
  63.         decoration: BoxDecoration(
  64.           color: Theme.of(context).buttonColor,
  65.           borderRadius: BorderRadius.circular(8.0),
  66.         ),
  67.         child: Text('My Button'),
  68.       ),
  69.     );
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement