我有一个像app这样的网格,它的行和列数是可变的。问题是轻松获得用户点击的小部件。我为网格中的每个单元格定义了一个容器,并添加了一个Tap处理程序,该处理程序将返回TapDownDetails,这给了我globalPosition.dx和dy。我真的想要父级窗口小部件,是的,我可以为网格中的所有窗口小部件建立偏移列表,然后从列表中找出合适的偏移量。我通过在布局后的时间建立一个列表来做到这一点: WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
_afterLayout(_) {
int i = 0;
bb = new List<Offset>(keys.length);
this.keys.forEach((key) {
RenderBox cell1 = key.currentContext.findRenderObject();
bb[i++] = cell1.localToGlobal(Offset.zero);
});
我将密钥添加到构建代码中,并在点击代码中进行测试。现在,此方法有效,但肯定会丢失明显的方法,并且有更好的解决方案
我的代码
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<myCell> slots = new List<myCell>();
bool inputMode = true;
List<BE> BEs = new List<BE>();
Map<int,List<int>> reserved ;
int timeRows = 4;
int court = 0;
List<GlobalKey> keys = new List<GlobalKey>();
List<Offset> bb = List<Offset>();
MyHomePage(){
initState();
}
void initState() {
setState(() {
inputMode = false;
slots.add(new myCell(true,TypeBooking.Booked,TimeInterval.hour,1,6));
court +=1;
slots.add(new myCell(true,TypeBooking.Booked,TimeInterval.hour,timeRows*court*4+2,6));
court +=1;
slots.add(new myCell(true,TypeBooking.Booked,TimeInterval.hour,timeRows*court*4,6));
court +=1;
slots.add(new myCell(false,TypeBooking.Booked,TimeInterval.hour,timeRows*court*4,4));
court +=1;
slots.add(new myCell(true,TypeBooking.Booked,TimeInterval.hour,timeRows*court*4+2,6));
slots.add(new myCell(true,TypeBooking.Maintainence,TimeInterval.hour,8,4));
BEs.add(new BE(0,6));
BEs.add(new BE(0,6));
BEs.add(new BE(2,6));
BEs.add(new BE(0,6));
BEs.add(new BE(0,4));
BEs.add(new BE(2,6));
BEs.add(new BE(0,6));
BEs.add(new BE(0,4));
BEs.add(new BE(0,4));
BEs.add(new BE(2,6));
BEs.add(new BE(0,6));
BEs.add(new BE(0,4));
reserved = new Map<int,List<int>>();
});
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
}
void _toggleInput() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
// inputMode = !inputMode ;
inputMode = true;
});
}
@override
Widget build(BuildContext context) {
Globals.count = 0;
Globals.hour = 8;
// slots.add(new myCell(true,false,TimeInterval.hour,0,6));
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body:
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SingleChildScrollView(
child: Row(
children: List.generate(
BEs.length,
(col) => Column(
children: List.generate(
4*4,
(row) => ProduceCell(inputMode,row,col,slots,BEs)
),
),
),
),
),
)
);
}
Widget ProduceCell(bool inputMode,int row, int col,List<myCell> slots,List<BE> BEs)
{
double borderwidth = 1;
Color mycolor;
String label;
Globals.countperColumn += 1;
int remain = Globals.countperColumn % 4;
var bottomstyle = remain == 0? BorderStyle.solid : BorderStyle.none;
var key = GlobalKey();
keys.add(key);
var con = GestureDetector(
onTap: () {
setState(() {
inputMode = !inputMode;
});
},
child: Container(
//draw border
key: key,
decoration: BoxDecoration(
border: Border(
right: BorderSide(color: Colors.black, width: 1.0),
bottom: BorderSide(
color: Colors.black, width: borderwidth, style: bottomstyle)
)
),
width: 150,
height: 60,
//child is empty container or a textbox
)
// color: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0));
);
return con;
}