How To Prevent Button Double Tap Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 30-10-2020

The sample Google flutter code below illustrate how to prevent button double tap in flutter. Depending on the use case, you disable a button click to prevent The Future _onWillPop() will return a flutter toast which will return either True whenever user double click on the back button Or False Nothing will happen. You can see the full source code of the project here

How To Prevent Button Double Tap Using Flutter Android App

Prevent Button Double Tap
Complete Code For Prevent Button Double Tap In Flutter
main.dart

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() {
    return new MyHomePageState();
  }
}

class MyHomePageState extends State<MyHomePage> {
  bool _isButtonTapped = false;

  _onTapped() {
    setState(() => _isButtonTapped =
    !_isButtonTapped);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.deepPurple[900],
        title: Text('Prevent Button Double Tap'),
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.deepPurple[900],
          child: Text('Tap Once',style: TextStyle(color: Colors.white),),
          onPressed: _isButtonTapped
              ? null
              : _onTapped,
        ),
      ),
    );
  }
}

 

Related Post