Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. and there is no isButtonDisabled prop just like in Radio widget. 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',
              ),
            ),
          ),
        )
      ],
    );
  }
}
like image 246
biniyam112 Avatar asked Jan 04 '21 08:01

biniyam112


1 Answers

Whenever onChange is null the component will become disabled. 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.

In the end it could look like:

onChanged: alreadyProvided ? null : (value) => value = 0,
like image 51
Thiago Cordeiro Avatar answered Oct 20 '22 03:10

Thiago Cordeiro