Adding Bolg list

parent a94b6000
import 'package:flutter/material.dart';
import 'package:webfeed/webfeed.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
class BlogWidget extends StatefulWidget {
//
BlogWidget() : super();
final String title = 'RSS Feed Blog Widget';
@override
BlogWidgetState createState() => BlogWidgetState();
}
class BlogWidgetState extends State<BlogWidget> {
//
static const String FEED_URL =
'https://www.europe1.fr/rss/sciences.xml';
RssFeed _feed;
String _title;
static const String loadingFeedMsg = 'Loading Feed...';
static const String feedLoadErrorMsg = 'Error Loading Feed.';
static const String feedOpenErrorMsg = 'Error Opening Feed.';
GlobalKey<RefreshIndicatorState> _refreshKey;
updateTitle(title) {
setState(() {
_title = title;
});
}
updateFeed(feed) {
setState(() {
_feed = feed;
});
}
Future<void> openFeed(String url) async {
if (await canLaunch(url)) {
await launch(
url,
forceSafariVC: true,
forceWebView: false,
);
return;
}
updateTitle(feedOpenErrorMsg);
}
load() async {
updateTitle(loadingFeedMsg);
loadFeed().then((result) {
if (null == result || result.toString().isEmpty) {
updateTitle(feedLoadErrorMsg);
return;
}
updateFeed(result);
updateTitle(_feed.title);
});
}
Future<RssFeed> loadFeed() async {
try {
final client = http.Client();
final response = await client.get(FEED_URL);
return RssFeed.parse(response.body);
} catch (e) {
//
}
return null;
}
@override
void initState() {
super.initState();
_refreshKey = GlobalKey<RefreshIndicatorState>();
updateTitle(widget.title);
load();
}
title(title) {
return Text(
title,
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w500),
maxLines: 2,
overflow: TextOverflow.ellipsis,
);
}
rightIcon() {
return Icon(
Icons.keyboard_arrow_right,
color: Colors.grey,
size: 30.0,
);
}
list() {
return ListView.builder(
itemCount: _feed.items.length,
itemBuilder: (BuildContext context, int index) {
final item = _feed.items[index];
return ListTile(
title: title(item.title),
trailing: rightIcon(),
contentPadding: EdgeInsets.all(5.0),
onTap: () => openFeed(item.link),
);
},
);
}
isFeedEmpty() {
return null == _feed || null == _feed.items;
}
body() {
return isFeedEmpty()
? Center(
child: CircularProgressIndicator(),
)
: RefreshIndicator(
key: _refreshKey,
child: list(),
onRefresh: () => load(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_title),
),
body: body(),
);
}
}
/*
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:webfeed/domain/rss_feed.dart';
import 'package:webfeed/webfeed.dart';
import 'dart:io';
//var rssFeed = new RssFeed.parse(xmlString); // for parsing RSS feed
class NewsModel {
final String title;
final String description;
NewsModel({this.title, this.description});
}
class BlogWidget extends StatelessWidget {
static String rssUrl = 'https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
body: Container(
child: FutureBuilder(
future: fetchNews(),
builder: (context, snap) {
if (snap.hasData) {
final List _news = snap.data;
return ListView.separated(
itemBuilder: (context, i) {
final NewsModel _item = _news[i];
return ListTile(
title: Text('${_item.title}'),
subtitle: Text(
'${_item.description}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
);
},
separatorBuilder: (context, i) => Divider(),
itemCount: _news.length,
);
} else if (snap.hasError) {
return Center(
child: Text(snap.error.toString()),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
),
),
);
}
Future fetchNews() async {
final _response = await http.get(rssUrl);
if (_response.statusCode == 200) {
var _decoded = new RssFeed.parse(_response.body);
return _decoded.items
.map((item) => NewsModel(
title: item.title,
description: item.description,
))
.toList();
} else {
throw HttpException('Failed to fetch the data');
}
}
/* final client = http.Client();
rssStream() {
client
.get('https://nathaliehardyn.blog.tdg.ch/index.rss')
.then((response) {
return response.body;
}).then((bodyString) {
var channel = new RssFeed.parse(bodyString);
print(channel.title +"\n"+ channel.items.first.description);
return channel;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Blog Feed'),
),
body: Container(
child: ListWheelScrollView(
children: [
rssStream(),
Padding(
padding: const EdgeInsets.only(left: 20.0, right:20.0, bottom:8.0),
child: Container(
height: 100.0,
width: MediaQuery.of(context).size.width,
color: Colors.purple,
),
),
Padding(
padding: const EdgeInsets.only(left: 20.0, right:20.0, bottom:8.0),
child: Container(
height: 100.0,
width: MediaQuery.of(context).size.width,
color: Colors.purple,
),
)
],
itemExtent: 42,
useMagnifier: true,
magnification: 1.5,
)
)
);
}
*/
}
/*@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
......@@ -10,6 +585,7 @@ class BlogWidget extends StatelessWidget {
children: <Widget>[
titleSection,
Text('Article 1'),
Text('Article 2'),
],
),
......@@ -27,3 +603,24 @@ Widget titleSection = Container(
Text('Recherche'),
],
));
class Rss extends Stateless
void main() async {
var client = new http.Client();
//RSS feed
client
.get('https://www.ge.ch/rss/blog/3656')
.then((response) {
return response.body;
}).then((bodyString) {
var channel = new RssFeed.parse(bodyString);
print(channel);
return channel;
});
}
*/
*/
\ No newline at end of file
......@@ -32,7 +32,9 @@ dependencies:
fl_chart: ^0.9.4
sqflite:
path:
webfeed: ^0.6.0
url_launcher: ^5.2.4
intl: ^0.16.1
intl_translation: ^0.17.9
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment