0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flutter ボタンのonPressedをnullにしてるのにボタンが非活性にならない時

Posted at

はじめに

ボタンを非活性にする方法としてonPressednullを渡す方法があります。

が、、、

nullを渡してもボタンの色が灰色に変わらないやんけーーー!!!
という事態に陥って、ちょっとハマってしまったのでここに記録したいと思います!

三項演算子の位置が間違っていた。。

 ElevatedButton(
      onPressed: () {
        _inProgress ? null : decidePosition();
       },
        child: const Text('次へ!'),
      ),

特定の処理(今回の場合decidePosition())をしている最中に_inProgresstrueの時nullを渡し、ボタンが連打できないように非活性にしたかったです。
しかし、👆のコードでは非活性になりませんでした。。。

なぜだ????と思ったが、めっちゃうっかりミスしてました!

ElevatedButton(
  onPressed: null,
  child: const Text('次へ!'),
)

👆の場合は非活性になり、しっかりボタンの色が灰色に変化します。
ということは、、、、

ElevatedButton(
        onPressed: _inProgress
           ? null
           : () => decidePosition(),
        child: const Text('次へ!'),
     ),

これで無事_inProgresstrueの時ボタンを非活性にし灰色に変化させることができました。。。:sweat_smile:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?