Advertisement
Guest User

Scrollingexample

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.34 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:http/http.dart' as http;
  3. import 'package:url_launcher/url_launcher.dart';
  4. import 'dart:async';
  5. import 'dart:convert';
  6.  
  7. Future<DataModels> fetchPost() async {
  8.   final response =
  9.       await http.get('https://jsonplaceholder.typicode.com/posts/1');
  10.  
  11.   if (response.statusCode == 200) {
  12.     // If the call to the server was successful, parse the JSON.
  13.     return DataModels.fromJson(json.decode(response.body));
  14.   } else {
  15.     // If that call was not successful, throw an error.
  16.     throw Exception('Failed to load post');
  17.   }
  18. }
  19.  
  20. class Example extends StatefulWidget {
  21.   @override
  22.   _ExampleState createState() {
  23.     return _ExampleState();
  24.   }
  25. }
  26.  
  27. class _ExampleState extends State<Example> {
  28.   Future<DataModels> post;
  29.  
  30.   @override
  31.   void initState() {
  32.     super.initState();
  33.     post = fetchPost();
  34.   }
  35.  
  36.   @override
  37.   Widget build(BuildContext context) {
  38.     // bloc.Data();
  39.     return Scaffold(
  40.         appBar: AppBar(
  41.           title: Text("Some project"),
  42.         ),
  43.         body: new Container(
  44.           padding: EdgeInsets.all(15.0),
  45.           height: double.infinity,
  46.           child: FutureBuilder<DataModels>(
  47.             future: post,
  48.             builder: (context, AsyncSnapshot<DataModels> snapshot) {
  49.               if (snapshot.hasData) {
  50.                 return _List(snapshot);
  51.               } else if (snapshot.hasError) {
  52.                 return Text(snapshot.error.toString());
  53.               }
  54.               return Center(child: CircularProgressIndicator());
  55.             },
  56.           ),
  57.         ));
  58.   }
  59.  
  60.   Widget _List(AsyncSnapshot<DataModels> snapshot) {
  61.     return Flex(
  62.       direction: Axis.vertical,
  63.       children: <Widget>[menuList(snapshot), blogList(snapshot)],
  64.     );
  65.   }
  66.  
  67.   Widget menuList(AsyncSnapshot<DataModels> snapshot) {
  68.     return Expanded(
  69.         child: Card(
  70.             semanticContainer: false,
  71.             clipBehavior: Clip.none,
  72.             child: GridView.builder(
  73.               // shrinkWrap: true,
  74.               itemCount: 8,
  75.               gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
  76.                   crossAxisCount: 3),
  77.               itemBuilder: (BuildContext context, int index) {
  78.                 return Column(
  79.                   children: <Widget>[
  80.                     Expanded(
  81.                       child: Container(
  82.                         padding: EdgeInsets.all(10.0),
  83.                         child: Image.network(
  84.                           'https://picsum.photos/100?image=$index',
  85.                           fit: BoxFit.cover,
  86.                           height: 35.0,
  87.                         ),
  88.                       ),
  89.                     ),
  90.                     Expanded(
  91.                       child: Container(
  92.                         alignment: Alignment.center,
  93.                         child: Text('${snapshot.data.title}'),
  94.                       ),
  95.                     )
  96.                   ],
  97.                 );
  98.               },
  99.             )));
  100.   }
  101.  
  102.   Widget blogList(AsyncSnapshot<DataModels> snapshot) {
  103.     return Expanded(
  104.       child: ListView(
  105.         // shrinkWrap: true,
  106.         // semanticChildCount: 1,
  107.         children: <Widget>[
  108.           Column(
  109.             //shrinkWrap: true,
  110.             crossAxisAlignment: CrossAxisAlignment.start,
  111.             children: <Widget>[
  112.               Container(
  113.                 margin: EdgeInsets.only(top: 10.0, bottom: 5.0),
  114.                 child: Text('${snapshot.data.title}'),
  115.               ),
  116.               Container(
  117.                 child: ListView.builder(
  118.                     shrinkWrap: true,
  119.                     itemCount: 10,
  120.                     itemBuilder: (BuildContext context, int index) {
  121.                       return Container(
  122.                         margin: EdgeInsets.only(bottom: 10.0),
  123.                         child: GestureDetector(
  124.                           onTap: () => _launchURL(
  125.                               'https://picsum.photos/300?image=$index'),
  126.                           child: Card(
  127.                               child: new Column(
  128.                             crossAxisAlignment: CrossAxisAlignment.start,
  129.                             children: <Widget>[
  130.                               new Image.network(
  131.                                   'https://picsum.photos/100?image=$index'),
  132.                               new Padding(
  133.                                 padding: new EdgeInsets.all(7.0),
  134.                                 child: Text('${snapshot.data.body}'),
  135.                               )
  136.                             ],
  137.                           )),
  138.                         ),
  139.                       );
  140.                     }),
  141.               )
  142.             ],
  143.           )
  144.         ],
  145.       ),
  146.     );
  147.   }
  148.  
  149.   _launchURL(String url) async {
  150.     // final url = '$link';
  151.     if (await canLaunch(url)) {
  152.       await launch(url);
  153.     } else {
  154.       throw 'Could not launch $url';
  155.     }
  156.   }
  157. }
  158.  
  159. class DataModels {
  160.   final int userId;
  161.   final int id;
  162.   final String title;
  163.   final String body;
  164.   final String link;
  165.  
  166.   DataModels({this.userId, this.id, this.title, this.body, this.link});
  167.  
  168.   factory DataModels.fromJson(Map<String, dynamic> json) {
  169.     return DataModels(
  170.       userId: json['userId'],
  171.       id: json['id'],
  172.       title: json['title'],
  173.       body: json['body'],
  174.     );
  175.   }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement