diff --git a/chapter_07/lib/generated_plugin_registrant.dart b/chapter_07/lib/generated_plugin_registrant.dart deleted file mode 100644 index 6773082..0000000 --- a/chapter_07/lib/generated_plugin_registrant.dart +++ /dev/null @@ -1,15 +0,0 @@ -// -// Generated file. Do not edit. -// - -// ignore_for_file: lines_longer_than_80_chars - -import 'package:geolocator_web/geolocator_web.dart'; - -import 'package:flutter_web_plugins/flutter_web_plugins.dart'; - -// ignore: public_member_api_docs -void registerPlugins(Registrar registrar) { - GeolocatorPlugin.registerWith(registrar); - registrar.registerMessageHandler(); -} diff --git a/chapter_07/lib/geolocation.dart b/chapter_07/lib/geolocation.dart new file mode 100644 index 0000000..9b5ac05 --- /dev/null +++ b/chapter_07/lib/geolocation.dart @@ -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 { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Current Location'), + ), + body: Center( + child: FutureBuilder( + future: getPosition(), + builder: (BuildContext context, AsyncSnapshot 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 getPosition() async { + await Future.delayed(const Duration(seconds: 3)); + Position? position = await Geolocator.getCurrentPosition( + desiredAccuracy: LocationAccuracy.high); + return position; + } +} diff --git a/chapter_07/lib/location_screen.dart b/chapter_07/lib/location_screen.dart deleted file mode 100644 index a1f0e7b..0000000 --- a/chapter_07/lib/location_screen.dart +++ /dev/null @@ -1,34 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:geolocator/geolocator.dart'; - -class LocationScreen extends StatefulWidget { - @override - _LocationScreenState createState() => _LocationScreenState(); -} - -class _LocationScreenState extends State { - String myPosition = ''; - @override - void initState() { - getPosition().then((Position myPos) { - myPosition = 'Latitude: ' + myPos.latitude.toString() + ' - Longitude: ' + myPos.longitude.toString(); - setState(() { - myPosition = myPosition; - }); - }); - super.initState(); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text('Current Location')), - body: Center(child:Text(myPosition)), - ); - } - - Future getPosition() async { - Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); - return position; - } -} \ No newline at end of file diff --git a/chapter_07/lib/main.dart b/chapter_07/lib/main.dart index aaddbf0..1110be6 100644 --- a/chapter_07/lib/main.dart +++ b/chapter_07/lib/main.dart @@ -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( @@ -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 { - String result; - Completer completer; - Future getNumber() { + String result = ""; + late Completer completer; + Future getNumber() { completer = Completer(); calculate(); return completer.future; @@ -50,31 +51,22 @@ class _FuturePageState extends State { 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(), ]), ), ); @@ -111,11 +103,11 @@ class _FuturePageState extends State { futureGroup.add(returnTwoAsync()); futureGroup.add(returnThreeAsync()); futureGroup.close(); - futureGroup.future.then((List value) { + futureGroup.future.then((List value) { int total = 0; - value.forEach((element) { + for (var element in value) { total += element; - }); + } setState(() { result = total.toString(); }); @@ -126,9 +118,15 @@ class _FuturePageState extends State { throw ('Something terrible happened!'); } - Future 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'); + } } - } diff --git a/chapter_07/lib/navigation_dialog.dart b/chapter_07/lib/navigation_dialog.dart index cd4522a..809b350 100644 --- a/chapter_07/lib/navigation_dialog.dart +++ b/chapter_07/lib/navigation_dialog.dart @@ -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 { - 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); + }, + ), ), ); } @@ -31,25 +34,25 @@ class _NavigationDialogState extends State { 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: [ 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); }), ], diff --git a/chapter_07/lib/navigation_first.dart b/chapter_07/lib/navigation_first.dart index 35210c4..b1373f9 100644 --- a/chapter_07/lib/navigation_first.dart +++ b/chapter_07/lib/navigation_first.dart @@ -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 { - 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); }), diff --git a/chapter_07/lib/navigation_second.dart b/chapter_07/lib/navigation_second.dart index 681c406..c198004 100644 --- a/chapter_07/lib/navigation_second.dart +++ b/chapter_07/lib/navigation_second.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; class NavigationSecond extends StatefulWidget { + const NavigationSecond({Key? key}) : super(key: key); + @override _NavigationSecondState createState() => _NavigationSecondState(); } @@ -11,28 +13,28 @@ class _NavigationSecondState extends State { 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: [ 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); }), ], diff --git a/chapter_07/pubspec.yaml b/chapter_07/pubspec.yaml index 2c07504..a7402c7 100644 --- a/chapter_07/pubspec.yaml +++ b/chapter_07/pubspec.yaml @@ -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.