Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- String tapMessage = "Tap";
- Future<http.Response> fetchPost() async {
- final response = await http.get('http://hrothgar32.pythonanywhere.com/');
- print(response.statusCode);
- tapMessage = response.body;
- return response;
- }
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- final title = 'Gesture Demo';
- return MaterialApp(
- title: title,
- home: MyHomePage(title: title),
- );
- }
- }
- class MyHomePage extends StatelessWidget {
- final String title;
- MyHomePage({Key key, this.title}) : super(key: key);
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(title),
- ),
- body: Center(child: MyButton()),
- );
- }
- }
- class MyButton extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- // Our GestureDetector wraps our button
- return GestureDetector(
- // When the child is tapped, show a snackbar
- onTap: () {
- try{
- Future<http.Response> response = fetchPost();
- print("Yeah, boi!");
- }catch(e){
- print(e.toString());
- }
- final snackBar = SnackBar(content: Text(tapMessage));
- Scaffold.of(context).showSnackBar(snackBar);
- },
- // Our Custom Button!
- child: Container(
- padding: EdgeInsets.all(12.0),
- decoration: BoxDecoration(
- color: Theme.of(context).buttonColor,
- borderRadius: BorderRadius.circular(8.0),
- ),
- child: Text('My Button'),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement