我基本上想创建一个充满动态创建的单选按钮的表,我可以在其中处理它们的单个点击。
行标题和列标题都将包含其自己的文本标题,而随附的单元格将包含radioButtons。
答案 0 :(得分:0)
首先,您必须创建具有适当标识符的单选按钮的数组类型: 3 * 3单选按钮表就像这样:
* * *
* * *
* * *
RadioButton [,] r=new RadioButton[3,3];
然后设置位置并调用in的初始化方法:
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
r[i,j]=new RadioButton();
r[i,j].Tag=I.toString()+"-"+j.toString();
r[i,j].top=30+j*30;
r[i,j].top=30+i*30;
r[i,j].click+=new EvantHandler(*/the method that you whant to call when click in each radio button*/);
}
}
当调用事件处理程序方法时,您可以轻松找到单击了哪个单选按钮!从每个单选按钮的标签值开始
答案 1 :(得分:0)
我已经能够回答问题。查找随附的代码和屏幕截图供您细读。当屏幕为纵向时为水平滚动,而在横向模式下为垂直滚动。享受吧!
import 'package:flutter/material.dart';
class TableView extends StatefulWidget {
@override
_TableViewState createState() => _TableViewState();
}
class _TableViewState extends State<TableView> {
List rowHeaders = new List();
List columnHeaders = new List();
Map selected = new Map();
@override
void initState() {
super.initState();
saveHeaders(); //Iterate and store all Row and column Headers
}
saveHeaders() {
//Saving All Headers
columnHeaders.addAll(["Excellent", "Very Good", "Good", "Poor"]);
rowHeaders.addAll(["MTN", "Vodafone", "Tigo", "Expresso", "Glo"]);
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: new Text("TableView"),
),
body: new OrientationBuilder(builder: (context, orientation) {
return Center(
child: SingleChildScrollView(
scrollDirection: orientation ==
Orientation
.portrait //Handle Scroll when Orientation is changed
? Axis.horizontal
: Axis.vertical,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
color: Colors.blueGrey[200],
padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//headers
new Container(
margin: EdgeInsets.all(0.0),
child: new Row(
children: [
new Container(
alignment: FractionalOffset.center,
width: 140.0,
margin: EdgeInsets.all(0.0),
padding: const EdgeInsets.only(
top: 5.0, bottom: 5.0, right: 3.0, left: 3.0),
child: Text(
//Leave an empty text in Row(0) and Column (0)
"",
style: TextStyle(color: Colors.grey[800]),
textAlign: TextAlign.center,
),
)
]..addAll(columnHeaders
.map((header) => new Container(
alignment: FractionalOffset.center,
width: 120.0,
margin: EdgeInsets.all(0.0),
padding: const EdgeInsets.only(
top: 5.0,
bottom: 5.0,
right: 3.0,
left: 3.0),
child: new Text(
header,
style:
TextStyle(color: Colors.grey[800]),
textAlign: TextAlign.center,
),
))
.toList())),
),
],
),
)
]..addAll(createRows()), //Create Rows
),
));
}));
}
List<Widget> createRows() {
List<Widget> allRows = new List(); //For Saving all Created Rows
for (int i = 0; i < rowHeaders.length; i++) {
List<Widget> singleRow = new List(); //For creating a single row
for (int j = 0; j < columnHeaders.length; j++) {
singleRow.add(Container(
alignment: FractionalOffset.center,
width: 120.0,
padding: const EdgeInsets.only(
top: 6.0, bottom: 6.0, right: 3.0, left: 3.0),
child: Radio(
value: j, //Index of created Radio Button
groupValue: selected[rowHeaders[i]] !=
null //If groupValue is equal to value, the radioButton will be selected
? selected[rowHeaders[i]]
: "",
onChanged: (value) {
this.setState(() {
selected[rowHeaders[i]] =
value; //Adding selected rowName with its Index in a Map tagged "selected"
print("${rowHeaders[i]} ==> $value");
});
},
)));
}
//Adding single Row to allRows widget
allRows.add(new Container(
child: new Row(
children: [
new Container(
alignment: FractionalOffset.centerLeft,
width: 140.0,
padding: const EdgeInsets.only(
top: 6.0, bottom: 6.0, right: 3.0, left: 10.0),
child:
Text(rowHeaders[i], style: TextStyle(color: Colors.grey[800])),
)
]..addAll(singleRow), //Add single row here
)));
}
return allRows; //Return all single rows
}
}