【问题标题】:How to select a default radio button in flutter如何在颤动中选择默认单选按钮
【发布时间】:2021-05-13 05:55:23
【问题描述】:

我在我的颤振项目中实现了三个单选按钮。但是在执行应用程序后,没有一个单选按钮显示为选中状态。任何人都可以帮我解决我哪里出错了!

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

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

即使单击单选按钮也不会被选中。请帮我解决。

【问题讨论】:

    标签: flutter android-studio radio-button


    【解决方案1】:

    首先,默认情况下不会选择任何Radio,因为您的初始groupValue0,而您的Radio 中没有一个value

    这是一个完整的工作示例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;
        });
      }
    }
    

    【讨论】:

    • BENALI-RAGI 在点击其他收音机后仍然没有改变它的状态。默认情况下,它显示选择的第一个收音机。我希望在单击时也选择其他收音机。请帮忙
    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多