diff --git a/lib/data/tasks.dart b/lib/data/tasks.dart index 6a8e309..1ac5e73 100644 --- a/lib/data/tasks.dart +++ b/lib/data/tasks.dart @@ -50,7 +50,7 @@ final mockTasks = [ 'Купить что-то, где-то, зачем-то, но зачем не очень понятно, но точно чтобы показать как обрезается текст в этом поле', priority: Priority.high, deadline: DateTime(2024, 09, 23), - isDone: true, + isDone: false, ), Task( text: 'Купить что-то, где-то, зачем-то, но зачем не очень понятно', diff --git a/lib/l10n/app_ru.arb b/lib/l10n/app_ru.arb index 160f16d..18e84a1 100644 --- a/lib/l10n/app_ru.arb +++ b/lib/l10n/app_ru.arb @@ -20,5 +20,5 @@ "addTaskPriorityHigh": "Высокий", "addTaskDeadline": "Сделать до", "noTasks": "Вы ещё не добавили ни одной задачи", - "noDoneTasks": "Нет ни одной выполненной задачи\nДля просмотра всех дел отключите фильтр" + "onlyDoneTasksLeft": "Все задачи выполнены!\nДля просмотра завершённых дел отключите фильтр" } \ No newline at end of file diff --git a/lib/presentation/home_screen/home_screen.dart b/lib/presentation/home_screen/home_screen.dart index c6ca38f..25e3b6f 100644 --- a/lib/presentation/home_screen/home_screen.dart +++ b/lib/presentation/home_screen/home_screen.dart @@ -2,6 +2,7 @@ import 'package:app/data/tasks.dart'; import 'package:app/domain/models/task.dart'; import 'package:app/l10n/l10n_extension.dart'; import 'package:app/presentation/add_task_screen/add_task_screen.dart'; +import 'package:app/presentation/home_screen/widgets/done_tasks_visibility_button.dart'; import 'package:app/presentation/home_screen/widgets/header.dart'; import 'package:app/presentation/home_screen/widgets/task_list.dart'; import 'package:app/presentation/home_screen/widgets/top_bar.dart'; @@ -27,17 +28,23 @@ class HomeScreen extends StatefulWidget { aspect: _Aspects.tasksToDisplay, ).tasksToDisplay; + static int doneTaskCountOf(BuildContext context) => + _HomeScreenInheritedModel.of( + context, + aspect: _Aspects.doneTaskCount, + ).doneTaskCount; + static bool showAppBarOf(BuildContext context) => _HomeScreenInheritedModel.of( context, aspect: _Aspects.showAppBar, ).showAppBar; - static bool showOnlyDoneTasksOf(BuildContext context) => + static bool showDoneTasksOf(BuildContext context) => _HomeScreenInheritedModel.of( context, - aspect: _Aspects.showOnlyDoneTasks, - ).showOnlyDoneTasks; + aspect: _Aspects.showDoneTasks, + ).showDoneTasks; @override State createState() => HomeScreenState(); @@ -47,10 +54,13 @@ class HomeScreenState extends State { final scrollController = ScrollController(); late List tasks; var showAppBar = false; - var showOnlyDoneTasks = false; + var showDoneTasks = false; List get tasksToDisplay => - showOnlyDoneTasks ? tasks.where((task) => task.isDone).toList() : tasks; + showDoneTasks ? tasks : tasks.where((task) => !task.isDone).toList(); + + int get doneTaskCount => + tasks.fold(0, (acc, task) => acc + (task.isDone ? 1 : 0)); @override void initState() { @@ -94,12 +104,10 @@ class HomeScreenState extends State { }); } - void toggleShowOnlyDoneTasks() { + void toggleShowDoneTasks() { setState(() { - showOnlyDoneTasks = !showOnlyDoneTasks; + showDoneTasks = !showDoneTasks; }); - scrollController.animateTo(0, - duration: appTopBarAnimationDuration, curve: curve); } Future createTask() async { @@ -112,9 +120,6 @@ class HomeScreenState extends State { } } - int countDoneTasks() => - tasks.fold(0, (acc, task) => acc + (task.isDone ? 1 : 0)); - @override Widget build(BuildContext context) { return _HomeScreenInheritedModel( @@ -143,22 +148,25 @@ class HomeScreenState extends State { enum _Aspects { tasksToDisplay, + doneTaskCount, showAppBar, - showOnlyDoneTasks, + showDoneTasks, } class _HomeScreenInheritedModel extends InheritedModel<_Aspects> { final HomeScreenState state; final List tasksToDisplay; + final int doneTaskCount; final bool showAppBar; - final bool showOnlyDoneTasks; + final bool showDoneTasks; _HomeScreenInheritedModel({ required this.state, required super.child, }) : tasksToDisplay = state.tasksToDisplay, + doneTaskCount = state.doneTaskCount, showAppBar = state.showAppBar, - showOnlyDoneTasks = state.showOnlyDoneTasks; + showDoneTasks = state.showDoneTasks; static _HomeScreenInheritedModel of( BuildContext context, { @@ -189,10 +197,12 @@ class _HomeScreenInheritedModel extends InheritedModel<_Aspects> { ) => (dependencies.contains(_Aspects.tasksToDisplay) && tasksToDisplay != oldWidget.tasksToDisplay) || + (dependencies.contains(_Aspects.doneTaskCount) && + doneTaskCount != oldWidget.doneTaskCount) || (dependencies.contains(_Aspects.showAppBar) && showAppBar != oldWidget.showAppBar) || - (dependencies.contains(_Aspects.showOnlyDoneTasks) && - showOnlyDoneTasks != oldWidget.showOnlyDoneTasks); + (dependencies.contains(_Aspects.showDoneTasks) && + showDoneTasks != oldWidget.showDoneTasks); } class _Content extends StatelessWidget { @@ -204,13 +214,8 @@ class _Content extends StatelessWidget { return CustomScrollView( controller: HomeScreen.of(context).scrollController, slivers: [ - SliverToBoxAdapter( - child: Header( - doneTaskCount: HomeScreen.of(context).countDoneTasks(), - shouldShowOnlyDoneTasks: HomeScreen.showOnlyDoneTasksOf(context), - toggleShowOnlyDoneTasks: - HomeScreen.of(context).toggleShowOnlyDoneTasks, - ), + const SliverToBoxAdapter( + child: Header(), ), tasksToDisplay.isEmpty ? const SliverFillRemaining( @@ -241,17 +246,9 @@ class _TopBar extends StatelessWidget { duration: appTopBarAnimationDuration, child: TopBar( title: context.l10n.appTitle, - trailing: Padding( - padding: const EdgeInsets.only(right: 16), - child: IconButton( - onPressed: HomeScreen.of(context).toggleShowOnlyDoneTasks, - icon: Icon( - HomeScreen.showOnlyDoneTasksOf(context) - ? Icons.visibility_off - : Icons.visibility, - color: context.appColors.blue, - ), - ), + trailing: const Padding( + padding: EdgeInsets.only(right: 16), + child: DoneTasksVisibilityButton(), ), ), ), @@ -264,15 +261,18 @@ class _Empty extends StatelessWidget { @override Widget build(BuildContext context) { - return Center( - child: Text( - HomeScreen.showOnlyDoneTasksOf(context) - ? context.l10n.noDoneTasks - : context.l10n.noTasks, - style: context.appTextStyles.subhead.copyWith( - color: context.appColors.labelSecondary, + return Padding( + padding: const EdgeInsets.all(32), + child: Center( + child: Text( + HomeScreen.showDoneTasksOf(context) + ? context.l10n.noTasks + : context.l10n.onlyDoneTasksLeft, + style: context.appTextStyles.subhead.copyWith( + color: context.appColors.labelSecondary, + ), + textAlign: TextAlign.center, ), - textAlign: TextAlign.center, ), ); } diff --git a/lib/presentation/home_screen/widgets/done_tasks_visibility_button.dart b/lib/presentation/home_screen/widgets/done_tasks_visibility_button.dart new file mode 100644 index 0000000..1ec44dc --- /dev/null +++ b/lib/presentation/home_screen/widgets/done_tasks_visibility_button.dart @@ -0,0 +1,20 @@ +import 'package:app/presentation/home_screen/home_screen.dart'; +import 'package:app/presentation/theme/app_theme_extensions.dart'; +import 'package:flutter/material.dart'; + +class DoneTasksVisibilityButton extends StatelessWidget { + const DoneTasksVisibilityButton({super.key}); + + @override + Widget build(BuildContext context) { + return IconButton( + onPressed: HomeScreen.of(context).toggleShowDoneTasks, + icon: Icon( + HomeScreen.showDoneTasksOf(context) + ? Icons.visibility_off + : Icons.visibility, + color: context.appColors.blue, + ), + ); + } +} diff --git a/lib/presentation/home_screen/widgets/header.dart b/lib/presentation/home_screen/widgets/header.dart index 9e51770..9a1c681 100644 --- a/lib/presentation/home_screen/widgets/header.dart +++ b/lib/presentation/home_screen/widgets/header.dart @@ -1,18 +1,11 @@ import 'package:app/l10n/l10n_extension.dart'; +import 'package:app/presentation/home_screen/home_screen.dart'; +import 'package:app/presentation/home_screen/widgets/done_tasks_visibility_button.dart'; import 'package:app/presentation/theme/app_theme_extensions.dart'; import 'package:flutter/material.dart'; class Header extends StatelessWidget { - final int doneTaskCount; - final bool shouldShowOnlyDoneTasks; - final VoidCallback toggleShowOnlyDoneTasks; - - const Header({ - super.key, - required this.doneTaskCount, - required this.shouldShowOnlyDoneTasks, - required this.toggleShowOnlyDoneTasks, - }); + const Header({super.key}); @override Widget build(BuildContext context) { @@ -35,7 +28,7 @@ class Header extends StatelessWidget { children: [ Expanded( child: Text( - context.l10n.tasksDone(doneTaskCount), + context.l10n.tasksDone(HomeScreen.doneTaskCountOf(context)), style: context.appTextStyles.body.copyWith( color: context.appColors.labelTertiary, ), @@ -43,15 +36,7 @@ class Header extends StatelessWidget { ), ), const SizedBox(width: 16), - IconButton( - onPressed: toggleShowOnlyDoneTasks, - icon: Icon( - shouldShowOnlyDoneTasks - ? Icons.visibility_off - : Icons.visibility, - color: context.appColors.blue, - ), - ), + const DoneTasksVisibilityButton(), ], ), ],