typing text animation in flutter

 

widget.dart

import 'package:flutter/material.dart';
import 'package:matrimony/utils/colors.dart';
import 'package:matrimony/widgets/common/custom_text.dart';

class TypeWritingAnimationText extends StatefulWidget {
  final String text;
  final Duration duration;

  const TypeWritingAnimationText({
    Key? key,
    required this.text,
    required this.duration,
  }) : super(key: key);

  @override
  _TypeWritingAnimationTextState createState() =>
      _TypeWritingAnimationTextState();
}

class _TypeWritingAnimationTextState extends State<TypeWritingAnimationText>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  late String _animatedText;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: widget.duration,
    );

    _animation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeInOut,
      ),
    );

    _animatedText = '';

    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        final textLength = (_animation.value * widget.text.length).toInt();
        _animatedText = widget.text.substring(0, textLength);
        return CustomText(
          size: 22,
          text: _animatedText,
          textColor: blackColor,
        );
      },
    );
  }
}


how to call it


   TypeWritingAnimationText(
              text: 'MatchMaking Alogrithm',
              duration: Duration(seconds: 5),
            ),



Comments