如何在Flutter / Dart中创建动态Radio-Group(Android)

2019年1月20日 24点热度 0条评论

下面的代码我曾经在简单的android中创建了Flutter / Dart的功能,我对Flutter / Android完全陌生

  public static void setCustomRadioGroup(LinearLayout layout, Context context, ArrayList<String> setName, ArrayList<View> viewList) {
        int sizeOfList = setName.size();
        final RadioButton[] radioButtons = new RadioButton[setName.size()];
        RadioGroup radioGroup = new RadioGroup(context);
        System.out.println(layout.getTag()+"_RadioGroup"+"   this is tag from dynamic layout creation");
        //set this in constant afterweard while dry run
        radioGroup.setTag(layout.getTag()+"_RadioGroup");
//      /*  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                LinearLayout.LayoutParams.MATCH_PARENT,
//                LinearLayout.LayoutParams.WRAP_CONTENT
//        );
//        layout.addView(radioGroup, params);*/

        //radioGroup.setTag();//create the RadioGroup
        if (setName.size() < 3) {
            radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        } else {
            radioGroup.setOrientation(RadioGroup.VERTICAL);
        }
        //or RadioGroup.VERTICAL
        for (int i = 0; i < setName.size(); i++) {
            radioButtons[i] = new RadioButton(context);
            radioButtons[i].setText(setName.get(i));
            String id = layout.getTag()+setName.get(i);
//            radioButtons[i].setId(id.hashCode());
           // Log.v("Selected", "New radio item id in Dynamic fiels: " + id.hashCode());

            radioGroup.addView(radioButtons[i]);
        }
        layout.addView(radioGroup);
        viewList.add(radioGroup);

解决方案如下:

参见flutter提供了两种创建RadioButton列表的方法,可以使用https://docs.flutter.io/flutter/material/RadioListTile-class.html
https://docs.flutter.io/flutter/material/Radio-class.html
如果您需要进一步的帮助,请告诉我