Home > database >  Comparison of variables in a function
Comparison of variables in a function

Time:10-08

I have several buttons with different identifiers that are made in the form of int and passed to the button

   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")),
                            ],
                          ),

When I click on any 2 buttons, I would like to receive information whether their IDs match or not. How do I pass these identifiers to variables inside my function for further comparison ?

_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");
  }
}

CodePudding user response:

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")),
        ],
      ),
    );
  }
}
  • Related