【问题标题】:Flutter - notifications button with changing on / off iconFlutter - 带有更改开/关图标的通知按钮
【发布时间】:2021-06-17 13:27:54
【问题描述】:

我想将这个带有“通知开/关”功能的提升按钮转换为带有更改图标的按钮,类似于下面的按钮,用于暗/亮模式。只有随着通知的开启和关闭而停止,图标才会改变......

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

所以行 (isDark?Icons.wb_sunny: Icons.brightness_2,) 必须连接到 _notificationsEnabled 函数,以便图标不会根据暗/亮模式而改变,而是通知开/关图标.

                    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);
                          },
                        ),
                      ),
                    ),

【问题讨论】:

    标签: flutter button notifications icons


    【解决方案1】:

    您应该使用 ElevatedButton.icon。这是一个基于您的代码的示例。

            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);
                  });
                },
              ),
    

    【讨论】:

    • 这不是那个意思,我想我可能表达得有点笨拙......但是你的例子,我仍然可以将它用于另一个帖子,它必须是稍作修改,效果很好 :-) 谢谢? 我在上面的文字中添加了一些内容,以便您更好地理解其含义...“所以行 (isDark?Icons.wb_sunny: Icons.brightness_2, ) 必须连接到 _notificationsEnabled 函数,以便图标不会根据暗/亮模式而改变,而是通知开/关图标。”
    • 感谢您的代码,能够解决这个问题 :-) 谢谢,但它引起了另一个问题 ^^ 但我打开了另一个线程。再次感谢您的代码,它对我帮助很大?
    猜你喜欢
    • 2021-08-19
    • 2020-06-09
    • 2021-06-23
    • 1970-01-01
    • 2019-08-14
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多