Skip to content

fix depcreated api a and add docs #4

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 3 commits into
base: master
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
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.resocoder.flutter_testing_tutorial"
minSdkVersion 16
minSdkVersion flutter.minSdkVersion
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
3 changes: 3 additions & 0 deletions lib/article.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// A simple Article class with a title and content.
class Article {
final String title;
final String content;
Expand All @@ -7,6 +8,7 @@ class Article {
required this.content,
});

/// Creates a copy of this article with the given fields replaced with the new values.
Article copyWith({
String? title,
String? content,
Expand All @@ -20,6 +22,7 @@ class Article {
@override
String toString() => 'Article(title: $title, content: $content)';

/// Compares two articles based on their titles and contents.
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down
16 changes: 11 additions & 5 deletions lib/article_page.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_testing_tutorial/article.dart';

/// A widget that displays an article's title and content.
class ArticlePage extends StatelessWidget {
/// The article to be displayed.
final Article article;

/// Creates an instance of [ArticlePage].
///
/// The [article] parameter is required and must not be null.
const ArticlePage({
required this.article,
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
// Get the media query data for responsive design
final mq = MediaQuery.of(context);
return Scaffold(
appBar: AppBar(
title: Text(article.title),
),
body: SingleChildScrollView(
padding: EdgeInsets.only(
top: mq.padding.top,
Expand All @@ -22,11 +31,8 @@ class ArticlePage extends StatelessWidget {
),
child: Column(
children: [
Text(
article.title,
style: Theme.of(context).textTheme.headline5,
),
const SizedBox(height: 8),
// Display the article title
// Display the article content
Text(article.content),
],
),
Expand Down
12 changes: 11 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import 'package:flutter_testing_tutorial/news_service.dart';

void main() => runApp(MyApp());

/// Root widget of the application
///
/// This stateless widget sets up the basic app structure including:
/// - MaterialApp as the root
/// - Provider for state management
/// - Initial route to NewsPage
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'News App',
// Initialize NewsChangeNotifier with NewsService dependency
home: ChangeNotifierProvider(
create: (_) => NewsChangeNotifier(NewsService()),
child: NewsPage(),
child: const NewsPage(),
),
);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/news_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ import 'package:flutter/material.dart';
import 'package:flutter_testing_tutorial/article.dart';
import 'package:flutter_testing_tutorial/news_service.dart';

/// A class that extends [ChangeNotifier] to manage and notify listeners about news articles.
class NewsChangeNotifier extends ChangeNotifier {
/// The service responsible for fetching news articles.
final NewsService _newsService;

/// Creates an instance of [NewsChangeNotifier] with the given [NewsService].
NewsChangeNotifier(this._newsService);

/// A list of articles fetched from the news service.
List<Article> _articles = [];

/// Returns the list of articles.
List<Article> get articles => _articles;

/// Indicates whether articles are currently being loaded.
bool _isLoading = false;

/// Returns true if articles are being loaded, false otherwise.
bool get isLoading => _isLoading;

/// Fetches articles from the news service and updates the state.
Future<void> getArticles() async {
_isLoading = true;
notifyListeners();
Expand Down
15 changes: 15 additions & 0 deletions lib/news_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@ import 'package:provider/provider.dart';
import 'package:flutter_testing_tutorial/article_page.dart';
import 'package:flutter_testing_tutorial/news_change_notifier.dart';

/// A page that displays a list of news articles.
class NewsPage extends StatefulWidget {
/// Creates a [NewsPage].
const NewsPage({Key? key}) : super(key: key);

@override
State<NewsPage> createState() => _NewsPageState();
}

class _NewsPageState extends State<NewsPage> {

@override
void initState() {
super.initState();
// Fetch articles when the widget is initialized.
Future.microtask(
() => context.read<NewsChangeNotifier>().getArticles(),
);
}

@override
Widget build(BuildContext context) {
// Build the UI for the NewsPage.
return Scaffold(
appBar: AppBar(
title: const Text('News'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
// Refresh articles when the refresh button is pressed.
context.read<NewsChangeNotifier>().getArticles();
},
),
],
),
body: Consumer<NewsChangeNotifier>(
builder: (context, notifier, child) {
Expand All @@ -42,6 +56,7 @@ class _NewsPageState extends State<NewsPage> {
elevation: 2,
child: InkWell(
onTap: () {
// Navigate to the ArticlePage when an article is tapped.
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ArticlePage(article: article),
Expand Down
17 changes: 14 additions & 3 deletions lib/news_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ import 'package:flutter_testing_tutorial/article.dart';

/// A News service simulating communication with a server.
class NewsService {
// Simulating a remote database
/// In-memory list of articles that simulates a remote database.
/// Generates 10 articles with random lorem ipsum text for both
/// title and content.

final _articles = List.generate(
10,
(_) => Article(
title: lorem(paragraphs: 1, words: 3),
content: lorem(paragraphs: 10, words: 500),
title: lorem(paragraphs: 1, words: 3), // Generate a 3-word title
content: lorem(
paragraphs: 7, words: 500), // Generate content with 10 paragraphs
),
);

/// Fetches all articles from the simulated database.
///
/// Adds an artificial delay of 1 second to simulate network latency.
///
/// Returns:
/// Future<List<Article>>: A list of Article objects after the delay.
Future<List<Article>> getArticles() async {
await Future.delayed(const Duration(seconds: 1));
return _articles;
}

}
2 changes: 1 addition & 1 deletion macos/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :osx, '10.11'
platform :osx, '10.14'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
11 changes: 6 additions & 5 deletions macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down Expand Up @@ -202,7 +202,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 0930;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down Expand Up @@ -255,6 +255,7 @@
/* Begin PBXShellScriptBuildPhase section */
3399D490228B24CF009A79C7 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -386,7 +387,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -465,7 +466,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -512,7 +513,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
Loading