Skip to content

Add Basith P' s app #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BasithP/Preview after build.mp4
Binary file not shown.
Binary file added BasithP/Release/app-release.apk
Binary file not shown.
29 changes: 29 additions & 0 deletions BasithP/lib/api/news.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:convert';

import 'package:http/http.dart' as http;

import 'apikey.dart';
import '/models/modal_article.dart';

class News {
// String url = "https://newsapi.org/v2/everything?country=us&apiKey=$apiKey";
String url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=$apiKey";

Future<List<Article>> getNews() async {
var response = await http.Client().get(Uri.parse(url));

if (response.statusCode == 200) {
Map<String, dynamic> _json = json.decode(response.body);
List<dynamic> body = _json['articles'];
List<Article> articles = [];
for (var element in body) {
try {
articles.add(Article.fromJson(element));
} catch (_) {}
}
return articles;
} else {
throw Exception("There was an error calling the API");
}
}
}
18 changes: 18 additions & 0 deletions BasithP/lib/config/routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';

import '../pages/home_page/home_page.dart';
import '../pages/profile_page/profile_page.dart';

const String homePage = 'home';
const String profilePage = 'profile';

Route<dynamic> controller(RouteSettings settings) {
switch (settings.name) {
case homePage:
return MaterialPageRoute(builder: (context) => const HomePage());
case profilePage:
return MaterialPageRoute(builder: (context) => const ProfilePage());
default:
throw ('This route name does not exists');
}
}
35 changes: 35 additions & 0 deletions BasithP/lib/config/themes/theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:google_fonts/google_fonts.dart';

var themeData = ThemeData.dark().copyWith(
scaffoldBackgroundColor: const Color(0xff111214),
appBarTheme: AppBarTheme(
centerTitle: true,
backgroundColor: Colors.red[900],
),
textTheme: ThemeData.dark().textTheme.copyWith(
headline3: GoogleFonts.lato(
color: const Color(0xffFEFFFF),
fontWeight: FontWeight.bold,
height: 1.4,
),
headline5: GoogleFonts.lato(
color: const Color(0xffFEFFFF),
fontWeight: FontWeight.bold,
),
headline6: GoogleFonts.lato(
color: Colors.grey[300],
fontWeight: FontWeight.bold,
fontSize: 18,
),
bodyText1: GoogleFonts.lato(
color: Colors.grey,
),
bodyText2: GoogleFonts.lato(
color: const Color(0xffFEFFFF),
fontSize: 18,
),
),
);
16 changes: 16 additions & 0 deletions BasithP/lib/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Generated file. Do not edit.
//

// ignore_for_file: directives_ordering
// ignore_for_file: lines_longer_than_80_chars

import 'package:url_launcher_web/url_launcher_web.dart';

import 'package:flutter_web_plugins/flutter_web_plugins.dart';

// ignore: public_member_api_docs
void registerPlugins(Registrar registrar) {
UrlLauncherPlugin.registerWith(registrar);
registrar.registerMessageHandler();
}
29 changes: 29 additions & 0 deletions BasithP/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'config/themes/theme.dart';
import 'config/routes.dart' as route;

void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(const BifNewsApp());
}

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BiF News',
theme: themeData,
debugShowCheckedModeBanner: false,
onGenerateRoute: route.controller,
initialRoute: route.homePage,
);
}
}
97 changes: 97 additions & 0 deletions BasithP/lib/models/modal_article.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// To parse this JSON data, do
//
// final welcome = welcomeFromJson(jsonString);

import 'dart:convert';

Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));

String welcomeToJson(Welcome data) => json.encode(data.toJson());

class Welcome {
Welcome({
required this.status,
required this.totalResults,
required this.articles,
});

String status;
int totalResults;
List<Article> articles;

factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
status: json["status"],
totalResults: json["totalResults"],
articles: List<Article>.from(json["articles"].map((x) => Article.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"status": status,
"totalResults": totalResults,
"articles": List<dynamic>.from(articles.map((x) => x.toJson())),
};
}

class Article {
Article({
required this.source,
required this.author,
required this.title,
required this.description,
required this.url,
required this.urlToImage,
required this.publishedAt,
required this.content,
});

Source source;
String author;
String title;
String description;
String url;
String urlToImage;
DateTime publishedAt;
String content;

factory Article.fromJson(Map<String, dynamic> json) => Article(
source: Source.fromJson(json["source"]),
author: json["author"],
title: json["title"],
description: json["description"],
url: json["url"],
urlToImage: json["urlToImage"],
publishedAt: DateTime.parse(json["publishedAt"]),
content: json["content"],
);

Map<String, dynamic> toJson() => {
"source": source.toJson(),
"author": author,
"title": title,
"description": description,
"url": url,
"urlToImage": urlToImage,
"publishedAt": publishedAt.toIso8601String(),
"content": content,
};
}

class Source {
Source({
required this.id,
required this.name,
});

String id;
String name;

factory Source.fromJson(Map<String, dynamic> json) => Source(
id: json["id"],
name: json["name"],
);

Map<String, dynamic> toJson() => {
"id": id,
"name": name,
};
}
73 changes: 73 additions & 0 deletions BasithP/lib/pages/home_page/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';

import '/config/routes.dart' as route;
import '/utils/vars.dart';
import '/widgets/circle_avatar_with_shadow.dart';
import 'widgets/all_news_section.dart';

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

@override
Widget build(BuildContext context) {
final mdQry = MediaQuery.of(context);
final appBar = AppBar(
leading: const Icon(Icons.search_rounded),
elevation: 0,
title: Text(
'THE BiF NEWS',
style: appBarTextStyle,
),
actions: [
GestureDetector(
onTap: () => Navigator.pushNamed(context, route.profilePage),
child: const Hero(
tag: 'profilePic',
child: CircleAvatarWithShadow(
image: AssetImage('assets/images/1.png'),
radius: 15,
),
),
),
const SizedBox(width: 10)
],
);

final List categories = ['All', 'Business', 'Entertainment', 'Science', 'Sports', 'Technology'];

return Scaffold(
appBar: appBar,
body: SafeArea(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 66,
child: ListView.builder(
padding: const EdgeInsets.all(15),
scrollDirection: Axis.horizontal,
physics: const BouncingScrollPhysics(),
itemCount: categories.length,
shrinkWrap: true,
itemBuilder: (ctx, index){
return Container(
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 15),
margin: const EdgeInsets.only(right: 10),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.withOpacity(0.4)),
borderRadius: BorderRadius.circular(50),
),
child: Text(categories[index]),
);
},),
),
AllNewsSection(mdQry: mdQry),
],
),
),
),
);
}
}
72 changes: 72 additions & 0 deletions BasithP/lib/pages/home_page/widgets/all_news_section.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:bif_news_app/models/modal_article.dart';
import 'package:flutter/material.dart';

import 'news_card.dart';
import '/api/news.dart';

class AllNewsSection extends StatefulWidget {
const AllNewsSection({
Key? key,
required this.mdQry,
}) : super(key: key);

final MediaQueryData mdQry;

@override
State<AllNewsSection> createState() => _AllNewsSectionState();
}

class _AllNewsSectionState extends State<AllNewsSection> {
late Future<List<Article>> _article;

@override
void initState() {
_article = News().getNews();
super.initState();
}

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(20).copyWith(bottom: 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'All stories',
style: Theme.of(context).textTheme.headline5,
),
const SizedBox(height: 10),
FutureBuilder<List<Article>>(
future: _article,
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: snapshot.data!.length,
shrinkWrap: true,
itemBuilder: (context, index) {
var data = snapshot.data![index]!;
return NewsCard(
title: data.title,
screenWidth: widget.mdQry.size.width,
description: data.description,
url: data.url,
urlToImage: data.urlToImage,
author: data.author,
publishedAt: data.publishedAt,
);
},
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
return const CircularProgressIndicator();
}
},
),
],
),
);
}
}
Loading