Advertisement
Guest User

Untitled

a guest
Mar 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 8.87 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'dart:convert';
  5. import 'dart:math' as math;
  6. import 'package:flutter_html_view/flutter_html_view.dart';
  7. // import 'package:share/share.dart';
  8. import 'package:auto_size_text/auto_size_text.dart';
  9. import 'package:url_launcher/url_launcher.dart';
  10.  
  11. import 'package:appolicoro/models/content.dart';
  12. import 'config.dart';
  13.  
  14. class SingleContent extends StatefulWidget {
  15.   final int id;
  16.   final String title;
  17.   final String type;
  18.  
  19.   SingleContent(
  20.       {Key key, @required this.title, @required this.id, @required this.type})
  21.       : super(key: key);
  22.  
  23.   @override
  24.   _SingleContentFetchDataState createState() =>
  25.       _SingleContentFetchDataState(this.title, this.id, this.type);
  26. }
  27.  
  28. class _SingleContentFetchDataState extends State<SingleContent> {
  29.   _SingleContentFetchDataState(this.title, this.id, this.type);
  30.  
  31.   Future<Content> page;
  32.   Content content;
  33.   final int id;
  34.   final String title;
  35.   final String type;
  36.  
  37.   @override
  38.   void initState() {
  39.     page = fetchItem();
  40.     super.initState();
  41.   }
  42.  
  43.   Future<Content> fetchItem() async {
  44.     String queryPath;
  45.     switch (type) {
  46.       case "page":
  47.         queryPath = "wp-json/wp/v2/pages/";
  48.         break;
  49.       case "post":
  50.         queryPath = "wp-json/wp/v2/posts/";
  51.         break;
  52.     }
  53.  
  54.     final response = await http.get(Config.baseURL + queryPath + id.toString());
  55.  
  56.     if (response.statusCode == 200) {
  57.       content = Content.fromJson(json.decode(response.body));
  58.       return content;
  59.     } else {
  60.       return null;
  61.     }
  62.   }
  63.  
  64.   @override
  65.   Widget build(BuildContext context) {
  66.     return Scaffold(
  67.         appBar: AppBar(
  68.             title: FittedBox(
  69.               child: Row(children: [
  70.                 Icon(Icons.info),
  71.                 Padding(padding: EdgeInsets.only(right: 5.0)),
  72.                 Text(this.title),
  73.               ]),
  74.             ),
  75.             actions: [
  76.               IconButton(
  77.                   icon: Icon(Icons.home),
  78.                   onPressed: () {
  79.                     Navigator.popUntil(context,
  80.                         ModalRoute.withName(Navigator.defaultRouteName));
  81.                   }),
  82.               /*PopupMenuButton<String>(
  83.                   onSelected: _shareIt,
  84.                   itemBuilder: (_) => <PopupMenuItem<String>>[
  85.                         PopupMenuItem<String>(
  86.                             child: Text("Condividi"), value: "Condividi"),
  87.                       ]),*/
  88.             ]),
  89.         body: FutureBuilder<Content>(
  90.             future: page,
  91.             builder: (context, snapshot) {
  92.               if (snapshot.hasData) {
  93.                 return CustomScrollView(slivers: <Widget>[
  94.                   SliverPersistentHeader(
  95.                     pinned: true,
  96.                     delegate: _SliverAppBarDelegate(
  97.                       minHeight: 70.0,
  98.                       maxHeight: 200.0,
  99.                       child: Stack(
  100.                           children:[
  101.                                               Container(
  102.                                                 height: 200.0,
  103.                           decoration: new BoxDecoration(
  104.                             image: new DecorationImage(
  105.                               image: new NetworkImage(snapshot.data.imageURL),
  106.                               fit: BoxFit.cover,
  107.                             ),
  108.                           ),),
  109.                           Positioned(
  110.                               bottom: 0.0,
  111.                               left:0.0,
  112.                                                       child: Container(
  113.                               width: MediaQuery.of(context).size.width,
  114.                               child: Padding(
  115.                                 padding: EdgeInsets.only(left: 10.0, right: 10.0),
  116.                                                               child: AutoSizeText(
  117.                                   snapshot.data.name,
  118.                                   maxLines: 2,
  119.                                   style: TextStyle(
  120.                                       shadows: <Shadow>[
  121.                                         Shadow(
  122.                                           offset: Offset(0.0, 0.0),
  123.                                           blurRadius: 10.0,
  124.                                           color: Color.fromARGB(255, 0, 0, 0),
  125.                                         )
  126.                                       ],
  127.                                       fontWeight: FontWeight.bold,
  128.                                       fontSize: Config.contentTitleSize,
  129.                                       color: Colors.white,
  130.                                       fontFamily: Config.font),
  131.                                 ),
  132.                               ),
  133.                           ),
  134.                         )],
  135.                       ),
  136.                     ),
  137.                   ),
  138.                   SliverList(
  139.                     delegate: SliverChildBuilderDelegate(
  140.                       (BuildContext context, int index) {
  141.                         return Container(
  142.                             padding:
  143.                                 EdgeInsets.all(Config.paddingBetweenThings),
  144.                             child: Column(
  145.                               children: [
  146.                                 SizedBox(
  147.                                   height: 10.0,
  148.                                 ),
  149.                                 Config.printHeader("Descrizione"),
  150.                                 new HtmlView(
  151.                                   data: snapshot.data.description,
  152.                                 ),
  153.                                                     Container(
  154.                       child: snapshot.data.link == ""
  155.                           ? Container()
  156.                           : Padding(
  157.                               padding: EdgeInsetsDirectional.only(bottom: Config.paddingBetweenThings),
  158.                               child: FittedBox(
  159.                                 child: RaisedButton(
  160.                                     onPressed: () {
  161.                                       _launchURL(snapshot.data.link);
  162.                                     },
  163.                                     child: Row(
  164.                                         mainAxisAlignment:
  165.                                             MainAxisAlignment.center,
  166.                                         children: [
  167.                                           Icon(
  168.                                             Icons.link,
  169.                                             // color: Config.mainColor,
  170.                                           ),
  171.                                           Padding(
  172.                                               padding:
  173.                                                   EdgeInsets.only(right: 5.0)),
  174.                                           Text(
  175.                                             "Leggi di più",
  176.                                             textAlign: TextAlign.center,
  177.                                           ),
  178.                                         ])),
  179.                               ),
  180.                             ),
  181.                     ),
  182.                                 /*Html(
  183.                                 data: snapshot.data.description,
  184.                                 defaultTextStyle: TextStyle(
  185.                                     fontFamily: Config.font,
  186.                                     color: Config.contentTextColor)),*/
  187.                               ],
  188.                             ));
  189.                       },
  190.                       childCount: 1,
  191.                     ),
  192.                   )
  193.                 ]);
  194.               } else
  195.                 return Center(
  196.                   child: CircularProgressIndicator(),
  197.                 );
  198.             }));
  199.   }
  200.  
  201.   /*void _shareIt(String selection) {
  202.     Share.share(content.name + ": " + content.fullURL);
  203.   }*/
  204.  
  205.   _launchURL(String theURL) async {
  206.     if (await canLaunch(theURL))
  207.       await launch(theURL);
  208.     else {
  209.       throw ("Impossibile accedere all'indirizzo $theURL");
  210.     }
  211.   }
  212.  
  213. }
  214.  
  215. class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  216.   _SliverAppBarDelegate({
  217.     @required this.minHeight,
  218.     @required this.maxHeight,
  219.     @required this.child,
  220.   });
  221.   final double minHeight;
  222.   final double maxHeight;
  223.   final Widget child;
  224.   @override
  225.   double get minExtent => minHeight;
  226.   @override
  227.   double get maxExtent => math.max(maxHeight, minHeight);
  228.   @override
  229.   Widget build(
  230.       BuildContext context, double shrinkOffset, bool overlapsContent) {
  231.     return new SizedBox.expand(child: child);
  232.   }
  233.  
  234.   @override
  235.   bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
  236.     return maxHeight != oldDelegate.maxHeight ||
  237.         minHeight != oldDelegate.minHeight ||
  238.         child != oldDelegate.child;
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement