问题描述
因此,每当每个小部件的onTap按钮用尽时,我都试图弹出一个对话框。
我在这里附上如何实现相同代码的代码段。
代码中包含一堆用于处理数组的函数,这就是为什么要弄乱以获得结果的原因。
So I am trying to pop out a dialog box whenever the onTap button for each widget is getting exhausted up.I am here with attaching my code snippet of how to implement the same.The code contains a bunch to deal with the array and which is why I am messed up to attain the result.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Ticket {
Ticket({
this.tickets,
});
List<List<List<int>>> tickets;
factory Ticket.fromJson(Map<String, dynamic> json) => Ticket(
tickets: List<List<List<int>>>.from(json["tickets"].map((x) =>
List<List<int>>.from(
x.map((x) => List<int>.from(x.map((x) => x)))))),
);
Map<String, dynamic> toJson() => {
"tickets": List<dynamic>.from(tickets.map((x) => List<dynamic>.from(
x.map((x) => List<dynamic>.from(x.map((x) => x)))))),
};
}
class TicketPage extends StatefulWidget {
@override
_TicketPageState createState() => _TicketPageState();
}
class _TicketPageState extends State<TicketPage> {
var nos;
Ticket ticketList;
String apiResult;
Map<String, bool> cellStatus = {};
@override
void initState() {
super.initState();
_getNumbers();
}
_getNumbers() async {
var result = await http
.post(
'https://tickets-qzd55332wa-de.a.run.app/generateTickets?ticketsRequired=2')
.then((result) {
//Waits for the API response and assigns to apiResult variable
setState(() {
apiResult = result.body;
});
});
}
// List tick = [
// {
// 'tickets': [
// [
// [11, 5, 7, 10, 28, 9, 7, 74, 59],
// [1, 15, 7, 10, 8, 79, 27, 74, 9],
// [71, 5, 7, 20, 18, 9, 77, 74, 79],
// ],
// [
// [21, 5, 7, 80, 8, 9, 7, 74, 49],
// [31, 15, 7, 10, 18, 79, 7, 74, 19],
// [71, 5, 7, 20, 18, 79, 77, 74, 29],
// ],
// ]
// },
// ];
@override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var w = MediaQuery.of(context).size.width;
if (apiResult == null) {
return Center(child: CircularProgressIndicator());
} else {
//Get an instance of Ticket from the API assigned to apiResponse variable
ticketList = Ticket.fromJson(json.decode(apiResult));
print('Tickets: ${ticketList.tickets}');
return Scaffold(
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
)),
child: ListView.builder(
itemCount: ticketList.tickets.length,
itemBuilder: (BuildContext context, index) {
List tripleNumbersList = [];
List<Widget> cells = [];
List<Widget> rows = [];
//Get the lists of lists inside the 'tickets' list
tripleNumbersList = ticketList.tickets[index];
//Iterates over each list with other 3 lists
for (int j = 0; j < tripleNumbersList.length; j++) {
//Get one of the 3 lists
List<int> list = tripleNumbersList[j];
//Iterates over the list of numbers
for (int k = 0; k < list.length; k++) {
//Adds a Widget to 'cells; list for each number
cells.add(Container(
height: 35,
width: 35,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: GestureDetector(
onTap: () {
print('Working');
if (cellStatus['$j$k'] ?? true) {
print('Working');
setState(() {
cellStatus.addAll({'$j$k': false});
});
}
},
child: list[k] != 0
? Text(
' ${list[k]} ',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold),
)
: Text(''),
)));
}
//Adds the list of 'cells' in the 'rows' list
rows.add(Row(children: cells));
cells = [];
}
//Adds a empty row to make space
rows.add(Row(children: [
Container(
height: 10,
)
]));
return Center(
child: Container(
height: h / 3,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: Column(
//Adds the list of rows to the column
children: rows,
),
),
);
},
),
),
),
),
),
);
}
}
}
```
上面的代码确实具有onTap功能,一旦每个文本小部件都被点击,onTap功能将被禁用。
我试图达到的目标是,一旦禁用每个onTap,就会弹出警报或对话框。
The above code does have an onTap feature where once every text widget is tapped out the onTap feature is being disabled.I am trying to attain that once every onTap is disabled an alert or a dialog box should pop out.
我收到的显示是这个
我被这个问题困扰,不知道如何解决...
请帮助我说明我的错误以及解决方法实现相同的
I am stuck at this problem and have no clue how to solve it...Please help me out stating my mistakes and the ways to how to implement the same
推荐答案
根据我所看到的,首先您可以基于按钮创建字符串和布尔值的映射,
Based on what I see, first you can create a map of string and boolean based on your buttons, something like this:
var _details = {'6':false,'15':false ... '86':false};
,然后在每个按钮上单击,首先将其值更改为true,然后检查所有值是否为true(这意味着所有按钮点击):
and then on each button click first change its value to true and then check if all values are true (which means all buttons are clicked):
_details['86']=true;
var result = _details.values.toList().indexWhere((element) => !element);
if(result == -1) // all values are true, which means all buttons are tapped
这篇关于当所有onTap功能都用尽时,无法显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!