Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:http/http.dart' as http;
- import 'dart:convert';
- import 'dart:math' as math;
- import 'package:flutter_html_view/flutter_html_view.dart';
- // import 'package:share/share.dart';
- import 'package:auto_size_text/auto_size_text.dart';
- import 'package:url_launcher/url_launcher.dart';
- import 'package:appolicoro/models/content.dart';
- import 'config.dart';
- class SingleContent extends StatefulWidget {
- final int id;
- final String title;
- final String type;
- SingleContent(
- {Key key, @required this.title, @required this.id, @required this.type})
- : super(key: key);
- @override
- _SingleContentFetchDataState createState() =>
- _SingleContentFetchDataState(this.title, this.id, this.type);
- }
- class _SingleContentFetchDataState extends State<SingleContent> {
- _SingleContentFetchDataState(this.title, this.id, this.type);
- Future<Content> page;
- Content content;
- final int id;
- final String title;
- final String type;
- @override
- void initState() {
- page = fetchItem();
- super.initState();
- }
- Future<Content> fetchItem() async {
- String queryPath;
- switch (type) {
- case "page":
- queryPath = "wp-json/wp/v2/pages/";
- break;
- case "post":
- queryPath = "wp-json/wp/v2/posts/";
- break;
- }
- final response = await http.get(Config.baseURL + queryPath + id.toString());
- if (response.statusCode == 200) {
- content = Content.fromJson(json.decode(response.body));
- return content;
- } else {
- return null;
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: FittedBox(
- child: Row(children: [
- Icon(Icons.info),
- Padding(padding: EdgeInsets.only(right: 5.0)),
- Text(this.title),
- ]),
- ),
- actions: [
- IconButton(
- icon: Icon(Icons.home),
- onPressed: () {
- Navigator.popUntil(context,
- ModalRoute.withName(Navigator.defaultRouteName));
- }),
- /*PopupMenuButton<String>(
- onSelected: _shareIt,
- itemBuilder: (_) => <PopupMenuItem<String>>[
- PopupMenuItem<String>(
- child: Text("Condividi"), value: "Condividi"),
- ]),*/
- ]),
- body: FutureBuilder<Content>(
- future: page,
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return CustomScrollView(slivers: <Widget>[
- SliverPersistentHeader(
- pinned: true,
- delegate: _SliverAppBarDelegate(
- minHeight: 70.0,
- maxHeight: 200.0,
- child: Stack(
- children:[
- Container(
- height: 200.0,
- decoration: new BoxDecoration(
- image: new DecorationImage(
- image: new NetworkImage(snapshot.data.imageURL),
- fit: BoxFit.cover,
- ),
- ),),
- Positioned(
- bottom: 0.0,
- left:0.0,
- child: Container(
- width: MediaQuery.of(context).size.width,
- child: Padding(
- padding: EdgeInsets.only(left: 10.0, right: 10.0),
- child: AutoSizeText(
- snapshot.data.name,
- maxLines: 2,
- style: TextStyle(
- shadows: <Shadow>[
- Shadow(
- offset: Offset(0.0, 0.0),
- blurRadius: 10.0,
- color: Color.fromARGB(255, 0, 0, 0),
- )
- ],
- fontWeight: FontWeight.bold,
- fontSize: Config.contentTitleSize,
- color: Colors.white,
- fontFamily: Config.font),
- ),
- ),
- ),
- )],
- ),
- ),
- ),
- SliverList(
- delegate: SliverChildBuilderDelegate(
- (BuildContext context, int index) {
- return Container(
- padding:
- EdgeInsets.all(Config.paddingBetweenThings),
- child: Column(
- children: [
- SizedBox(
- height: 10.0,
- ),
- Config.printHeader("Descrizione"),
- new HtmlView(
- data: snapshot.data.description,
- ),
- Container(
- child: snapshot.data.link == ""
- ? Container()
- : Padding(
- padding: EdgeInsetsDirectional.only(bottom: Config.paddingBetweenThings),
- child: FittedBox(
- child: RaisedButton(
- onPressed: () {
- _launchURL(snapshot.data.link);
- },
- child: Row(
- mainAxisAlignment:
- MainAxisAlignment.center,
- children: [
- Icon(
- Icons.link,
- // color: Config.mainColor,
- ),
- Padding(
- padding:
- EdgeInsets.only(right: 5.0)),
- Text(
- "Leggi di più",
- textAlign: TextAlign.center,
- ),
- ])),
- ),
- ),
- ),
- /*Html(
- data: snapshot.data.description,
- defaultTextStyle: TextStyle(
- fontFamily: Config.font,
- color: Config.contentTextColor)),*/
- ],
- ));
- },
- childCount: 1,
- ),
- )
- ]);
- } else
- return Center(
- child: CircularProgressIndicator(),
- );
- }));
- }
- /*void _shareIt(String selection) {
- Share.share(content.name + ": " + content.fullURL);
- }*/
- _launchURL(String theURL) async {
- if (await canLaunch(theURL))
- await launch(theURL);
- else {
- throw ("Impossibile accedere all'indirizzo $theURL");
- }
- }
- }
- class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
- _SliverAppBarDelegate({
- @required this.minHeight,
- @required this.maxHeight,
- @required this.child,
- });
- final double minHeight;
- final double maxHeight;
- final Widget child;
- @override
- double get minExtent => minHeight;
- @override
- double get maxExtent => math.max(maxHeight, minHeight);
- @override
- Widget build(
- BuildContext context, double shrinkOffset, bool overlapsContent) {
- return new SizedBox.expand(child: child);
- }
- @override
- bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
- return maxHeight != oldDelegate.maxHeight ||
- minHeight != oldDelegate.minHeight ||
- child != oldDelegate.child;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement