1- import 'package:flutter/cupertino.dart' ;
2- import 'package:flutter/material.dart' ;
3- import 'package:flutter_test/flutter_test.dart' ;
4- import 'package:provider/provider.dart' ;
51import 'package:change_notifier_provider_sample/home/home_screen.dart' ;
62import 'package:change_notifier_provider_sample/localization.dart' ;
73import 'package:change_notifier_provider_sample/models.dart' ;
84import 'package:change_notifier_provider_sample/todo_list_model.dart' ;
5+ import 'package:flutter/cupertino.dart' ;
6+ import 'package:flutter/material.dart' ;
7+ import 'package:flutter_test/flutter_test.dart' ;
8+ import 'package:provider/provider.dart' ;
99import 'package:todos_app_core/todos_app_core.dart' ;
1010import 'package:todos_repository_core/todos_repository_core.dart' ;
1111
1212import 'mock_repository.dart' ;
1313
14- /// Demonstrates how to test Widgets
14+ /// Demonstrates how to test Widgets in combination with a ChangeNotifier
1515void main () {
1616 group ('HomeScreen' , () {
1717 final todoListFinder = find.byKey (ArchSampleKeys .todoList);
@@ -91,16 +91,16 @@ class _TestWidget extends StatelessWidget {
9191 Key key,
9292 this .child,
9393 this .repository,
94- this .todos = const [] ,
94+ this .todos,
9595 }) : super (key: key);
9696
9797 @override
9898 Widget build (BuildContext context) {
9999 return ChangeNotifierProvider <TodoListModel >(
100- create: (_) => TodoListModel (
101- repository : MockRepository ([]),
102- todos : defaultTodos (),
103- ) ,
100+ create: (_) {
101+ final repo = MockRepository (todos ?? _defaultTodos);
102+ return TodoListModel (repository : repo).. loadTodos ();
103+ } ,
104104 child: MaterialApp (
105105 localizationsDelegates: [
106106 ProviderLocalizationsDelegate (),
@@ -110,14 +110,14 @@ class _TestWidget extends StatelessWidget {
110110 ),
111111 );
112112 }
113- }
114113
115- List <Todo > defaultTodos () {
116- return [
117- Todo ('T1' , id: '1' , note: 'N1' ),
118- Todo ('T2' , id: '2' ),
119- Todo ('T3' , id: '3' , complete: true ),
120- ];
114+ static List <Todo > get _defaultTodos {
115+ return [
116+ Todo ('T1' , id: '1' , note: 'N1' ),
117+ Todo ('T2' , id: '2' ),
118+ Todo ('T3' , id: '3' , complete: true ),
119+ ];
120+ }
121121}
122122
123123Matcher isChecked (bool isChecked) {
0 commit comments