Skip to content

Commit

Permalink
Added NewsAPI service
Browse files Browse the repository at this point in the history
  • Loading branch information
insfirred committed Jun 16, 2022
1 parent 4d2c93e commit 3e207f0
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 38 deletions.
63 changes: 63 additions & 0 deletions lib/HomeScreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import './Screens/waitingScreen.dart';
import './Screens/bookmarks.dart';
import './Screens/newsList.dart';
import './Screens/webView.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

var controller = PageController(initialPage: 1);

String url = "https://newsapi.org/v2/top-headlines?country=in&category=science&apiKey=ff7aefdd16e6480faf2817f36e2daa5e";

late var jsonData;

bool isNewsFetched = false;

void fetchNews() async{
var response = await http.get(Uri.parse(url));
var news = json.decode(response.body);
jsonData = news;
if(news != null){
isNewsFetched = true;
setState(() {});
}
}

@override

void initState() {
super.initState();
fetchNews();
}

Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: PageView(
controller: controller,
children: [
Bookmarks(),
isNewsFetched ?NewsList(jsonData) :WaitingScreen(),
// isNewsFetched ?WaitingScreen() :WaitingScreen(),
WebView(),
],
),
// floatingActionButton: FloatingActionButton(
// onPressed: ()=> fetchNews(),
// child: Icon(Icons.search),
// ),
),
);
}
}
46 changes: 26 additions & 20 deletions lib/Screens/newsList.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class NewsList extends StatefulWidget {
const NewsList({Key? key}) : super(key: key);
class NewsList extends StatelessWidget {

var jsonData;
NewsList(this.jsonData);

@override
State<NewsList> createState() => _NewsListState();
}

class _NewsListState extends State<NewsList> {
String Heading = "Indian street food restaurant Chai Pani named best in US";
String description =
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).";
final DateTime now = DateTime.now();
final timeFormatter = DateFormat('Hms');
final dateFormatter = DateFormat('MMMMd');

@override
Widget build(BuildContext context) {
return PageView.builder(
scrollDirection: Axis.vertical,
itemCount: 3,
itemCount: jsonData["articles"].length,
itemBuilder: (context, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: MediaQuery.of(context).size.height / 3,
color: Colors.amber,
child: Image(
fit: BoxFit.fill,
image: AssetImage('assets/images/img.png')),
// child: Image(
// fit: BoxFit.fill,
// image: AssetImage('assets/images/img.png')
// image: NetworkImage,
// ),
child: Image.network("${jsonData["articles"][index]["urlToImage"]}", fit: BoxFit.fill,),
),
Expanded(
child: Container(
Expand All @@ -36,19 +37,24 @@ class _NewsListState extends State<NewsList> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(Heading,
Text(
jsonData["articles"][index]["title"],
style: TextStyle(fontSize: 25),
textAlign: TextAlign.justify),
SizedBox(
height: 10,
),
const SizedBox(height: 10),
Text(
description,
(jsonData["articles"][index]["content"] != null) ?jsonData["articles"][index]["content"] :"",
style: TextStyle(
color: Colors.grey[600], fontSize: 17),
textAlign: TextAlign.justify,
),
SizedBox(height: 15),
Text(
'Published at: ${dateFormatter.format( DateTime.parse(jsonData["articles"][index]["publishedAt"]))} ${timeFormatter.format( DateTime.parse(jsonData["articles"][index]["publishedAt"]))}',
style: TextStyle(color: Colors.grey, fontSize: 16),
textAlign: TextAlign.justify,
),
SizedBox(height: 20,),
Text(
'swipe left for more info.',
style: TextStyle(color: Colors.grey, fontSize: 14),
Expand All @@ -61,4 +67,4 @@ class _NewsListState extends State<NewsList> {
);
});
}
}
}
27 changes: 27 additions & 0 deletions lib/Screens/waitingScreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';

class WaitingScreen extends StatefulWidget {
const WaitingScreen({Key? key}) : super(key: key);

@override
State<WaitingScreen> createState() => _WaitingScreenState();
}

class _WaitingScreenState extends State<WaitingScreen> {
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.cloud_outlined,size: 150,),
Icon(Icons.arrow_downward_rounded,size: 65,),
SizedBox(height: 15),
Icon(Icons.mobile_friendly_rounded,size: 130,),
SizedBox(height: 50,),
Text('Loading Shorts...',style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),),
SizedBox(height: 40,),
CircularProgressIndicator(color: Colors.grey[600],)
],
);
}
}
21 changes: 3 additions & 18 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import 'package:flutter/material.dart';

import './Screens/bookmarks.dart';
import './Screens/newsList.dart';
import './Screens/webView.dart';
import './HomeScreen.dart';

void main() {
var controller = PageController(initialPage: 1);
runApp(MaterialApp(
title: 'Flutter Demo',
themeMode: ThemeMode.system,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: SafeArea(
child: Scaffold(
body: PageView(
controller: controller,
children: [
Bookmarks(),
NewsList(),
WebView(),
],
),
),
),
home: HomeScreen(),
));
}

}
28 changes: 28 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -156,6 +177,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
vector_math:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
http: ^0.13.4
intl: ^0.17.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 3e207f0

Please sign in to comment.