diff --git a/chapter_06/lib/controllers/plan_controller.dart b/chapter_06/lib/controllers/plan_controller.dart index acdb45f..e85f472 100644 --- a/chapter_06/lib/controllers/plan_controller.dart +++ b/chapter_06/lib/controllers/plan_controller.dart @@ -23,7 +23,7 @@ class PlanController { services.delete(plan); } - void createNewTask(Plan plan, [String description]) { + void createNewTask(Plan plan, [String? description]) { if (description == null || description.isEmpty) { description = 'New Task'; } diff --git a/chapter_06/lib/main.dart b/chapter_06/lib/main.dart index ce4d50f..b78963a 100644 --- a/chapter_06/lib/main.dart +++ b/chapter_06/lib/main.dart @@ -2,14 +2,16 @@ import 'package:flutter/material.dart'; import 'plan_provider.dart'; import 'views/plan_creator_screen.dart'; -void main() => runApp(PlanProvider(child: MasterPlanApp())); +void main() => runApp(PlanProvider(child: const MasterPlanApp())); class MasterPlanApp extends StatelessWidget { + const MasterPlanApp({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( - theme: ThemeData(primarySwatch: Colors.purple), - home: PlanCreatorScreen(), + theme: ThemeData(primarySwatch: Colors.purple), + home: const PlanCreatorScreen(), ); } -} \ No newline at end of file +} diff --git a/chapter_06/lib/models/data_layer.dart b/chapter_06/lib/models/data_layer.dart index 99b5f7f..c706bb9 100644 --- a/chapter_06/lib/models/data_layer.dart +++ b/chapter_06/lib/models/data_layer.dart @@ -1,2 +1,2 @@ export 'plan.dart'; -export 'task.dart'; \ No newline at end of file +export 'task.dart'; diff --git a/chapter_06/lib/models/plan.dart b/chapter_06/lib/models/plan.dart index bb8f91d..8a148ed 100644 --- a/chapter_06/lib/models/plan.dart +++ b/chapter_06/lib/models/plan.dart @@ -1,18 +1,17 @@ import '../repositories/repository.dart'; -import 'package:flutter/foundation.dart'; import './task.dart'; class Plan { final int id; - String name = ''; + String name; List tasks = []; - Plan({@required this.id, this.name = ''}); + Plan({required this.id, this.name = ''}); Plan.fromModel(Model model) : id = model.id, - name = model?.data['name'], - tasks = model?.data['task'] + name = model.data['name'] ?? '', + tasks = model.data['tasks'] ?.map((task) => Task.fromModel(task)) ?.toList() ?? []; diff --git a/chapter_06/lib/models/task.dart b/chapter_06/lib/models/task.dart index 6a0db87..d039e11 100644 --- a/chapter_06/lib/models/task.dart +++ b/chapter_06/lib/models/task.dart @@ -1,5 +1,4 @@ import '../repositories/repository.dart'; -import 'package:flutter/foundation.dart'; class Task { final int id; @@ -7,7 +6,7 @@ class Task { bool complete; Task({ - @required this.id, + required this.id, this.complete = false, this.description = '', }); diff --git a/chapter_06/lib/plan_provider.dart b/chapter_06/lib/plan_provider.dart index 41d307b..83cade1 100644 --- a/chapter_06/lib/plan_provider.dart +++ b/chapter_06/lib/plan_provider.dart @@ -4,13 +4,15 @@ import 'controllers/plan_controller.dart'; class PlanProvider extends InheritedWidget { final _controller = PlanController(); - PlanProvider({Key key, Widget child}) : super(key: key, child: child); - @override - bool updateShouldNotify(InheritedWidget oldWidget) => false; + bool updateShouldNotify(covariant InheritedWidget oldWidget) => false; + + PlanProvider({Key? key, required Widget child}) + : super(key: key, child: child); static PlanController of(BuildContext context) { - PlanProvider provider = context.dependOnInheritedWidgetOfExactType(); + final provider = context.dependOnInheritedWidgetOfExactType() + as PlanProvider; return provider._controller; } } diff --git a/chapter_06/lib/repositories/in_memory_cache.dart b/chapter_06/lib/repositories/in_memory_cache.dart index 6789c23..26342b6 100644 --- a/chapter_06/lib/repositories/in_memory_cache.dart +++ b/chapter_06/lib/repositories/in_memory_cache.dart @@ -1,13 +1,13 @@ import 'repository.dart'; class InMemoryCache implements Repository { - final _storage = Map(); + final _storage = {}; @override Model create() { final ids = _storage.keys.toList()..sort(); - final id = (ids.length == 0) ? 1 : ids.last + 1; + final id = (ids.isEmpty) ? 1 : ids.last + 1; final model = Model(id: id); _storage[id] = model; @@ -15,7 +15,7 @@ class InMemoryCache implements Repository { } @override - Model get(int id) { + Model? get(int id) { return _storage[id]; } diff --git a/chapter_06/lib/repositories/repository.dart b/chapter_06/lib/repositories/repository.dart index 1defb57..a3ad207 100644 --- a/chapter_06/lib/repositories/repository.dart +++ b/chapter_06/lib/repositories/repository.dart @@ -1,10 +1,8 @@ -import 'package:flutter/foundation.dart'; - abstract class Repository { Model create(); List getAll(); - Model get(int id); + Model? get(int id); void update(Model item); void delete(Model item); @@ -16,7 +14,7 @@ class Model { final Map data; const Model({ - @required this.id, + required this.id, this.data = const {}, }); -} \ No newline at end of file +} diff --git a/chapter_06/lib/services/plan_services.dart b/chapter_06/lib/services/plan_services.dart index 6f80cdb..fe05e67 100644 --- a/chapter_06/lib/services/plan_services.dart +++ b/chapter_06/lib/services/plan_services.dart @@ -3,7 +3,7 @@ import '../repositories/in_memory_cache.dart'; import '../repositories/repository.dart'; class PlanServices { - Repository _repository = InMemoryCache(); + final Repository _repository = InMemoryCache(); Plan createPlan(String name) { final model = _repository.create(); @@ -28,7 +28,10 @@ class PlanServices { } void addTask(Plan plan, String description) { - final id = plan.tasks.last?.id ?? 0 + 1; + int id = 0; + if (plan.tasks.isNotEmpty) { + id = plan.tasks.last.id + 1; + } final task = Task(id: id, description: description); plan.tasks.add(task); savePlan(plan); diff --git a/chapter_06/lib/views/plan_screen.dart b/chapter_06/lib/views/plan_screen.dart index 7dcbc7f..fc99bff 100644 --- a/chapter_06/lib/views/plan_screen.dart +++ b/chapter_06/lib/views/plan_screen.dart @@ -24,14 +24,25 @@ class _PlanScreenState extends State { @override Widget build(BuildContext context) { - // final plan = PlanProvider.of(context); - return Scaffold( - appBar: AppBar(title: Text('Master Plan')), - body: Column(children: [ - Expanded(child: _buildList()), - SafeArea(child: Text(plan.completenessMessage)) - ]), - floatingActionButton: _buildAddTaskButton()); + return WillPopScope( + onWillPop: () { + final controller = PlanProvider.of(context); + controller.savePlan(plan); + return Future.value(true); + }, + child: Scaffold( + appBar: AppBar( + title: const Text('Master Plan'), + ), + body: Column( + children: [ + Expanded(child: _buildList()), + SafeArea(child: Text(plan.completenessMessage)) + ], + ), + floatingActionButton: _buildAddTaskButton(), + ), + ); } @override @@ -53,7 +64,6 @@ class _PlanScreenState extends State { } Widget _buildList() { - //final plan = PlanProvider.of(context); return ListView.builder( controller: scrollController, itemCount: plan.tasks.length,