Flutter_跑马灯

Flutter 跑马灯

/// description_begin.
/// 跑马灯, 根据滚动方向可以分为 横向滚动 和 纵向滚动。
/// 此页实现的跑马灯是 横向滚动的。
/// description_end.
import 'dart:async';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key) {
    debugPrint('Track_MyApp');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('跑马灯'),
        ),
        //body占屏幕的大部分。
        body: _buildBody(),
      ),
    );
  }

  Widget _buildBody() {
    return Container(
        margin: const EdgeInsets.only(left: 15, top: 10, right: 15),
        padding: const EdgeInsets.only(left: 15, right: 15),
        height: 36,
        decoration: BoxDecoration(
          color: const Color(0xFFAAAAAA),
          borderRadius: BorderRadius.circular(15),
        ),
        child: Row(
          children: [
            Container(
              margin: const EdgeInsets.only(right: 10),
              width: 15,
              height: 15,
              decoration: const BoxDecoration(
                color: Colors.blue,
              ),
            ),
            Expanded(
              child: MarqueeView(
                child: _buildText(
                    '手机用户155****0523借款成功 ( This text is to long to be shown in just one line. )'),
              ),
            ),
          ],
        ));
  }

  Widget _buildText(String txt) {
    return Text(
      txt,
      style: const TextStyle(
        fontSize: 13,
        color: Colors.white,
      ),
      maxLines: 1,
      overflow: TextOverflow.fade,
    );
  }
}

//////////////////////////////////////////////////

class MarqueeView extends StatefulWidget {
  final Duration pauseDuration, forwardDuration;
  final double scrollSpeed; //滚动速度(时间单位是秒)。
  final Widget child; //子视图。

  /// 注: 构造函数入参的默认值必须是常量。
  const MarqueeView(
      {Key? key,
      this.pauseDuration = const Duration(milliseconds: 100),
      this.forwardDuration = const Duration(milliseconds: 3000),
      this.scrollSpeed = 40.0,
      required this.child})
      : super(key: key);

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

class _MarqueeViewState extends State
    with SingleTickerProviderStateMixin {
  bool _validFlag = true;
  double _boxWidth = 0;
  final ScrollController _controller = ScrollController();

  @override
  void dispose() {
    debugPrint('Track_MarqueeView_dispose');
    _validFlag = false;
    _controller.dispose();
    super.dispose();
  }

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

  @override
  Widget build(BuildContext context) {
    /// 使用LayoutBuilder获取组件的大小。
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        _boxWidth = constraints.maxWidth;
        return SingleChildScrollView(
          // 禁止手动滑动。
          physics: const NeverScrollableScrollPhysics(),
          controller: _controller,
          scrollDirection: Axis.horizontal,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: _boxWidth),
            child: widget.child,
          ),
        );
      },
    );
  }

  void scroll() async {
    while (_validFlag) {
      debugPrint('Track_MarqueeView_scroll');
      await Future.delayed(widget.pauseDuration);
      if (_boxWidth <= 0) {
        continue;
      }
      _controller.jumpTo(0);
      // 两个方向: Curves.easeIn 和 Curves.easeOut 。
      await _controller.animateTo(_controller.position.maxScrollExtent,
          duration: Duration(
              seconds:
                  (_controller.position.maxScrollExtent / widget.scrollSpeed)
                      .floor()),
          curve: Curves.easeIn);
    }
  }
}

参考文章:
https://cloud.tencent.com/developer/ask/155300/answer/268377
https://www.cnblogs.com/qqcc1388/p/12405548.html
https://blog.csdn.net/weixin_42629287/article/details/107086670

你可能感兴趣的:(Flutter_跑马灯)