-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ add functionality to scroll to bottom button (#246)
- Loading branch information
1 parent
d57c5cc
commit 202142a
Showing
10 changed files
with
325 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/src/models/config_models/scroll_to_bottom_button_config.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:chatview/chatview.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// Configuration for the "Scroll to Bottom" button. | ||
class ScrollToBottomButtonConfig { | ||
ScrollToBottomButtonConfig({ | ||
this.backgroundColor, | ||
this.border, | ||
this.borderRadius, | ||
this.icon, | ||
this.scrollAnimationDuration, | ||
this.alignment, | ||
this.padding, | ||
this.onClick, | ||
this.buttonDisplayOffset, | ||
}); | ||
|
||
/// The background color of the button. | ||
final Color? backgroundColor; | ||
|
||
/// The border of the button. | ||
final Border? border; | ||
|
||
/// The border radius of the button. | ||
final BorderRadius? borderRadius; | ||
|
||
/// The icon displayed on the button. | ||
final Icon? icon; | ||
|
||
/// The duration of the scroll animation when the button is clicked. | ||
final Duration? scrollAnimationDuration; | ||
|
||
/// The alignment of the button on top of text field. | ||
final ScrollButtonAlignment? alignment; | ||
|
||
/// The padding around the button. | ||
final EdgeInsets? padding; | ||
|
||
/// The callback function to be executed when the button is clicked. | ||
final VoidCallback? onClick; | ||
|
||
/// The scroll offset after which the button is displayed. | ||
/// The button appears when the scroll position is greater than or equal to this value. | ||
final double? buttonDisplayOffset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import 'package:chatview/src/extensions/extensions.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class ScrollToBottomButton extends StatefulWidget { | ||
const ScrollToBottomButton({super.key}); | ||
|
||
@override | ||
ScrollToBottomButtonState createState() => ScrollToBottomButtonState(); | ||
} | ||
|
||
class ScrollToBottomButtonState extends State<ScrollToBottomButton> { | ||
bool isButtonVisible = false; | ||
ScrollController? scrollController; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
scrollController = chatViewIW?.chatController.scrollController; | ||
scrollController?.addListener(_updateScrollButtonVisibility); | ||
}); | ||
} | ||
|
||
void _updateScrollButtonVisibility() { | ||
if (!mounted) return; | ||
|
||
final double currentOffset = scrollController?.offset ?? 0; | ||
final double buttonDisplayOffset = | ||
chatListConfig.scrollToBottomButtonConfig?.buttonDisplayOffset ?? 300; | ||
final bool isOffsetCrossedLimit = currentOffset > buttonDisplayOffset; | ||
if (isOffsetCrossedLimit && !isButtonVisible) { | ||
setState(() { | ||
isButtonVisible = true; | ||
}); | ||
} else { | ||
setState(() { | ||
isButtonVisible = false; | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final scrollToBottomButtonConfig = | ||
chatListConfig.scrollToBottomButtonConfig; | ||
return TweenAnimationBuilder<double>( | ||
tween: Tween(begin: 0, end: isButtonVisible ? 1.0 : 0.0), | ||
duration: const Duration(milliseconds: 200), | ||
curve: Curves.easeInOut, | ||
builder: (context, scale, child) { | ||
return Transform.scale( | ||
scale: scale, | ||
child: InkWell( | ||
onTap: () { | ||
scrollToBottomButtonConfig?.onClick?.call(); | ||
final scrollController = | ||
chatViewIW?.chatController.scrollController; | ||
scrollController?.animateTo( | ||
0, | ||
duration: scrollToBottomButtonConfig?.scrollAnimationDuration ?? | ||
const Duration(milliseconds: 200), | ||
curve: Curves.linear, | ||
); | ||
}, | ||
child: Container( | ||
decoration: BoxDecoration( | ||
borderRadius: scrollToBottomButtonConfig?.borderRadius ?? | ||
BorderRadius.circular(50), | ||
border: scrollToBottomButtonConfig?.border ?? | ||
Border.all(color: Colors.grey), | ||
color: | ||
scrollToBottomButtonConfig?.backgroundColor ?? Colors.white, | ||
), | ||
padding: const EdgeInsets.all(4), | ||
child: scrollToBottomButtonConfig?.icon ?? | ||
const Icon( | ||
Icons.keyboard_arrow_down_rounded, | ||
color: Colors.grey, | ||
weight: 10, | ||
size: 30, | ||
), | ||
), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
scrollController?.removeListener(_updateScrollButtonVisibility); | ||
super.dispose(); | ||
} | ||
} |
Oops, something went wrong.