Skip to content
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
45 changes: 45 additions & 0 deletions examples/fwe/wikipedia_reader/.gitignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can delete this file. There's a root gitignore that covers these.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/
/coverage/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
45 changes: 45 additions & 0 deletions examples/fwe/wikipedia_reader/.metadata
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "e308d690a1193ea0d93d62aad6efe48e5994bc3c"
channel: "[user-branch]"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: android
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: ios
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: linux
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: macos
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: web
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
- platform: windows
create_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c
base_revision: e308d690a1193ea0d93d62aad6efe48e5994bc3c

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
3 changes: 3 additions & 0 deletions examples/fwe/wikipedia_reader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# wikipedia_reader

A new Flutter project.
1 change: 1 addition & 0 deletions examples/fwe/wikipedia_reader/analysis_options.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this file. We have a shared analysis config at the root of the /examples directory.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flutter_lints/flutter.yaml
167 changes: 167 additions & 0 deletions examples/fwe/wikipedia_reader/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// ignore_for_file: avoid_dynamic_calls

import 'dart:convert';
Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignore shouldn't be needed. I don't see any code that would trigger it.

Suggested change
// ignore_for_file: avoid_dynamic_calls
import 'dart:convert';
import 'dart:convert';

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

import 'summary.dart';

void main() {
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(home: ArticleView());
}
}

class ArticleModel {
Future<Summary> getRandomArticleSummary() async {
final uri = Uri.https(
'en.wikipedia.org',
'/api/rest_v1/page/random/summary',
);
final response = await get(uri);

if (response.statusCode != 200) {
throw const HttpException('Failed to update resource');
}

return Summary.fromJson(jsonDecode(response.body) as Map<String, Object?>);
}
}

class ArticleViewModel extends ChangeNotifier {
ArticleViewModel(this.model);
final ArticleModel model;

Summary? summary;

bool isLoading = false;

Exception? error;

Future<void> fetchArticle() async {
isLoading = true;
error = null;
notifyListeners();

try {
summary = await model.getRandomArticleSummary();
error = null;
} on Exception catch (e) {
error = e;
} finally {
isLoading = false;
notifyListeners();
}
}
}

class ArticleView extends StatefulWidget {
const ArticleView({super.key});

@override
State<ArticleView> createState() => _ArticleViewState();
}

class _ArticleViewState extends State<ArticleView> {
late final ArticleViewModel viewModel;

@override
void initState() {
super.initState();
viewModel = ArticleViewModel(ArticleModel());
viewModel.fetchArticle();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Wikipedia Flutter')),
body: Center(
child: ListenableBuilder(
listenable: viewModel,
builder: (context, _) {
return switch ((
viewModel.isLoading,
viewModel.summary,
viewModel.error,
)) {
(true, _, _) => const CircularProgressIndicator(),
(_, final summary?, _) => ArticlePage(
summary: summary,
nextArticleCallback: viewModel.fetchArticle,
),
(_, _, final Exception e) => Text('Error: $e'),
_ => const Text('Something went wrong!'),
};
},
),
),
);
}
}

class ArticlePage extends StatelessWidget {
const ArticlePage({
super.key,
required this.summary,
required this.nextArticleCallback,
});

final Summary summary;
final VoidCallback nextArticleCallback;

@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
ArticleWidget(summary: summary),
ElevatedButton(
onPressed: nextArticleCallback,
child: const Text('Next random article'),
),
],
),
);
}
}

class ArticleWidget extends StatelessWidget {
const ArticleWidget({super.key, required this.summary});

final Summary summary;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
spacing: 10.0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The Column widget does not have a spacing property. This will cause a compilation error. For spacing between children in a Column, consider using SizedBox widgets.

Suggested change
spacing: 10.0,
children: [

children: [
if (summary.hasImage) Image.network(summary.originalImage!.source),
Text(
summary.titles.normalized,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.displaySmall,
),
if (summary.description != null)
Text(
summary.description!,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodySmall,
),
Text(summary.extract),
],
),
);
}
}
34 changes: 34 additions & 0 deletions examples/fwe/wikipedia_reader/lib/step1_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ignore_for_file: unused_import

// #docregion All
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

import 'summary.dart';

// #docregion main
void main() {
runApp(const MainApp());
}
// #enddocregion main

// #docregion MainApp
class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Wikipedia Flutter')),
body: const Center(child: Text('Loading...')),
),
);
}
}
// #enddocregion MainApp

// #enddocregion All
46 changes: 46 additions & 0 deletions examples/fwe/wikipedia_reader/lib/step2_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ignore_for_file: unused_import, avoid_dynamic_calls

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

import 'summary.dart';

void main() {
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Wikipedia Flutter')),
body: const Center(child: Text('Loading...')),
),
);
}
}

// #docregion ArticleModel
class ArticleModel {
Future<Summary> getRandomArticleSummary() async {
final uri = Uri.https(
'en.wikipedia.org',
'/api/rest_v1/page/random/summary',
);
final response = await get(uri);

if (response.statusCode != 200) {
throw HttpException('Failed to update resource');
}

return Summary.fromJson(jsonDecode(response.body) as Map<String, Object?>);
}
}

// #enddocregion ArticleModel
34 changes: 34 additions & 0 deletions examples/fwe/wikipedia_reader/lib/step2a_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ignore_for_file: unused_import

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

import 'summary.dart';

void main() {
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Wikipedia Flutter')),
body: const Center(child: Text('Loading...')),
),
);
}
}

// #docregion ArticleModel
class ArticleModel {
// Properties and methods will be added here.
}

// #enddocregion ArticleModel
Loading
Loading