Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'package:url_launcher/url_launcher.dart';
- import 'dart:async';
- import 'dart:convert';
- Future<DataModels> fetchPost() async {
- final response =
- await http.get('https://jsonplaceholder.typicode.com/posts/1');
- if (response.statusCode == 200) {
- // If the call to the server was successful, parse the JSON.
- return DataModels.fromJson(json.decode(response.body));
- } else {
- // If that call was not successful, throw an error.
- throw Exception('Failed to load post');
- }
- }
- class Example extends StatefulWidget {
- @override
- _ExampleState createState() {
- return _ExampleState();
- }
- }
- class _ExampleState extends State<Example> {
- Future<DataModels> post;
- @override
- void initState() {
- super.initState();
- post = fetchPost();
- }
- @override
- Widget build(BuildContext context) {
- // bloc.Data();
- return Scaffold(
- appBar: AppBar(
- title: Text("Some project"),
- ),
- body: new Container(
- padding: EdgeInsets.all(15.0),
- height: double.infinity,
- child: FutureBuilder<DataModels>(
- future: post,
- builder: (context, AsyncSnapshot<DataModels> snapshot) {
- if (snapshot.hasData) {
- return _List(snapshot);
- } else if (snapshot.hasError) {
- return Text(snapshot.error.toString());
- }
- return Center(child: CircularProgressIndicator());
- },
- ),
- ));
- }
- Widget _List(AsyncSnapshot<DataModels> snapshot) {
- return Flex(
- direction: Axis.vertical,
- children: <Widget>[menuList(snapshot), blogList(snapshot)],
- );
- }
- Widget menuList(AsyncSnapshot<DataModels> snapshot) {
- return Expanded(
- child: Card(
- semanticContainer: false,
- clipBehavior: Clip.none,
- child: GridView.builder(
- // shrinkWrap: true,
- itemCount: 8,
- gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 3),
- itemBuilder: (BuildContext context, int index) {
- return Column(
- children: <Widget>[
- Expanded(
- child: Container(
- padding: EdgeInsets.all(10.0),
- child: Image.network(
- 'https://picsum.photos/100?image=$index',
- fit: BoxFit.cover,
- height: 35.0,
- ),
- ),
- ),
- Expanded(
- child: Container(
- alignment: Alignment.center,
- child: Text('${snapshot.data.title}'),
- ),
- )
- ],
- );
- },
- )));
- }
- Widget blogList(AsyncSnapshot<DataModels> snapshot) {
- return Expanded(
- child: ListView(
- // shrinkWrap: true,
- // semanticChildCount: 1,
- children: <Widget>[
- Column(
- //shrinkWrap: true,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Container(
- margin: EdgeInsets.only(top: 10.0, bottom: 5.0),
- child: Text('${snapshot.data.title}'),
- ),
- Container(
- child: ListView.builder(
- shrinkWrap: true,
- itemCount: 10,
- itemBuilder: (BuildContext context, int index) {
- return Container(
- margin: EdgeInsets.only(bottom: 10.0),
- child: GestureDetector(
- onTap: () => _launchURL(
- 'https://picsum.photos/300?image=$index'),
- child: Card(
- child: new Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- new Image.network(
- 'https://picsum.photos/100?image=$index'),
- new Padding(
- padding: new EdgeInsets.all(7.0),
- child: Text('${snapshot.data.body}'),
- )
- ],
- )),
- ),
- );
- }),
- )
- ],
- )
- ],
- ),
- );
- }
- _launchURL(String url) async {
- // final url = '$link';
- if (await canLaunch(url)) {
- await launch(url);
- } else {
- throw 'Could not launch $url';
- }
- }
- }
- class DataModels {
- final int userId;
- final int id;
- final String title;
- final String body;
- final String link;
- DataModels({this.userId, this.id, this.title, this.body, this.link});
- factory DataModels.fromJson(Map<String, dynamic> json) {
- return DataModels(
- userId: json['userId'],
- id: json['id'],
- title: json['title'],
- body: json['body'],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement