import 'package:diplomaticquarterapp/services/robo_search/event_provider.dart';
import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart';
import 'package:flutter/material.dart';

class TyperAnimatedTextKit extends StatefulWidget {
  final String text;
  final int textLength;
  TyperAnimatedTextKit({this.text, this.textLength});
  @override
  _TyperState createState() => _TyperState();
}

class _TyperState extends State<TyperAnimatedTextKit>
    with TickerProviderStateMixin {
  Animation<int> _characterCount;
  AnimationController _controller;
  int _stringIndex;
  var event = RobotProvider();
  @override
  void initState() {
    _controller = new AnimationController(
      duration: const Duration(seconds: 7),
      vsync: this,
    );

    Future.delayed(const Duration(milliseconds: 500), () {
      setState(() {
        _stringIndex = _stringIndex == null ? 0 : _stringIndex + 1;
        _characterCount = new StepTween(begin: 0, end: widget.textLength)
            .animate(
                new CurvedAnimation(parent: _controller, curve: Curves.easeIn));
      });
      _controller.forward();
    });

    super.initState();
  }

  @override
  void didUpdateWidget(TyperAnimatedTextKit oldWidget) {
    super.didUpdateWidget(oldWidget);
    _controller = new AnimationController(
      duration: const Duration(seconds: 7),
      vsync: this,
    );

    setState(() {
      _stringIndex = _stringIndex == null ? 0 : _stringIndex + 1;
      _characterCount = new StepTween(begin: 0, end: widget.textLength).animate(
          new CurvedAnimation(parent: _controller, curve: Curves.easeIn));
    });
    _controller.forward();
  }

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

  @override
  Widget build(BuildContext context) {
    return Container(
        child: _characterCount == null
            ? null
            : new AnimatedBuilder(
                animation: _characterCount,
                builder: (BuildContext context, Widget child) {
                  String text = widget.text.substring(0, _characterCount.value);
                  return AppText(
                    text,
                    fontSize: 12,
                    textAlign: TextAlign.start,
                  );
                },
              ));
  }
}
