Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

android - 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',
              ),
            ),
          ),
        )
      ],
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

hope this was helpful!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...