rendering

Usage Mode 1 The default value is countdown

CountdownWidget(
  5.///Countdown time
  onClick: () { /// Click the callback of the event
    _skip2main();
  },
  onFinish: () { ///Countdown to complete callback_skip2main(); },)Copy the code

Usage 2 Modify rounded corners and copy


CountdownWidget(
  total: 10,
  content: "Sent",
  textColor: Colors.blue,
  borderRadius: 2, onClick: () { _skip2main(); }, onFinish: () { _skip2main(); },)Copy the code

Countdown implementation

import 'dart:async';

import 'package:bilibili_flutter/common/base/base_state.dart';
import 'package:bilibili_flutter/common/base/base_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';

///Used for the countdown button in splash interface

class CountdownWidget extends BiliWidget {
  ///A function is passed in the construct
  final VoidCallback? onClick;

  final VoidCallback? onFinish;
  final int total;
  final double borderRadius;
  final String content;
  final double? height;

  final Color? focusColor;
  final Color? hoverColor;
  final Color? highlightColor;

  CountdownWidget(
      {this.total = 5,
      Key? key,
      this.height = 40.this.onClick,
      this.onFinish,
      this.borderRadius = 20.this.content = "Countdown".this.focusColor,
      this.hoverColor,
      this.highlightColor})
      : super(key: key);

  @override
  State<CountdownWidget> createState() => _CountdownWidgetState();
}

class _CountdownWidgetState extends BiliState<CountdownWidget> {
  var _count = 0;

  late Timer _timer;

  ///Registration countdown
  @override
  void initState() {
    super.initState();
    var duration = const Duration(seconds: 1);
    _timer = Timer.periodic(duration, (timer) {
      if (_count < widget.total) {
        setState(() {
          _count++;
        });
      } else {
        widget.onFinish?.call();
        _timer.cancel();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    returnInkWell( focusColor: widget.focusColor, hoverColor: widget.hoverColor, highlightColor: widget.highlightColor, onTap: () { widget.onClick? .call(); }, child: SizedBox( height: widget.height, child: Card( elevation:1,
          shape: RoundedRectangleBorder(
            borderRadius:
                BorderRadius.all(Radius.circular(widget.borderRadius)),
          ),
          child: Center(
            child: Padding(
              padding: const EdgeInsets.only(left: 10, right: 10),
              child: Text("${widget.content}${widget.total - _count}s"),),),),),); }}Copy the code

Two base classes that depend on

import 'package:flutter/material.dart';

@immutable
abstract class BiliWidget extends StatefulWidget {
  BiliWidget({
    Key? key,
  }) : super(key: key);

  String param = "";

  void setParam(String param) {
    this.param = param; }}Copy the code
import 'package:flutter/material.dart';

import 'base_state.dart';

abstract class BiliState<T extends BiliWidget> extends State<T> {}
Copy the code

Program source code

Github.com/HaiYangCode…