@@ -5,6 +5,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
55import 'package:firebase_auth/firebase_auth.dart' ;
66import 'package:flutter/cupertino.dart' ;
77import 'package:flutter/material.dart' ;
8+ import 'package:flutter/rendering.dart' ;
89import 'package:flutter/foundation.dart' ;
910import 'package:tarefas/l10n/app_localizations.dart' ;
1011import 'package:flutter_local_notifications/flutter_local_notifications.dart' ;
@@ -32,17 +33,13 @@ class _HomeScreenState extends State<HomeScreen>
3233 late AnimationController _fabAnimationController;
3334 late Animation <double > _fabAnimation;
3435 late Animation <Offset > _fabSlideAnimation;
35- bool _isFabVisible = true ;
36- double _lastScrollPosition = 0 ;
3736
3837 @override
3938 void initState () {
4039 super .initState ();
4140
4241 _scrollController = ScrollController ();
4342
44- _scrollController.addListener (_onScroll);
45-
4643 _fabAnimationController = AnimationController (
4744 duration: const Duration (milliseconds: 200 ),
4845 vsync: this ,
@@ -67,47 +64,25 @@ class _HomeScreenState extends State<HomeScreen>
6764
6865 @override
6966 void dispose () {
70- _scrollController.removeListener (_onScroll);
7167 _scrollController.dispose ();
7268 _fabAnimationController.dispose ();
7369 super .dispose ();
7470 }
7571
76- void _onScroll () {
77- final currentScrollPosition = _scrollController.position.pixels;
78- final scrollDelta = currentScrollPosition - _lastScrollPosition;
79-
80- if (scrollDelta.abs () > 5 ) {
81- if (scrollDelta > 0 && _isFabVisible) {
82- _hideFab ();
83- } else if (scrollDelta < 0 && ! _isFabVisible) {
84- _showFab ();
85- }
86- }
87-
88- _lastScrollPosition = currentScrollPosition;
89- }
90-
91- void _showFab () {
92- if (! _isFabVisible) {
93- if (mounted) {
94- setState (() {
95- _isFabVisible = true ;
96- });
97- }
98- _fabAnimationController.forward ();
99- }
100- }
101-
102- void _hideFab () {
103- if (_isFabVisible) {
104- if (mounted) {
105- setState (() {
106- _isFabVisible = false ;
107- });
72+ bool _onScrollNotification (ScrollNotification notification) {
73+ if (notification is UserScrollNotification ) {
74+ final ScrollDirection direction = notification.direction;
75+ if (direction == ScrollDirection .reverse) {
76+ if (_fabAnimationController.isCompleted) {
77+ _fabAnimationController.reverse ();
78+ }
79+ } else if (direction == ScrollDirection .forward) {
80+ if (_fabAnimationController.isDismissed) {
81+ _fabAnimationController.forward ();
82+ }
10883 }
109- _fabAnimationController.reverse ();
11084 }
85+ return false ;
11186 }
11287
11388 void _checkUser () async {
@@ -337,14 +312,6 @@ class _HomeScreenState extends State<HomeScreen>
337312 ),
338313 );
339314 }
340-
341- if (mounted) {
342- ScaffoldMessenger .of (context).showSnackBar (
343- SnackBar (
344- content: Text ('Falha ao atualizar a tarefa. Tente novamente.' ),
345- ),
346- );
347- }
348315 }
349316 }
350317
@@ -647,35 +614,41 @@ class _HomeScreenState extends State<HomeScreen>
647614 (a, b) => b.dateTime! .compareTo (a.dateTime! ),
648615 );
649616
650- return CustomScrollView (
651- controller: _scrollController,
652- slivers: [
653- SliverToBoxAdapter (
654- child: SizedBox (height: kToolbarHeight + 40 ),
655- ),
656- if (pendingTasks.isNotEmpty)
617+ return NotificationListener <ScrollNotification >(
618+ onNotification: _onScrollNotification,
619+ child: CustomScrollView (
620+ controller: _scrollController,
621+ slivers: [
657622 SliverToBoxAdapter (
658- child: _buildSectionHeader (appLocalizations.pendants ),
623+ child: SizedBox (height : kToolbarHeight + 40 ),
659624 ),
660- SliverList (
661- delegate: SliverChildBuilderDelegate (
662- (_, index) => buildTaskItem (pendingTasks[index], true ),
663- childCount: pendingTasks.length,
664- ),
665- ),
666- if (completedTasks.isNotEmpty)
667- SliverToBoxAdapter (
668- child: _buildSectionHeader (appLocalizations.completed),
625+ if (pendingTasks.isNotEmpty)
626+ SliverToBoxAdapter (
627+ child: _buildSectionHeader (appLocalizations.pendants),
628+ ),
629+ SliverList (
630+ delegate: SliverChildBuilderDelegate (
631+ (_, index) =>
632+ buildTaskItem (pendingTasks[index], true ),
633+ childCount: pendingTasks.length,
634+ ),
669635 ),
670- SliverList (
671- delegate: SliverChildBuilderDelegate (
672- (_, index) =>
673- buildTaskItem (completedTasks[index], false ),
674- childCount: completedTasks.length,
636+ if (completedTasks.isNotEmpty)
637+ SliverToBoxAdapter (
638+ child: _buildSectionHeader (
639+ appLocalizations.completed,
640+ ),
641+ ),
642+ SliverList (
643+ delegate: SliverChildBuilderDelegate (
644+ (_, index) =>
645+ buildTaskItem (completedTasks[index], false ),
646+ childCount: completedTasks.length,
647+ ),
675648 ),
676- ),
677- const SliverToBoxAdapter (child : SizedBox (height : 140 )) ,
678- ] ,
649+ const SliverToBoxAdapter (child : SizedBox (height : 140 ) ),
650+ ] ,
651+ ) ,
679652 );
680653 },
681654 ),
0 commit comments