如何在颤振中选择默认单选按钮

时间:2021-02-09 15:14:58

标签: flutter android-studio radio-button

我在我的 flutter 项目中实现了三个单选按钮。但是在执行应用程序后,没有一个单选按钮被选中。任何人都可以帮助我哪里出错了!

虽然我点击了单选按钮,但没有被选中。我无法弄清楚原因。请帮我解决它。这是我的代码:

class AdminHomeContent extends StatefulWidget {

  @override
  _AdminHomeContentState createState() => _AdminHomeContentState();
}

class _AdminHomeContentState extends State<AdminHomeContent> {

  static final Map<String, Color> options = {
    'All': Colors.black, 'Cancelled':Colors.red,
    'Pending': Colors.yellow, 'Visited': Colors.green[900],
  };

  List keyList = options.keys.toList();
  List keyColor = options.values.toList();

  String selectedOption = 'All';

  int groupValue = 0 ;

  void buttonValue(int v){
    setState(() {
 groupValue = v ;
    });
  }
 @override
    Widget build(BuildContext context) {
      return ChangeNotifierProvider<PatientDataNotifier>(
        create: (context) => PatientDataNotifier(),
        child: MaterialApp(
            home: Scaffold(
            
             // some of my other codes
             ----------
             -----------


//method that defines three radio buttons
 Future<dynamic> getStatus(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: SingleChildScrollView(
              child: Container(
                child: Column(
                  children: [
                    Row(
                      children: [
                        Radio(value: 1,
                            activeColor: Colors.blue,
                            groupValue: groupValue,
                            onChanged: (int v){
                          print(v);
                              buttonValue(v);
                            }
                        ),
                        Text(keyList[2] , style: TextStyle(color: keyColor[2]),)
                      ],
                    ),
                    Row(
                      children: [
                        Radio(value: 2,
                            activeColor: Colors.blue,
                            groupValue: groupValue,
                            onChanged: (int v) {
                          print(v);
                              buttonValue(v);
                            }
                        ),
                        Text(keyList[3] , style: TextStyle(color: keyColor[3]))
                      ],
                    ),
                    Row(
                      children: [
                        Radio(value: 3,
                            activeColor: Colors.blue,
                            groupValue: groupValue,
                            onChanged: (int v) {
                          print(v);
                              buttonValue(v);
                            }
                        ),
                        Text(keyList[1] , style: TextStyle(color: keyColor[1]))
                      ],
                    )
                  ],
                ),
              ),
            ),
          );
        }
    );
  }

     // some codes
   ------------------
-----------------

//调用 Text

onTap() 属性内的 getStatus()
GestureDetector(
                            child: Text(patient.status,
                              style: getTextStyle('Poppins-Regular',15.0, FontWeight.bold, options[status]),
                            ),
                            onTap: () async{
                              await getStatus(context);
                            },
                          ),
                       }
                    }

here is my output

即使点击单选按钮也没有被选中。请帮我解决。

1 个答案:

答案 0 :(得分:0)

首先,默认情况下没有选择 Radio,因为您的初始 groupValue0 而您的 Radio 都没有这个 value。< /p>

这里是一个完整的工作示例 Radio

  class MyRadio extends StatefulWidget {
  @override
  _MyRadioState createState() => _MyRadioState();
}

int groupValue = 1;

class _MyRadioState extends State<MyRadio> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              Radio(
                value: 1,
                activeColor: Colors.blue,
                groupValue: groupValue,
                onChanged: (int v) {
                  buttonValue(v);
                },
              ),
              Radio(
                value: 2,
                activeColor: Colors.blue,
                groupValue: groupValue,
                onChanged: (int v) {
                  buttonValue(v);
                },
              ),
              Radio(
                value: 3,
                activeColor: Colors.blue,
                groupValue: groupValue,
                onChanged: (int v) {
                  buttonValue(v);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  void buttonValue(int v) {
    setState(() {
      groupValue = v;
    });
  }
}