From 119f41770cd15aa5db316e389a88358d42cc3934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wr=C3=B3bel?= Date: Fri, 14 Oct 2022 03:23:33 +0200 Subject: [PATCH] update moveDistance property added timerRestproperty added defaultTextStyle added --- example/lib/main.dart | 2 +- lib/auto_scroll_text.dart | 43 ++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index f4f47af..6abb444 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -122,7 +122,7 @@ class _VerticalExampleState extends State { text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", textStyle: TextStyle(fontSize: 24), - scrollAxis: Axis.vertical, + scrollDirection: Axis.vertical, ), ], ), diff --git a/lib/auto_scroll_text.dart b/lib/auto_scroll_text.dart index 585f185..c8fbde6 100644 --- a/lib/auto_scroll_text.dart +++ b/lib/auto_scroll_text.dart @@ -10,17 +10,32 @@ import 'after_layout_mixin.dart'; /// text widget for long texts without overlaping or overflow.elipsis /// [AutoScrollText] supports both directions [Axis.horizontal] and [Axis.vertical] class AutoScrollText extends StatefulWidget { + /// [Text.text] of [AutoScrollText] final String text; + + /// [TextStyle] of [Text] final TextStyle? textStyle; - final Axis scrollAxis; + + /// [SingleChildScrollView.scrollDirection] default value [Axis.horizontal] + final Axis scrollDirection; + + /// [Curve] of scroll animation final Curve curve; + /// Distance per tick + final double moveDistance; + + /// Reset timer + final int timerRest; + const AutoScrollText({ super.key, required this.text, this.textStyle, - this.scrollAxis = Axis.horizontal, + this.scrollDirection = Axis.horizontal, this.curve = Curves.linear, + this.moveDistance = 1.0, + this.timerRest = 100, }); @override @@ -43,39 +58,35 @@ class AutoScrollTextState extends State /// Repeatable 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 bool _isScrollable = false; /// SingleChildScrollView key final GlobalKey _scrollKey = GlobalKey(); + /// Default [TextStyle] if [widget.textStyle] is null + TextStyle get defaultTextStyle => const TextStyle(); + @override void initState() { textToScroll = widget.text; super.initState(); } - TextStyle get defaultTextStyle => const TextStyle(); - /// Timer for animation void _startTimer() { 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 pixels = _scrollController.position.pixels; - if (pixels + _moveDistance >= maxScrollExtent) { + if (pixels + widget.moveDistance >= maxScrollExtent) { position = 0; _scrollController.jumpTo(position); } - position += _moveDistance; + position += widget.moveDistance; _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 /// Text builder Widget _text() { - if (widget.scrollAxis == Axis.vertical) { + if (widget.scrollDirection == Axis.vertical) { String newString = textToScroll.split("").join("\n"); return Text( newString, @@ -115,7 +126,7 @@ class AutoScrollTextState extends State Widget build(BuildContext context) { return SingleChildScrollView( key: _scrollKey, - scrollDirection: widget.scrollAxis, + scrollDirection: widget.scrollDirection, controller: _scrollController, physics: _isScrollable ? const AlwaysScrollableScrollPhysics()