marquee.dart 1.86 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Dan Field's avatar
Dan Field committed
5 6 7 8 9 10 11 12 13 14
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/services.dart';

class _MarqueeText extends AnimatedWidget {
  const _MarqueeText({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
15
    final Animation<double> animation = listenable as Animation<double>;
Dan Field's avatar
Dan Field committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    return Container(
      margin: EdgeInsets.only(left: animation.value),
      child: const Text(
        'This is Marquee',
        softWrap: false,
      ),
    );
  }
}

class Marquee extends StatefulWidget {
  const Marquee({this.color});

  final Color color;

  @override
  State<StatefulWidget> createState() => MarqueeState();
}

class MarqueeState extends State<Marquee> with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      duration: const Duration(milliseconds: 2000),
      vsync: this,
    );
    animation = Tween<double>(begin: 0.0, end: 400.0).animate(controller);
    controller.repeat();
  }

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

  @override
  Widget build(BuildContext context) {
    return Material(
      color: widget.color,
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Column(
          children: <Widget>[
            Align(
              child: _MarqueeText(animation: animation),
              alignment: Alignment.centerLeft,
            ),
            const FlatButton(child: Text('POP'), onPressed: SystemNavigator.pop),
          ],
        ),
      ),
    );
  }
}