简体   繁体   English

Flutter - 带有更改开/关图标的通知按钮

[英]Flutter - notifications button with changing on / off icon

I would like to convert this elevated button with the "notifications on / off" function to a button with a changing icon, similar to the button below which is for dark / light mode.我想将这个带有“通知开/关”的提升按钮 function 转换为带有更改图标的按钮,类似于下面的按钮,用于暗/亮模式。 only that stops with notifications on and off, the icon changes...只有随着通知的开启和关闭而停止,图标才会改变......

                          child: ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                primary: Color(0xFF34445C)),
                            onPressed: () {
                              setState(() {
                                _notificationsEnabled =
                                    !_notificationsEnabled;
                                _updateNotifications(_notificationsEnabled);
                              });
                            },
                            child: Text(
                                _notificationsEnabled
                                    ? 'PUSH-BENACHRICHTIGUNGEN AKTIVIERT'
                                    : 'PUSH-BENACHRICHTIGUNGEN DEAKTIVIERT',
                                style: TextStyle(
                                    fontSize: 11.0)),
                          ),

So the line (isDark? Icons.wb_sunny: Icons.brightness_2,) would have to be connected to the _notificationsEnabled function so that the icon does not change depending on the dark / light mode, but rather a notifications on / off icon.所以线 (isDark?Icons.wb_sunny: Icons.brightness_2,) 必须连接到 _notificationsEnabled function 以便图标不会根据暗/亮模式而改变,而是通知开/关图标。

                    child: ClipOval(
                      child: Material(
                        color: Color(0xFF282C39),
                        child: InkWell(
                          splashColor: Colors.blue,
                          child: SizedBox(
                              width: 32,
                              height: 32,
                              child: Icon(
                                  isDark ? Icons.wb_sunny : Icons.brightness_2,
                                  size: 20,
                                  color: Colors.white)),
                          onTap: () {
                            isDark
                                ? Magazin.of(context)!
                                .setBrightness(Brightness.light)
                                : Magazin.of(context)!
                                .setBrightness(Brightness.dark);
                          },
                        ),
                      ),
                    ),

You should use ElevatedButton.icon .您应该使用ElevatedButton.icon Here an example base on your code.这是一个基于您的代码的示例。

        ElevatedButton.icon(
            icon: _notificationsEnabled
                ? Icon(Icons.notifications_active)
                : Icon(Icons.notifications_off),
            label: Text(
              _notificationsEnabled
                  ? 'PUSH-BENACHRICHTIGUNGEN AKTIVIERT'
                  : 'PUSH-BENACHRICHTIGUNGEN DEAKTIVIERT',
              style: TextStyle(fontSize: 11.0),
            ),
            onPressed: () {
              setState(() {
                _notificationsEnabled = !_notificationsEnabled;
                _updateNotifications(_notificationsEnabled);
              });
            },
          ),

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

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