簡體   English   中英

如何在 Flutter 中使用簡寫 IF 禁用單選按鈕?

[英]How can I disable a Radio button using a short hand IF in Flutter?

我有一個Radio ,我想在有條件的情況下禁用它,例如:

if (enabled == true){
*enable radio button*
} else {
*disable radio button*
}

編輯

我將添加我的類的完整代碼,因為我只添加了它的一部分。

這是完整的代碼:

import 'package:flutter/material.dart';

class RadioButton extends StatelessWidget {
  final String label;
  final int groupValue;
  final int value;
  final ValueChanged<int> onChanged;

  const RadioButton(
      {super.key,
      required this.label,
      required this.groupValue,
      required this.value,
      required this.onChanged});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Radio<int>(
                        groupValue: groupValue,
                        value: value,
                        onChanged: (int? newValue) {
                          onChanged(newValue!);
                        }),
                  ),
                  Text(label),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }
}

要禁用Radio按鈕,請將onChanged設置為null

return Radio(
      value: 1,
      groupValue: 1,
      onChanged: null,
    );
    

編輯:根據條件禁用它:

var isDisabled = true;
    Radio(
      value: 1,
      groupValue: 1,
      onChanged: isDisabled ? null : (value) {},
      

來自onChanged文檔

如果為空,單選按鈕將顯示為禁用。


您可以記住這個設置null的技巧,因為它還會禁用其他Icons /小部件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM