@ -10,17 +10,32 @@ import 'after_layout_mixin.dart';
/ / / text widget for long texts without overlaping or overflow . elipsis
/ / / text widget for long texts without overlaping or overflow . elipsis
/ / / [ AutoScrollText ] supports both directions [ Axis . horizontal ] and [ Axis . vertical ]
/ / / [ AutoScrollText ] supports both directions [ Axis . horizontal ] and [ Axis . vertical ]
class AutoScrollText extends StatefulWidget {
class AutoScrollText extends StatefulWidget {
/ / / [ Text . text ] of [ AutoScrollText ]
final String text ;
final String text ;
/ / / [ TextStyle ] of [ Text ]
final TextStyle ? textStyle ;
final TextStyle ? textStyle ;
final Axis scrollAxis ;
/ / / [ SingleChildScrollView . scrollDirection ] default value [ Axis . horizontal ]
final Axis scrollDirection ;
/ / / [ Curve ] of scroll animation
final Curve curve ;
final Curve curve ;
/ / / Distance per tick
final double moveDistance ;
/ / / Reset timer
final int timerRest ;
const AutoScrollText ( {
const AutoScrollText ( {
super . key ,
super . key ,
required this . text ,
required this . text ,
this . textStyle ,
this . textStyle ,
this . scrollAxis = Axis . horizontal ,
this . scrollDirection = Axis . horizontal ,
this . curve = Curves . linear ,
this . curve = Curves . linear ,
this . moveDistance = 1.0 ,
this . timerRest = 100 ,
} ) ;
} ) ;
@ override
@ override
@ -43,39 +58,35 @@ class AutoScrollTextState extends State<AutoScrollText>
/ / / Repeatable timer
/ / / Repeatable timer
Timer ? timer ;
Timer ? timer ;
/ / / Distance per tick
final double _moveDistance = 1.0 ;
/ / / Reset timer
final int _timerRest = 100 ;
/ / / if text is to long for axis , define auto scroll action
/ / / if text is to long for axis , define auto scroll action
bool _isScrollable = false ;
bool _isScrollable = false ;
/ / / SingleChildScrollView key
/ / / SingleChildScrollView key
final GlobalKey _scrollKey = GlobalKey ( ) ;
final GlobalKey _scrollKey = GlobalKey ( ) ;
/ / / Default [ TextStyle ] if [ widget . textStyle ] is null
TextStyle get defaultTextStyle = > const TextStyle ( ) ;
@ override
@ override
void initState ( ) {
void initState ( ) {
textToScroll = widget . text ;
textToScroll = widget . text ;
super . initState ( ) ;
super . initState ( ) ;
}
}
TextStyle get defaultTextStyle = > const TextStyle ( ) ;
/ / / Timer for animation
/ / / Timer for animation
void _startTimer ( ) {
void _startTimer ( ) {
if ( _scrollKey . currentContext ! = null ) {
if ( _scrollKey . currentContext ! = null ) {
timer = Timer . periodic ( Duration ( milliseconds: _ timerRest) , ( timer ) {
timer = Timer . periodic ( Duration ( milliseconds: widget . timerRest ) , ( timer ) {
double maxScrollExtent = _scrollController . position . maxScrollExtent ;
double maxScrollExtent = _scrollController . position . maxScrollExtent ;
double pixels = _scrollController . position . pixels ;
double pixels = _scrollController . position . pixels ;
if ( pixels + _ moveDistance > = maxScrollExtent ) {
if ( pixels + widget . moveDistance > = maxScrollExtent ) {
position = 0 ;
position = 0 ;
_scrollController . jumpTo ( position ) ;
_scrollController . jumpTo ( position ) ;
}
}
position + = _ moveDistance;
position + = widget . moveDistance ;
_scrollController . animateTo ( position ,
_scrollController . animateTo ( position ,
duration: Duration ( milliseconds: _timerRest ) , curve: Curves . linear ) ;
duration: Duration ( milliseconds: widget . timerRest ) ,
curve: widget . curve ) ;
} ) ;
} ) ;
}
}
}
}
@ -89,7 +100,7 @@ class AutoScrollTextState extends State<AutoScrollText>
/ / / Text builder
/ / / Text builder
Widget _text ( ) {
Widget _text ( ) {
if ( widget . scrollAxis = = Axis . vertical ) {
if ( widget . scrollDirection = = Axis . vertical ) {
String newString = textToScroll . split ( " " ) . join ( " \n " ) ;
String newString = textToScroll . split ( " " ) . join ( " \n " ) ;
return Text (
return Text (
newString ,
newString ,
@ -115,7 +126,7 @@ class AutoScrollTextState extends State<AutoScrollText>
Widget build ( BuildContext context ) {
Widget build ( BuildContext context ) {
return SingleChildScrollView (
return SingleChildScrollView (
key: _scrollKey ,
key: _scrollKey ,
scrollDirection: widget . scrollAxis ,
scrollDirection: widget . scrollDirection ,
controller: _scrollController ,
controller: _scrollController ,
physics: _isScrollable
physics: _isScrollable
? const AlwaysScrollableScrollPhysics ( )
? const AlwaysScrollableScrollPhysics ( )