Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

dart - how to implement button with timeout in flutter

User can tap button until the end of timer. I don't know how to start the layout. Does flutter sdk have any built in (or similar to be implemented) widgets for this case.

enter image description here

question from:https://stackoverflow.com/questions/65902029/how-to-implement-button-with-timeout-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can easily achieve it with a CustomPainter widget. Check the source code below and tweak it to your needs.

enter image description here


import 'dart:async';

import 'package:flutter/material.dart';

class MyProgressButton extends StatefulWidget {
  @override
  _MyProgressButtonState createState() => _MyProgressButtonState();
}

class _MyProgressButtonState extends State<MyProgressButton> {
  Timer _timer;
  int _progress = 0;
  int _totalActionTimeInSeconds = 3;

  void _initCounter() {
    _timer?.cancel();
    _progress = 0;
    _timer = Timer.periodic(const Duration(milliseconds: 50), (_) {
      setState(() => _progress += 50);

      if (Duration(milliseconds: _progress).inSeconds >= _totalActionTimeInSeconds) {
        _timer.cancel();
        // Do something
      }
    });
  }

  void _stopCounter() {
    _timer?.cancel();
    setState(() => _progress = 0);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) => _initCounter(),
      onTapUp: (_) => _stopCounter(),
      child: CustomPaint(
        painter: _MyCustomButtonPainter((_progress / 1000) / _totalActionTimeInSeconds),
        child: Container(
          alignment: Alignment.center,
          width: 500.0,
          height: 200.0,
          child: Text('Press me'),
        ),
      ),
    );
  }
}

class _MyCustomButtonPainter extends CustomPainter {
  const _MyCustomButtonPainter(this.progress);

  final double progress;

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()..color = Colors.grey;
    final double buttonWidth = size.width;
    final double buttonHeight = size.height;

    canvas.drawRRect(
      RRect.fromRectAndRadius(
        Rect.fromLTRB(0.0, 0.0, buttonWidth, buttonHeight),
        Radius.circular(5.0),
      ),
      paint,
    );

    paint.color = Colors.green;

    canvas.drawRRect(
      RRect.fromRectAndRadius(
        Rect.fromLTRB(0.0, 0.0, progress * buttonWidth, buttonHeight),
        Radius.circular(5.0),
      ),
      paint,
    );
  }

  @override
  bool shouldRepaint(_MyCustomButtonPainter oldDelegate) => this.progress != oldDelegate.progress;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...