Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- void main() => runApp(new MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- title: 'Flutter Demo',
- theme: new ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: new MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => new _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- Widget image;
- Future<String> getImageUrlFromSite() async {
- http.Response res = await http.get(
- "https://api.unsplash.com/photos/random?client_id=1bf67fa81b7d834ac542c7d80ba7f724858d870515e63bf8df91eae3cb022a97");
- String jsonString = res.body;
- dynamic jsonStruct = json.decode(jsonString);
- if (jsonStruct is Map) {
- Map<String, dynamic> data = jsonStruct;
- if (data.containsKey("urls") && data["urls"].containsKey("regular")) return data["urls"]["regular"];
- }
- return null;
- }
- void reMakeCounter() async {
- getImageUrlFromSite().then((String urlFromSite) {
- setState(() {
- image = new Image.network(urlFromSite);
- });
- });
- }
- @override
- void initState() {
- super.initState();
- reMakeCounter();
- }
- @override
- Widget build(BuildContext context) {
- Column imageColumn = new Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new Text(
- 'Awaiting for image...',
- ),
- ],
- );
- if (image != null) {
- imageColumn.children.removeLast();
- imageColumn.children.add(image);
- }
- return new Scaffold(
- appBar: new AppBar(
- title: new Text(widget.title),
- ),
- body: new Center(
- child: imageColumn,
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement