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
15 changes: 0 additions & 15 deletions chapter_07/lib/generated_plugin_registrant.dart

This file was deleted.

44 changes: 44 additions & 0 deletions chapter_07/lib/geolocation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

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

@override
_LocationScreenState createState() => _LocationScreenState();
}

class _LocationScreenState extends State<LocationScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Current Location'),
),
body: Center(
child: FutureBuilder(
future: getPosition(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Something terrible happened!');
}
return Text(snapshot.data);
} else {
return const Text('');
}
},
),
),
);
}

Future<Position> getPosition() async {
await Future<int>.delayed(const Duration(seconds: 3));
Position? position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
return position;
}
}
34 changes: 0 additions & 34 deletions chapter_07/lib/location_screen.dart

This file was deleted.

64 changes: 31 additions & 33 deletions chapter_07/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import 'package:cookbook_ch_07/navigation_first.dart';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;

import 'location_screen.dart';
import 'geolocation.dart';
import 'navigation_dialog.dart';

void main() {
runApp(MyApp());
}
void main() => runApp(const MyApp());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -22,20 +21,22 @@ class MyApp extends StatelessWidget {
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: NavigationDialog(),
home: const NavigationDialog(),
);
}
}

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

@override
_FuturePageState createState() => _FuturePageState();
}

class _FuturePageState extends State<FuturePage> {
String result;
Completer completer;
Future<int> getNumber() {
String result = "";
late Completer completer;
Future getNumber() {
completer = Completer<int>();
calculate();
return completer.future;
Expand All @@ -50,31 +51,22 @@ class _FuturePageState extends State<FuturePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Back from the Future'),
title: const Text('Back from the Future'),
),
body: Center(
child: Column(children: [
Spacer(),
const Spacer(),
ElevatedButton(
child: Text('GO!'),
child: const Text('GO!'),
onPressed: () {
returnError()
.then((value){
setState(() {
result = 'Success';
});
}).catchError((onError){
setState(() {
result = onError;
});
}).whenComplete(() => print('Complete'));
handleError();
},
),
Spacer(),
constSpacer(),
Text(result.toString()),
Spacer(),
CircularProgressIndicator(),
Spacer(),
const Spacer(),
const CircularProgressIndicator(),
const Spacer(),
]),
),
);
Expand Down Expand Up @@ -111,11 +103,11 @@ class _FuturePageState extends State<FuturePage> {
futureGroup.add(returnTwoAsync());
futureGroup.add(returnThreeAsync());
futureGroup.close();
futureGroup.future.then((List <int> value) {
futureGroup.future.then((List<int> value) {
int total = 0;
value.forEach((element) {
for (var element in value) {
total += element;
});
}
setState(() {
result = total.toString();
});
Expand All @@ -126,9 +118,15 @@ class _FuturePageState extends State<FuturePage> {
throw ('Something terrible happened!');
}

Future<Position> getPosition() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
return position;
Future handleError() async {
try {
await returnError();
} catch (error) {
setState(() {
result = error.toString();
});
} finally {
print('Complete');
}
}

}
31 changes: 17 additions & 14 deletions chapter_07/lib/navigation_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import 'package:flutter/material.dart';

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

@override
_NavigationDialogState createState() => _NavigationDialogState();
}

class _NavigationDialogState extends State<NavigationDialog> {
Color color = Colors.blue[700];
Color? color = Colors.blue[700];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
appBar: AppBar(
title: Text('Navigation Dialog Screen'),
title: const Text('Navigation Dialog Screen'),
),
body: Center(
child: ElevatedButton(
child: Text('Change Color'),
onPressed: () {
_showColorDialog(context);
}),
child: const Text('Change Color'),
onPressed: () {
_showColorDialog(context);
},
),
),
);
}
Expand All @@ -31,25 +34,25 @@ class _NavigationDialogState extends State<NavigationDialog> {
context: context,
builder: (_) {
return AlertDialog(
title: Text('Very important question'),
content: Text('Please choose a color'),
title: const Text('Very important question'),
content: const Text('Please choose a color'),
actions: <Widget>[
TextButton(
child: Text('Red'),
child: const Text('Red'),
onPressed: () {
color = Colors.red[700];
color = Colors.red;
Navigator.pop(context, color);
}),
TextButton(
child: Text('Green'),
child: const Text('Green'),
onPressed: () {
color = Colors.green[700];
color = Colors.green;
Navigator.pop(context, color);
}),
TextButton(
child: Text('Blue'),
child: const Text('Blue'),
onPressed: () {
color = Colors.blue[700];
color = Colors.blue;
Navigator.pop(context, color);
}),
],
Expand Down
9 changes: 5 additions & 4 deletions chapter_07/lib/navigation_first.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import 'package:flutter/material.dart';

import 'navigation_second.dart';

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

@override
_NavigationFirstState createState() => _NavigationFirstState();
}

class _NavigationFirstState extends State<NavigationFirst> {
Color color = Colors.blue[700];
Color color = Colors.blue;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
appBar: AppBar(
title: Text('Navigation First Screen'),
title: const Text('Navigation First Screen'),
),
body: Center(
child: ElevatedButton(
child: Text('Change Color'),
child: const Text('Change Color'),
onPressed: () {
_navigateAndGetColor(context);
}),
Expand Down
16 changes: 9 additions & 7 deletions chapter_07/lib/navigation_second.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';

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

@override
_NavigationSecondState createState() => _NavigationSecondState();
}
Expand All @@ -11,28 +13,28 @@ class _NavigationSecondState extends State<NavigationSecond> {
Color color;
return Scaffold(
appBar: AppBar(
title: Text('Navigation Second Screen'),
title: const Text('Navigation Second Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
child: Text('Red'),
child: const Text('Red'),
onPressed: () {
color = Colors.red[700];
color = Colors.red;
Navigator.pop(context, color);
}),
ElevatedButton(
child: Text('Green'),
child: const Text('Green'),
onPressed: () {
color = Colors.green[700];
color = Colors.green;
Navigator.pop(context, color);
}),
ElevatedButton(
child: Text('Blue'),
child: const Text('Blue'),
onPressed: () {
color = Colors.blue[700];
color = Colors.blue;
Navigator.pop(context, color);
}),
],
Expand Down
6 changes: 3 additions & 3 deletions chapter_07/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^0.13.1
async: ^2.5.0
geolocator: ^7.0.3
http: ^0.13.4
async: 2.8.1
geolocator: ^7.7.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down