简体   繁体   English

如何在 flutter 中禁用 RadioListTile 小部件

[英]How to make RadioListTile widget disabled in flutter

I'm using the RadioListTile widget in my application and I want to disable changing the value after some condition is satisfied.我在我的应用程序中使用RadioListTile小部件,我想在满足某些条件后禁用更改值。 and there is no isButtonDisabled prop just like in Radio widget.并且没有isButtonDisabled道具,就像Radio小部件中一样。 how can I disable this widget(stop the value from changing)?如何禁用此小部件(阻止值更改)?

By the time I was writing the above statement I figured it out and it was easy.当我写上面的声明时,我想通了,这很容易。 so Ill post the question and will answer it below.所以生病发布问题并将在下面回答。

import 'package:flutter/material.dart';

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  bool evaluate = false;
  int value;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        RadioListTile<int>(
          value: 0,
          isThreeLine: false,
          title: Text(
            'radio 1',
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
          groupValue: value,
          onChanged: (value) => evaluate ? null : value = 0,
          activeColor: Colors.green,
        ),
        RadioListTile<int>(
          value: 1,
          isThreeLine: false,
          title: Text(
            'radio 2',
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
          groupValue: value,
          onChanged: (value) => evaluate ? null : value = 1,
          activeColor: Colors.green,
        ),
        InkWell(
          onTap: () {
            evaluate = true;
          },
          child: Container(
            child: Center(
              child: Text(
                'Submit answer',
              ),
            ),
          ),
        )
      ],
    );
  }
}

Whenever onChange is null the component will become disabled.每当onChange为 null 时,组件将被禁用。 You should be able to do something link this instead:你应该能够做一些链接这个而不是:

onChanged: evaluate ? null : (value) => value = 0,

I would also rename evaluate to make it easier to understand, something like alreadyProvided , notAllowed , disabled or whatever makes sense, the most important thing is to really represent why it can't be changed.我还将重命名evaluate以使其更易于理解,例如alreadyProvidednotAlloweddisabled或任何有意义的东西,最重要的是真正代表它为什么不能更改。

In the end it could look like:最后它可能看起来像:

onChanged: alreadyProvided ? null : (value) => value = 0,

I think you have already figured out the answer from the code.我想你已经从代码中找到了答案。 the solution is to declare bool value evaluate in my case then on button click make that value true and setstate with that value and on the onChanged prop of the RadioListTile add this code:解决方案是在我的情况下声明 bool 值evaluate ,然后单击按钮使该值变为 true 并使用该值设置状态,并在RadioListTileonChanged道具上添加以下代码:

onChanged: (value) => evaluate ? null : value = 0,

hope this was helpful!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM