Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振中超时按钮的实现_Flutter_Dart_Button_Timer_Progress Bar - Fatal编程技术网

Flutter 颤振中超时按钮的实现

Flutter 颤振中超时按钮的实现,flutter,dart,button,timer,progress-bar,Flutter,Dart,Button,Timer,Progress Bar,用户可以点击按钮直到计时器结束。我不知道如何开始布局。对于这种情况,颤振sdk是否有任何内置(或类似的实现)小部件 试试这个: class Button extends StatefulWidget { @override _ButtonState createState() => _ButtonState(); } class _ButtonState extends State<Button> { double progress; Timer timer;

用户可以点击按钮直到计时器结束。我不知道如何开始布局。对于这种情况,颤振sdk是否有任何内置(或类似的实现)小部件

试试这个:

class Button extends StatefulWidget {
  @override
  _ButtonState createState() => _ButtonState();
}

class _ButtonState extends State<Button> {
  double progress;
  Timer timer;

  @override
  void initState() {
    timer = Timer.periodic(Duration(milliseconds: 50), (timer) {
      if (progress <= 0) return timer.cancel();
      setState(() {
        progress -= 0.0025;
      });
    });
    progress = 1;
    super.initState();
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(5),
      child: Container(
        height: 70,
        width: 200,
        child: Stack(
          alignment: Alignment.center,
          children: [
            SizedBox.expand(
              child: LinearProgressIndicator(
                value: progress,
                backgroundColor: Colors.grey,
                valueColor: AlwaysStoppedAnimation(Colors.green),
              ),
            ),
            Text(
              'Button',
              style: TextStyle(color: Colors.white),
            ),
          ],
        ),
      ),
    );
  }
}
class按钮扩展StatefulWidget{
@凌驾
_ButtonState createState()=>\u ButtonState();
}
类_ButtonState扩展状态{
双重进步;
定时器;
@凌驾
void initState(){
计时器=计时器。周期性(持续时间(毫秒:50),(计时器){

如果(progress您可以使用
CustomPainter
小部件轻松实现它。请检查下面的源代码,并根据您的需要进行调整


导入“dart:async”;
进口“包装:颤振/材料.省道”;
类MyProgressButton扩展StatefulWidget{
@凌驾
_MyProgressButtonState createState()=>\u MyProgressButtonState();
}
类_MyProgressButtonState扩展状态{
定时器(u定时器),;
int _progress=0;
int_totalActionTimeInSeconds=3;
void _initCounter(){
_计时器?.cancel();
_进度=0;
_计时器=计时器。周期性(常量持续时间(毫秒:50),(41;{
设置状态(()=>_进度+=50);
if(持续时间(毫秒):进度.unseconds>=\u TotalActionTimeUnseconds){
_timer.cancel();
//做点什么
}
});
}
void_stopCounter(){
_计时器?.cancel();
设置状态(()=>_进度=0);
}
@凌驾
小部件构建(构建上下文){
返回手势检测器(
onTapDown:()=>_initCounter(),
onTapUp:()=>_停止计数器(),
孩子:定制油漆(
画师:_MyCustomButtonPainter((_progress/1000)/_totalActionTimeInSeconds),
子:容器(
对齐:对齐.center,
宽度:500.0,
高度:200.0,
child:Text('Press me'),
),
),
);
}
}
类_MyCustomButtonPainer扩展CustomPainter{
const_MyCustomButtonPainter(本进度);
最终双倍进展;
@凌驾
空心油漆(帆布,尺寸){
最终油漆=油漆()…颜色=颜色。灰色;
最终双按钮宽度=尺寸。宽度;
最终双钮扣高度=尺寸。高度;
画布.绘图更正(
从矩形到半径(
垂直于LTRB(0.0,0.0,按钮宽度,按钮宽度),
圆形半径(5.0),
),
油漆,
);
paint.color=Colors.green;
画布.绘图更正(
从矩形到半径(
LTRB的矩形(0.0,0.0,进度*按钮宽度,按钮宽度),
圆形半径(5.0),
),
油漆,
);
}
@凌驾
bool应该重新绘制(\u MyCustomButtonPainter oldDelegate)=>this.progress!=oldDelegate.progress;
}

试试这个类似的答案事实上,我也是这么做的。哈哈哈!))

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;
}