flutter 更改按钮颜色抖动

qhhrdooz  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(169)

我们有9个按钮(1个是最低的主按钮和8个其他按钮),和8个灯(按钮,他们的颜色变化randonly,而按下主按钮)。(如下图所示)。

我们如何根据随机选择改变灯光的颜色,并了解哪一盏灯打开了,保存这些信息并检查玩家按下的按钮是否正确
我们试图使用按钮小部件的关键因素,但它没有工作

import 'dart:math';
import 'package:flutter/material.dart';
import 'Screen building third test.dart';
import 'globals.dart' as globals;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
    );
  }
}

class LBulb extends StatefulWidget {
  const LBulb({super.key});

  @override
  State<LBulb> createState() => _LBulb();
}

final stopwatchD = Stopwatch();
final stopwatchR = Stopwatch();
bool isChose = false;
int ver = 0;

class _LBulb extends State<LBulb> {
  @override
  void initState() {
    ver = oneOrThree();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue[600]?.withOpacity(0.5),
      ),
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage("assets/images/background2.png"),
              fit: BoxFit.cover),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 60),
            const Text(
              "Please click on the appropriate light's button",
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 40,
                  fontWeight: FontWeight.bold,
                  fontFamily: 'Alkatra'),
            ),
            const SizedBox(height: 30),
            Center(
              child: Container(
                height: 500,
                width: 810,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(100),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.5),
                      offset: const Offset(
                        7.0,
                        7.0,
                      ),
                      blurRadius: 10.0,
                    ),
                  ],
                ),
                child: screenBuilding(
                  distance: 310.0,
                  // Center button
                  centerButton: TextButton(
                    onPressed: () {},
                    child: Container(
                      height: 20,
                      width: 20,
                      decoration: BoxDecoration(
                          color: Colors.black,
                          borderRadius: BorderRadius.circular(500)),
                      child: GestureDetector(
                        // while long tap
                        onTapDown: (details) {
                          // start the detection timer
                          stopwatchD.start();
                          // when the player press the button we want to chose
                          // the game version (1 or 3 lights) and light random light/lights
                          if (ver == 1) {
                          } else {}
                        },
                        // when the player release the button
                        onTapUp: (details) {
                          // save the time of the detection time
                          stopwatchD.stop();
                          globals.detectionTime = stopwatchD.elapsed;
                          globals.detectionTimes[globals.numOfTurn] =
                              globals.detectionTime;
                          // reset the values
                          stopwatchD.reset();
                          globals.detectionTime = const Duration(seconds: 0);
                          globals.numOfTurn++;
                          print(globals.detectionTimes);
                          // start the response timer
                          // this timer will be closed when the player will press
                          // the button of the correct light
                          stopwatchR.start();
                        },
                      ),
                    ),
                  ),
                  // the lights buttons
                  buttonsFirstRow: [
                    TextButton(
                      onPressed: () {
                        // check if it's the correct light
                        // when it's the correct light stop the response time.
                        print("clicked1");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked2");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked3");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked4");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked5");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked6");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked7");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked8");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(500)),
                      ),
                    ),
                  ],
                  // the first 4 light
                  buttonsSecondRowHalf1: [
                    TextButton(
                      onPressed: () {
                        print("clicked1");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked2");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked3");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked4");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                  ],
                  // the other 4 light
                  buttonsSecondRowHalf2: [
                    TextButton(
                      onPressed: () {
                        print("clicked5");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked6");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked7");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        print("clicked8");
                      },
                      child: Container(
                        height: 20,
                        width: 20,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(500),
                            border: Border.all(width: 3)),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Color turnLight(bool isChose) {
    if (isChose == true) {
      return Colors.green;
    }
    return Colors.white;
  }

  int oneOrThree() {
    final rand = Random();
    List<int> a = [1, 3];
    return a[rand.nextInt(a.length)];
  }

  List<int> randomLights(int ver) {
    final rand = Random();
    List<int> a = [1, 2, 3, 4, 5, 6, 7, 8];
    List<int> ans = [];
    if (ver == 1) {
      ans[0] = a[rand.nextInt(a.length)];
    } else {
      ans[0] = a[rand.nextInt(a.length)];
      a.remove(ans[0]);
      ans[1] = a[rand.nextInt(a.length)];
      a.remove(ans[1]);
      ans[2] = a[rand.nextInt(a.length)];
    }
    return ans;
  }
}
xmakbtuz

xmakbtuz1#

要根据随机选择更改灯光颜色并检查玩家是否按下了正确的按钮,请按照以下步骤操作:
1:定义灯光的颜色列表和存储正确灯光索引的变量:

List<Color> lightColors = [
  Colors.red,
  Colors.blue,
  Colors.green,
  Colors.purple,
  Colors.white,
  Colors.black,
  Colors.gray,
  Colors.deepPink, // Add More colors as you want 

];

// Here store the correct Light Index

int correctLightIndex = Random().nextInt(lightColors.length);

第2步:使用ElevatedButton小部件为灯光创建按钮列表。为lightColors列表中的每个按钮指定相应的颜色:

List<ElevatedButton> lightButtons = List.generate(lightColors.length, (index) {
  return ElevatedButton(
    style: ButtonStyle(
      backgroundColor: MaterialStateProperty.all(lightColors[index]),
    ),
    onPressed: () {
      // Check if the pressed button is the correct one
      if (index == correctLightIndex) {
        // Correct button pressed
        // Handle the logic for a correct guess
      } else {
        // Incorrect button pressed
        // Handle the logic for an incorrect guess
      }
    },
  );
})

在上面的代码中,我们有两个条件,如果用户按下具有相同光指数的键,则向用户显示消息。
在主屏幕上打乱颜色数组。按钮按下使用此逻辑

List<Color> shuffledLightColors = [];
onPressed: () {
  setState(() {
    shuffledLightColors = List.from(lightColors)..shuffle();
  });
},

希望这对你有帮助。

相关问题