diff --git a/uni/lib/view/exams/widgets/exam_filter_form.dart b/uni/lib/view/exams/widgets/exam_filter_form.dart index 7f8396e01..a93a459b6 100644 --- a/uni/lib/view/exams/widgets/exam_filter_form.dart +++ b/uni/lib/view/exams/widgets/exam_filter_form.dart @@ -1,19 +1,18 @@ +import 'dart:collection'; import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import 'package:uni/generated/l10n.dart'; import 'package:uni/model/entities/exam.dart'; -import 'package:uni/model/providers/lazy/exam_provider.dart'; -class ExamFilterForm extends StatefulWidget { - const ExamFilterForm(this.filteredExamsTypes, {super.key}); +class ExamFilterForm extends StatelessWidget { + const ExamFilterForm( + this.filteredExamsTypes, + this.setExamsTypesCallback, { + super.key, + }); final Map filteredExamsTypes; + final void Function(Map) setExamsTypesCallback; - @override - ExamFilterFormState createState() => ExamFilterFormState(); -} - -class ExamFilterFormState extends State { @override Widget build(BuildContext context) { return AlertDialog( @@ -29,28 +28,34 @@ class ExamFilterFormState extends State { ), onPressed: () => Navigator.pop(context), ), - ElevatedButton( - child: Text(S.of(context).confirm), - onPressed: () { - Provider.of(context, listen: false) - .setFilteredExams(widget.filteredExamsTypes); - Navigator.pop(context); - }, - ), ], content: SizedBox( height: 230, width: 200, - child: getExamCheckboxes(widget.filteredExamsTypes, context), + child: FilteredExamList( + UnmodifiableMapView( + Map.from(filteredExamsTypes) + ..removeWhere((key, value) => !Exam.types.containsKey(key)), + ), + setExamsTypesCallback, + ), ), ); } +} + +class FilteredExamList extends StatelessWidget { + const FilteredExamList( + this.filteredExams, + this.changeFilteredExamList, { + super.key, + }); - Widget getExamCheckboxes( - Map filteredExams, - BuildContext context, - ) { - filteredExams.removeWhere((key, value) => !Exam.types.containsKey(key)); + final UnmodifiableMapView filteredExams; + final void Function(Map) changeFilteredExamList; + + @override + Widget build(BuildContext context) { return ListView( children: List.generate(filteredExams.length, (i) { final key = filteredExams.keys.elementAt(i); @@ -66,9 +71,9 @@ class ExamFilterFormState extends State { key: Key('ExamCheck$key'), value: filteredExams[key], onChanged: (value) { - setState(() { - filteredExams[key] = value!; - }); + final newFilteredExams = Map.from(filteredExams); + newFilteredExams[key] = value!; + changeFilteredExamList(newFilteredExams); }, ); }), diff --git a/uni/lib/view/exams/widgets/exam_filter_menu.dart b/uni/lib/view/exams/widgets/exam_filter_menu.dart index 1ea516678..094858b2f 100644 --- a/uni/lib/view/exams/widgets/exam_filter_menu.dart +++ b/uni/lib/view/exams/widgets/exam_filter_menu.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import 'package:uni/model/providers/lazy/exam_provider.dart'; import 'package:uni/view/exams/widgets/exam_filter_form.dart'; +import 'package:uni/view/lazy_consumer.dart'; class ExamFilterMenu extends StatefulWidget { const ExamFilterMenu({super.key}); @@ -19,14 +19,14 @@ class ExamFilterMenuState extends State { showDialog( context: context, builder: (_) { - final examProvider = - Provider.of(context, listen: false); - final filteredExamsTypes = examProvider.filteredExamsTypes; - return ChangeNotifierProvider.value( - value: examProvider, - child: ExamFilterForm( - Map.from(filteredExamsTypes), - ), + return LazyConsumer( + builder: (context, examProvider) { + return ExamFilterForm( + Map.from(examProvider.filteredExamsTypes), + (newFilteredExams) { + examProvider.setFilteredExams(newFilteredExams); + }); + }, ); }, ); diff --git a/uni/test/integration/src/exams_page_test.dart b/uni/test/integration/src/exams_page_test.dart index 4e37ebd3f..390a9590c 100644 --- a/uni/test/integration/src/exams_page_test.dart +++ b/uni/test/integration/src/exams_page_test.dart @@ -157,11 +157,6 @@ void main() { await tester.tap(find.byWidget(mtCheckboxTile)); await tester.pumpAndSettle(); - final okButton = find.widgetWithText(ElevatedButton, 'Confirmar'); - expect(okButton, findsOneWidget); - - await tester.tap(okButton); - await tester.pumpAndSettle(); expect(find.byKey(Key('$sdisExam-exam')), findsNothing);