gpt4 book ai didi

flutter - 单击后如何禁用onTap

转载 作者:行者123 更新时间:2023-12-03 04:20:34 25 4
gpt4 key购买 nike

我试图在单击时禁用onTap功能。
我声明了var _onTap;
至于将 bool(boolean) 值合计为true,它仍然没有改变...请看一下。
并且在手势检测器部分中,每个文本都可以按行顺序单击

                      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;
bool isActive = true;

@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}');

// bool isActive = true;

return Scaffold(
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: ListView.builder(
itemCount: ticketList.tickets.length,
itemBuilder: (BuildContext context, index) {
List tripleNumbersList = [];
List<Widget> cells = [];
List<Widget> rows = [];
//Get the lenght of the list 'tickets' of the Ticket class
int ticketsCount = ticketList.tickets.length;

//Iterates over the lists inside the 'tickets' list of the Ticket class
for (int i = 0; i < ticketsCount; i++) {
//Get the lists of lists inside the 'tickets' list
tripleNumbersList = ticketList.tickets[i];
//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(Center(
child: Container(
height: 45,
width: 35,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
color: Colors.pink
),
child: GestureDetector(
onTap: () {
if (isActive) {
print('Working');
setState(() {
isActive = false;
});
}
},
child: Text(
' ${list[k]} ',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
),
),
));
}
//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: 30,
)
]));
}

return Center(
child: Container(
height: h,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.red // covers the big box
),
child: Column(
children: rows,
)
),
);
},
),
),
),
),
);
}
}
}
任何人都可以纠正所犯的错误。
这是输出 I want pretty much the same output but it is displaying 9 tickets instead of 3 tickets

最佳答案

onTap是被检测到的操作,您不能禁用该操作。
您可以做的是用 bool(boolean) 值包装 Action ,该 Action 在第一次点击后会更新。
例如:

bool singleTap = true;
GestureDetector(
onTap: () {
if (singleTap) {
// Do something here
setState(() {
singleTap = false; // update bool
});
}
}
child: Text("tap Here")
)

关于flutter - 单击后如何禁用onTap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62861636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com