简体   繁体   English

如何在flutter中将徽章计数数字添加为BottomNavigationBarItem的一部分

[英]How to add a badge count number as part of a BottomNavigationBarItem in flutter

I am writing a flutter app which updates the number of notifications in the BottomNavigationBar . 我正在编写一个BottomNavigationBar应用程序,该应用程序更新BottomNavigationBar的通知数量。

I used a bottom navigation library (AHBottomNavigation) to accomplish the same goal in native android(java) but I cant seem to find my way around it using flutter. 我使用底部导航库(AHBottomNavigation)在本机android(java)中实现了相同的目标,但似乎无法通过flutter找到解决方法。

 items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                      title: Text('Home'), icon: Icon(Icons.home)),
                  BottomNavigationBarItem(
                      title: Text('Friends'), icon:Icon(Icons.notifications)),
                  BottomNavigationBarItem(
                      title: Text('Settings'), icon: Icon(Icons.settings)),
                ],

I want to get what is in label 2 with the 4 attached to the BottomNavigationBarItem . 我想获取标签2中附有BottomNavigationBarItem标签4的BottomNavigationBarItem

在此处输入图片说明

Try this : 尝试这个 :

        import 'dart:async';
        import 'package:flutter/material.dart';
        import 'badge_icon.dart';
        void main() => runApp(MyApp());
        class MyApp extends StatelessWidget {
        // 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 StatefulWidget {
        @override
        _MyHomePageState createState() => _MyHomePageState();
        }

        class _MyHomePageState extends State<MyHomePage> {
        StreamController<int> _countController = StreamController<int>();

        int _currentIndex = 0;
        int _tabBarCount = 0;

        List<Widget> _pages;

        Widget _tabBar() {
            return BottomNavigationBar(
            items: [
                const BottomNavigationBarItem(
                icon: Icon(Icons.home, size: 25),
                title: const Text("Increment"),
                ),
                BottomNavigationBarItem(
                icon: StreamBuilder(
                    initialData: _tabBarCount,
                    stream: _countController.stream,
                    builder: (_, snapshot) => BadgeIcon(
                    icon: Icon(Icons.chat, size: 25),
                    badgeCount: snapshot.data,
                    ),
                ),
                title: const Text("Decrement"),
                ),
            ],
            currentIndex: _currentIndex,
            onTap: (index) => setState(() => _currentIndex = index),
            );
        }

        @override
        void initState() {
            _pages = [
            Container(
                child: Center(
                child: FlatButton(
                    child: Text('Increment'),
                    onPressed: () {
                    _tabBarCount = _tabBarCount + 1;
                    _countController.sink.add(_tabBarCount);
                    },
                ),
                ),
            ),
            Container(
                child: Center(
                child: FlatButton(
                    child: Text('Decrement'),
                    onPressed: () {
                    _tabBarCount = _tabBarCount - 1;
                    _countController.sink.add(_tabBarCount);
                    },
                ),
                ),
            ),
            ];
            super.initState();
        }

        @override
        Widget build(BuildContext context) {
            return Scaffold(
            appBar: AppBar(
                title: Text('Tab Bar Icon Badge'),
            ),
            body: _pages[_currentIndex],
            bottomNavigationBar: _tabBar(),
            );
        }

        @override
        void dispose() {
            _countController.close();
            super.dispose();
        }
        }

BadgeIcon widget : BadgeIcon小部件:

        import 'package:flutter/material.dart';

        class BadgeIcon extends StatelessWidget {
        BadgeIcon(
            {this.icon,
            this.badgeCount = 0,
            this.showIfZero = false,
            this.badgeColor = Colors.red,
            TextStyle badgeTextStyle})
            : this.badgeTextStyle = badgeTextStyle ??
                    TextStyle(
                    color: Colors.white,
                    fontSize: 8,
                    );
        final Widget icon;
        final int badgeCount;
        final bool showIfZero;
        final Color badgeColor;
        final TextStyle badgeTextStyle;

        @override
        Widget build(BuildContext context) {
            return new Stack(children: <Widget>[
            icon,
            if (badgeCount > 0 || showIfZero) badge(badgeCount),
            ]);
        }

        Widget badge(int count) => Positioned(
                right: 0,
                top: 0,
                child: new Container(
                padding: EdgeInsets.all(1),
                decoration: new BoxDecoration(
                    color: badgeColor,
                    borderRadius: BorderRadius.circular(8.5),
                ),
                constraints: BoxConstraints(
                    minWidth: 15,
                    minHeight: 15,
                ),
                child: Text(
                    count.toString(),
                    style: new TextStyle(
                    color: Colors.white,
                    fontSize: 10,
                    ),
                    textAlign: TextAlign.center,
                ),
                ),
            );
        }

在此处输入图片说明

You can also use badges package, a picture from its page : 您还可以使用badges包,其页面上的图片:

包示例图片

And then provide it as icon to your BottomNavigationBarItem : 然后将其作为icon提供给BottomNavigationBarItem

BottomNavigationBarItem(
  icon: BadgeIconButton(
                        itemCount: 5, // required
                        icon: Icon(
                          Icons.monetization_on,
                          color:
                              _selectedIndex == 2 ? Colors.blue : Colors.grey,
                        ), // required
                        badgeColor: Colors.red, // default: Colors.red
                        badgeTextColor: Colors.white, // default: Colors.white
                        hideZeroCount: true, // default: true
                        onPressed: null),
                    title: Text(
                      'Item',
                      style: TextStyle(
                        color: _selectedIndex == 2 ? Colors.blue : Colors.grey,
                      ),
                    )),

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

相关问题 如何在 Flutter 中为 BottomNavigationBarItem 添加徽章? - How to add badge to BottomNavigationBarItem in Flutter? 如何在 Android 中添加通知徽章计数? - How to add notification Badge count in Android? Android - 如何将徽章计数添加到应用程序图标? - Android - how to add a badge count to an application icon? 当应用程序在 flutter 中处于前台时,如何设置 Android 应用程序徽章计数? - How to set Android app badge count when app is foreground in flutter? Flutter-将抽屉与其他项目一起添加到bottomNavigationBarItem并导航 - Flutter- add drawer into bottomNavigationBarItem with other items and navigate startForeground() 时如何隐藏徽章计数 - How can i hide badge count number when startForeground() 如何在启动图标上添加未读徽章计数? - How can I add a unread badge count on launch icon? 如何使 Flutter BottomNavigationBarItem 图标和 label 在同一行 - How to make Flutter BottomNavigationBarItem icon and label in same line 如何在Sony Xperia设备上的应用程序图标上添加通知标志/计数? - How to add a notification badge/count to application icon on Sony Xperia devices? 当用户点击 BottomNavigationBarItem 时如何显示 flutter modalBottomSheet - How to display a flutter modalBottomSheet when user taps a BottomNavigationBarItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM