0
点赞
收藏
分享

微信扫一扫

区块链智能合约开发

我是小小懒 02-28 15:00 阅读 3
flutter

在Flutter开发中,实现页面按钮被点击后倒计时15秒,这15秒内按钮不能再次被点击的功能,可以通过使用Flutter的Timer类来实现。以下是一个简单的示例代码:

import 'package:flutter/material.dart';
import 'dart:async';

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

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _buttonEnabled = true;

  void _startCountdown() {
    setState(() {
      _buttonEnabled = false;
    });

    Timer(Duration(seconds: 15), () {
      setState(() {
        _buttonEnabled = true;
      });
    });
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Countdown Button Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _buttonEnabled ? _startCountdown : null,
          child: Text('Click Me'),
        ),
      ),
    );
  }
}

这段代码创建了一个按钮,点击按钮后会触发倒计时15秒的功能,期间按钮会被禁用。倒计时结束后按钮恢复可点击状态。

举报

相关推荐

0 条评论