【发布时间】:2021-07-26 19:39:47
【问题描述】:
我想创建这样的用户界面
最后,我得到了所有选中的单选按钮的详细信息。
这是我的完整代码
当我尝试这样做时,所有单选按钮都转向一侧。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Affiche_grille extends StatefulWidget {
@override
_QuestionWidgetState createState() => _QuestionWidgetState();
}
int selectedRadio;
int _groupValue = -1;
class _QuestionWidgetState extends State<Affiche_grille> {
List<RadioModel> sampleData = new List<RadioModel>();
@override
void initState() {
super.initState();
sampleData.add(new RadioModel(false, 'A', 'April 18', 'text1'));
sampleData.add(new RadioModel(false, 'B', 'April 17', 'text2'));
sampleData.add(new RadioModel(false, 'C', 'April 16', 'text3'));
sampleData.add(new RadioModel(false, 'D', 'April 15', 'text4'));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("ListItem"),
),
body: ListView.builder(
itemCount: sampleData.length,
itemBuilder: (context, index) => ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
_myRadioButton(
title: "Checkbox 0",
value: 0,
onChanged: (newValue) => setState(() => _groupValue = newValue),
),
_myRadioButton(
title: "Checkbox 1",
value: 1,
onChanged: (newValue) => setState(() => _groupValue = newValue),
),
],
),
),
);
}
}
Widget _myRadioButton({String title, int value, Function onChanged}) {
return Radio(
value: value,
groupValue: _groupValue,
onChanged: onChanged,
);
}
class RadioModel {
bool isSelected;
final String buttonText;
final String text;
final String text1;
RadioModel(this.isSelected, this.buttonText, this.text, this.text1);
}
此应用程序 UI 由此 CODE 制成。
【问题讨论】:
标签: flutter dart flutter-layout radio radio-group