简体   繁体   English

如何在 flutter appBar 中构建有状态的“动作图标”

[英]How to build Stateful "Action Icons" in flutter appBar

I'm a new learner of flutter, and I meet a problem when I create my appBar for Android apps.我是 flutter 的新手,在为 Android 应用程序创建 appBar 时遇到问题。 There should be a button of the "camera flashlight" icon on the action area of appBar, and users can tap on this icon to choose flashlight options (on/off/auto) from a popupmenu, I finished this step. appBar的操作区域应该有一个“相机手电筒”图标的按钮,用户可以点击这个图标从弹出菜单中选择手电筒选项(开/关/自动),我完成了这一步。

But if I want the flashlight icon on the action area of appBar changes with the change of users' choice (eg if the user chooses auto, then the appBar icon should display an"auto flashlight"), how should I achieve it?但是如果我想让appBar的action area上的手电筒图标随着用户选择的变化而变化(比如如果用户选择auto,那么appBar的图标应该显示一个“auto flashlight”),应该如何实现呢?

Thank you so much in advance.非常感谢你提前。

I had tried using an "if" in the appBar action icon widget, and it failed.我曾尝试在 appBar 操作图标小部件中使用“if”,但失败了。

Now my code in appBar.dart is现在我在 appBar.dart 中的代码是

`Icon flashIcon = Icon(Icons.flash_auto);
FlashMode flashstate;

setIcon([Icon Function() param0]) {
  if (flashstate == FlashMode.off) {
    setIcon(() => flashIcon = Icon(Icons.flash_off));
  } else if (flashstate == FlashMode.torch) {
    flashIcon = Icon(Icons.flash_on);
  } else if (flashstate == FlashMode.always) {
    flashIcon = Icon(Icons.flash_on);
  } else {
    flashIcon = Icon(Icons.flash_auto);
  }
}`

and for the Action button codes is:`而Action按钮的代码是:`

PopupMenuButton<String>(
              icon: setIcon(),

              onSelected: widget.isFlash,
              itemBuilder: (context) {
                List<PopupMenuEntry<String>> menuEntries2 = [
                  const PopupMenuItem<String>(
                    child: Icon(
                      Icons.flash_auto,
                      color: Colors.black,
                    ),
                    value: 'auto',
                  ),
                  const PopupMenuItem<String>(
                    child: Icon(Icons.flash_off, color: Colors.black),
                    value: 'off',
                  ),
                  const PopupMenuItem<String>(
                    child: Icon(Icons.flash_on, color: Colors.black),
                    value: 'torch',
                  ),
                ];
                return menuEntries2;
              },
            ),`    

But the issue is still not resolved...The icon becomes three dots, and if I choose flashlight status in the popup menu, nothing change.但是问题还是没有解决。。图标变成了三个点,如果我在弹出的菜单中选择手电筒状态,没有任何变化。 enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

try code below and you got the exact output which is you describe试试下面的代码,你得到了你描述的确切的 output

Output:- Output:-

在此处输入图像描述

Code:-代码:-

import 'package:flutter/material.dart';

class OptionMenuExample extends StatefulWidget {
  const OptionMenuExample({Key? key}) : super(key: key);

  @override
  State<OptionMenuExample> createState() => _OptionMenuExampleState();
}

class _OptionMenuExampleState extends State<OptionMenuExample> {
  int selectedValue = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Appbar"),
        actions: [
          PopupMenuButton(
            initialValue: selectedValue,
            onSelected: (int value) => setState(() {
              selectedValue = value;
            }),
            child: Icon(
                selectedValue == 1
                    ? Icons.flash_off
                    : selectedValue == 2
                        ? Icons.flash_auto
                        : Icons.flash_on,
                color: Colors.black),
            itemBuilder: (context) => const [
              PopupMenuItem(
                child: Icon(Icons.flash_on, color: Colors.black),
                value: 0,
              ),
              PopupMenuItem(
                child: Icon(Icons.flash_off, color: Colors.black),
                value: 1,
              ),
              PopupMenuItem(
                child: Icon(Icons.flash_auto, color: Colors.black),
                value: 2,
              ),
            ],
          ),
          const SizedBox(width: 16.0)
        ],
      ),
    );
  }
}

Since you haven't posted any code, here is my suggestion.由于您尚未发布任何代码,因此这是我的建议。 You can write a code like this:你可以写这样的代码:

Icon icon = Icon(Icons./*icon name*/);

And then create a function like:然后像这样创建一个 function:

setIcon() {
   //here you use the value of your popup(or whatever you are using to let the user choose the state of flash) to change icon
   //example 
   if(value == **condition**) {
       setState(() => icon = /*new icon*/);
   } else {...} //and so on for all conditions 
   //or use a switch case if you prefer 
}

Now you call the function in the callback of your popup(or whatever) and use variable icon in your appBar button.现在您在弹出窗口(或其他)的回调中调用 function 并在您的 appBar 按钮中使用变量icon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM