Advertisement
sourav8256

Untitled

Sep 12th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1.  
  2. child: TypingAnimation(
  3. text: 'Delivered! From Your Finger Tips',
  4. // style: TextStyle(
  5. // fontSize: 18.0,
  6. // color: Colors.white
  7. // ),
  8. )
  9.  
  10. ========== TypingAnimation.dart ====================
  11.  
  12.  
  13. import 'package:flutter/material.dart';
  14.  
  15. class TypingAnimation extends StatefulWidget {
  16. final String text;
  17.  
  18. TypingAnimation({required this.text});
  19.  
  20. @override
  21. _TypingAnimationState createState() => _TypingAnimationState();
  22. }
  23.  
  24. class _TypingAnimationState extends State<TypingAnimation> {
  25. String typedText = '';
  26. int textIndex = 0;
  27. bool showCursor = true; // Track cursor visibility
  28.  
  29. @override
  30. void initState() {
  31. super.initState();
  32. _startTypingAnimation();
  33. }
  34.  
  35. void _startTypingAnimation() {
  36. Future.delayed(const Duration(milliseconds: 200), () {
  37. setState(() {
  38. typedText = widget.text.substring(0, textIndex);
  39. textIndex++;
  40.  
  41. // Toggle cursor visibility
  42. showCursor = !showCursor;
  43.  
  44. if (textIndex <= widget.text.length) {
  45. _startTypingAnimation();
  46. } else {
  47. // Reset the animation to run in a loop
  48. textIndex = 0;
  49. _startTypingAnimation();
  50. }
  51. });
  52. });
  53. }
  54.  
  55. @override
  56. Widget build(BuildContext context) {
  57. // Add a cursor "|" at the end if it should be shown
  58. final textWithCursor = showCursor ? '$typedText|' : typedText;
  59.  
  60. return Positioned(
  61. left: 0.0,
  62. right: 0,
  63. top: 86.0,
  64. child: Center(
  65. child: Text(
  66. textWithCursor,
  67. style: TextStyle(
  68. fontSize: 18.0,
  69. color: Colors.white,
  70. ),
  71. ),
  72. ),
  73. );
  74. }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement