news_api_flutter_package 1.0.0 copy "news_api_flutter_package: ^1.0.0" to clipboard
news_api_flutter_package: ^1.0.0 copied to clipboard

outdated

Flutter package for accessing News API.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:news_api_flutter_package/model/article.dart';
import 'package:news_api_flutter_package/model/error.dart';
import 'package:news_api_flutter_package/model/source.dart';
import 'package:news_api_flutter_package/news_api_flutter_package.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'News API Demo',
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  final NewsAPI _newsAPI = NewsAPI("aa67d8d98c8e4ad1b4f16dbd5f3be348");

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: _buildAppBar(),
        body: _buildBody(),
      ),
    );
  }

  AppBar _buildAppBar() {
    return AppBar(
      title: Text("News API Demo"),
      bottom: _buildTabBar(),
    );
  }

  TabBar _buildTabBar() {
    return TabBar(
      tabs: [
        Tab(text: "Top Headlines"),
        Tab(text: "Everything"),
        Tab(text: "Sources"),
      ],
    );
  }

  Widget _buildBody() {
    return TabBarView(
      children: [
        _buildTopHeadlinesTabView(),
        _buildEverythingTabView(),
        _buildSourcesTabView(),
      ],
    );
  }

  Widget _buildTopHeadlinesTabView() {
    return FutureBuilder<List<Article>>(
        future: _newsAPI.getTopHeadlines(country: "us"),
        builder: (BuildContext context, AsyncSnapshot<List<Article>> snapshot) {
          return snapshot.connectionState == ConnectionState.done
              ? snapshot.hasData
                  ? _buildArticleListView(snapshot.data)
                  : _buildError(snapshot.error)
              : _buildProgress();
        });
  }

  Widget _buildEverythingTabView() {
    return FutureBuilder<List<Article>>(
        future: _newsAPI.getEverything(query: "bitcoin"),
        builder: (BuildContext context, AsyncSnapshot<List<Article>> snapshot) {
          return snapshot.connectionState == ConnectionState.done
              ? snapshot.hasData
                  ? _buildArticleListView(snapshot.data)
                  : _buildError(snapshot.error)
              : _buildProgress();
        });
  }

  Widget _buildArticleListView(List<Article> articles) {
    return ListView.builder(
        itemCount: articles.length,
        itemBuilder: (context, index) {
          Article article = articles[index];
          return Card(
            child: ListTile(
              title: Text(article.title, maxLines: 2),
              subtitle: Text(article.description ?? "", maxLines: 3),
              trailing: article.urlToImage == null
                  ? null
                  : Image.network(article.urlToImage),
            ),
          );
        });
  }

  Widget _buildSourcesTabView() {
    return FutureBuilder<List<Source>>(
        future: _newsAPI.getSources(),
        builder: (BuildContext context, AsyncSnapshot<List<Source>> snapshot) {
          return snapshot.connectionState == ConnectionState.done
              ? snapshot.hasData
                  ? _buildSourceListView(snapshot.data)
                  : _buildError(snapshot.error)
              : _buildProgress();
        });
  }

  Widget _buildSourceListView(List<Source> sources) {
    return ListView.builder(
        itemCount: sources.length,
        itemBuilder: (context, index) {
          return Card(
            child: ListTile(
              title: Text(sources[index].name),
              subtitle: Text(sources[index].description),
            ),
          );
        });
  }

  Widget _buildProgress() {
    return Center(child: CircularProgressIndicator());
  }

  Widget _buildError(ApiError error) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              error.code ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 4),
            Text(error.message, textAlign: TextAlign.center),
          ],
        ),
      ),
    );
  }
}