颤振 - 打字文本动画

时间:2017-05-28 07:03:52

标签: dart flutter

对于我的每个文本小部件,我实际上希望文本输入而不是立即显示它。有没有比使用变量更简单的方法并在setState()中添加它?

由于

3 个答案:

答案 0 :(得分:7)

这可能是AnimatedBuilder的一个很好的用例。这将允许您更轻松地控制键入动画的持续时间,并仅在长度更改时重建窗口小部件。这是一个如何做到这一点的例子。

PCAP file

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primaryColor: const Color.fromARGB(255, 0, 199, 0),
        accentColor: const Color.fromARGB(255, 222, 233, 226),
        brightness: Brightness.dark,
        canvasColor: Colors.black,
      ),
      home: new MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {

  Animation<int> _characterCount;

  int _stringIndex;
  static const List<String> _kStrings = const <String>[
    'Call trans opt: received. 2-19-98 13:24:18 REC:Log>',
    'Trace program running.',
    '[312]555-0690',
  ];
  String get _currentString => _kStrings[_stringIndex % _kStrings.length];

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);
    TextStyle textStyle = theme.textTheme.title.copyWith(
      fontFamily: 'Courier New',
      color: theme.primaryColor,
    );
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.navigate_next),
        onPressed: () async {
          AnimationController controller = new AnimationController(
            duration: const Duration(milliseconds: 4000),
            vsync: this,
          );
          setState(() {
            _stringIndex = _stringIndex == null ? 0 : _stringIndex + 1;
            _characterCount = new StepTween(begin: 0, end: _currentString.length)
              .animate(new CurvedAnimation(parent: controller, curve: Curves.easeIn));
          });
          await controller.forward();
          controller.dispose();
        },
      ),
      body: new Container(
        margin: new EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),
        child: _characterCount == null ? null : new AnimatedBuilder(
          animation: _characterCount,
          builder: (BuildContext context, Widget child) {
            String text = _currentString.substring(0, _characterCount.value);
            return new Text(text, style: textStyle);
          },
        ),
      ),
    );
  }
}

如果您计划使用此动画文本小部件,可以使用AnimatedWidget将其重构为单独的类。

答案 1 :(得分:0)

您可以使用此插件

animated_text_kit:

示例:https://github.com/aagarwal1012/Animated-Text-Kit

一小段代码:

for i in os.listdir(directory):
    data[i] = pd.read_csv(directory+ '/' +i, encoding="utf8")

答案 2 :(得分:-1)

您还可以查看typeitjs.com link。简单的实现

<p id="replaceStrings"></p>
new TypeIt('#replaceStrings', {
  strings: ["This is a great string.", "But here is a better one."],
  speed: 50,
  breakLines: false,
  waitUntilVisible: true
}).go();

简单的JS解决方案

<!DOCTYPE html>
<html>
<body>

<h1>Typewriter</h1>

<button onclick="typeWriter()">Click me</button>

<p id="demo"></p>

<script>
var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}
</script>

</body>
</html>