【问题标题】:Flutter: How do i disable double pressed on IconButtonFlutter:如何禁用 IconButton 上的双按
【发布时间】:2022-12-12 13:38:46
【问题描述】:

我在 persistentFooterButtons 中有一些 IconButton 导航到其他屏幕,但如果我在任何按钮上按两次并按后退按钮,它会重定向两次。如果我多次按下按钮并按下后退按钮,它会显示相同的页面。所以我只想点击一次,当它点击并显示页面时,按钮将禁用并且不会按下我认为它有效但我知道我该怎么做。请帮我解决这个问题。

这是我的代码:-

class Footer extends StatefulWidget {

Footer({Key? key}) : super(key: key);

@override
_Footer createState() => _Footer();
}

class _Footer extends State<Footer>{

 bool _isButtonTapped1 = false;
 bool _isButtonTapped2 = false;
 bool _isButtonTapped3 = false;
 bool _isButtonTapped4 = false;

 @override
 Widget build(BuildContext context){
   
  return MaterialApp(
  home: Scaffold(
    appBar:  AppBar()
    persistentFooterButtons: [

      Row(
       children: [

        IconButton(
          onPressed: () {
          Navigator.of(context).pushAndRemoveUntil(
              MaterialPageRoute(builder: (context) => const CardApp()),
                  (Route<dynamic> route) => false);

          },
          icon: SvgPicture.asset(
          'assets/images/f_home.svg',
          ),
        ),
        Spacer(),
        IconButton(
          onPressed: () {
          Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Matches()),
            );
          },
          icon: SvgPicture.asset(
          'assets/images/f_fav.svg',
          ),
        ),
        Spacer(),
        IconButton(
          onPressed: () {
          Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Message()),
            );
          },
          icon: SvgPicture.asset(
          'assets/images/f_chat.svg',
          ),
        ),
        Spacer(),
        IconButton(
          onPressed: () {
          Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Favorites()),
            );
          },
          icon: SvgPicture.asset(
          'assets/images/f_star.svg',
          ),
        ),


       ]
      )
    ],
  ),
  );
 
 }

}

这是我的 persistentFooterButtons 代码,它添加了四个按钮,每个按钮在自己的屏幕上导航。 请帮我在每个按钮上禁用多个onPressed。如果有人知道请帮助我

【问题讨论】:

  • 您可以使用计时器和标志来管理点击see here 或尝试Debouncing
  • 感谢您的回复,我使用了debouncing,但我不明白我如何导航其他屏幕并停止双击。因为每次点击都会改变它的状态。

标签: android flutter flutter-layout flutter-navigation flutter-onpressed


【解决方案1】:

如果你想防止用户多次按下,在 InkWell 小部件中,使用这个: onDoubleTap: (){},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2018-10-27
    • 2018-08-27
    • 1970-01-01
    • 2021-05-20
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多