我有幾個具有不同識別符號的按鈕,它們以 int 的形式制作并傳遞給按鈕
int keyButton1 = 1;
int keyButton2 = 2;
int keyButton3 = 3;
int keyButton4 = 1;
ElevatedButton(
onPressed: () => {keyButton1, _unionKeyFunction()}),
child: Text("Button 1")),
],
),
ElevatedButton(
onPressed: () => {keyButton2, _unionKeyFunction()}),
child: Text("Button 2")),
],
),
ElevatedButton(
onPressed: () => {keyButton3, _unionKeyFunction()}),
child: Text("Button 3")),
],
),
ElevatedButton(
onPressed: () => {keyButton4, _unionKeyFunction()}),
child: Text("Button 4")),
],
),
當我點擊任意 2 個按鈕時,我想收到他們的 ID 是否匹配的資訊。如何將這些識別符號傳遞給函式內的變數以進行進一步比較?
_unionKeyFunction(){
var firstClikButton = //Any variable (keyButton1 , keyButton2, keyButton3, keyButton4) ;
var secondClikButton = //Any variable (keyButton1 , keyButton2, keyButton3, keyButton4) ;
if( firstClikButton == secondClikButton) {
print("Equal");
}
else {
print("Not equal");
}
}
uj5u.com熱心網友回復:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final int keyButton1 = 1;
final int keyButton2 = 2;
final int keyButton3 = 3;
final int keyButton4 = 1;
int?
firstClikButton; //Any variable (keyButton1 , keyButton2, keyButton3, keyButton4) ;
int?
secondClikButton; //Any variable (keyButton1 , keyButton2, keyButton3, keyButton4) ;
_unionKeyFunction(int keyButton) {
// check is firstClickButton is null or not
//if its null it will assign keybutton value to it
if (firstClikButton == null) {
firstClikButton ??= keyButton;
// since its the first key press we can safely
// return without check the secondClickButton
return;
} else {
// we need to check is secondClickButton is null or not
// if its null we are not changing the firstClickButton value
// if its not null we are setting the firstClickButton with secondClikButton
// and secondClikButton with the new keyButton
firstClikButton = secondClikButton ?? firstClikButton;
secondClikButton = keyButton;
}
if (firstClikButton == secondClikButton) {
print("Equal");
} else {
print("Not equal");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
ElevatedButton(
onPressed: () => _unionKeyFunction(keyButton1),
child: Text("Button 1")),
ElevatedButton(
onPressed: () => _unionKeyFunction(keyButton2),
child: Text("Button 2")),
ElevatedButton(
onPressed: () => _unionKeyFunction(keyButton3),
child: Text("Button 3")),
ElevatedButton(
onPressed: () => _unionKeyFunction(keyButton4),
child: Text("Button 4")),
],
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/511238.html
標籤:扑功能镖按钮
