ListTile does not show radio buttons

佐手、 提交于 2020-04-18 05:47:59

问题


I want to display 2 radio buttons horizontally on the subtitle of the ListTile. I can see only one Radio Button

List<QuestionsOptions> optionsList = [
QuestionsOptions(
  index: 1,
  name: "Yes",
),
QuestionsOptions(
  index: 0,
  name: "No",
),
];

回答1:


You need to use the Horizontal listview of the flutter by the help of the ListView.builder and set the scrollDirection: Axis.horizontal for the horizontal scroll and for the checkbox we need to use the RadioListTile for the radio button with text functionality.Please check my below code for it.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';

class HomeScreen extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomeScreen();
  }
}

class _HomeScreen extends State<HomeScreen> {
  List<String> selectedList = new List<String>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");
    selectedList.add("");

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
        height: MediaQuery.of(context).size.height * 0.29,
        child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: selectedList.length,
            itemBuilder: (context, index) {
              return Row(
                children: <Widget>[
                  Container(
                    width: MediaQuery.of(context).size.width * 0.4,
                    color: Colors.greenAccent,
                    child: Column(
                      children: <Widget>[
                        SizedBox(
                          height: 10.0,
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 10.0),
                          child:Align(

                            alignment: Alignment.topLeft,
                            child: Text("Question ${index}"),
                          ) ,
                        )
                        ,
                        RadioListTile(
                          groupValue: selectedList[index]==""?0:
                          selectedList[index]=="0"?false:true,
                          title: Text('Yes'),
                          value: true,
                          onChanged: (val) {
                            setState(() {
                              selectedList[index] = "1";

                            });

                          },
                        ),
                        RadioListTile(
                          groupValue: selectedList[index]==""?0:
                          selectedList[index]=="0"?false:true,
                          title: Text('No'),
                          value: false,
                          onChanged: (val) {
                            setState(() {
                              selectedList[index] = "0";
                            });

                          },
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    width: 10.0,
                  )
                ],
              );
            }),
      ),
    );
  }

    checkValue(int index, var selectedVale) {
    setState(() {
      if (selectedList[index] == "1") {
        selectedList[index] = "1";
      }
      else  if (selectedList[index] == "2") {
        selectedList[index] = "2";

      }
      else{
        selectedList[index] = "0";

      }
    });

  }
}

And from above code output will be following



来源:https://stackoverflow.com/questions/61264521/listtile-does-not-show-radio-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!