Как добавить число значков как часть BottomNavigationBarItem в flutter
Я пишу приложение Flutter, которое обновляет 9X_flutter-sdk количество уведомлений в BottomNavigationBar
.
Я использовал 9X_ios-sdk нижнюю навигационную библиотеку (AHBottomNavigation)
для достижения 9X_flutter той же цели в родном Android (java), но, похоже, я 9X_iphone-os не могу найти способ обойти это с помощью 9X_dart флаттера.
items: [ BottomNavigationBarItem( title: Text('Home'), icon: Icon(Icons.home)), BottomNavigationBarItem( title: Text('Friends'), icon:Icon(Icons.notifications)), BottomNavigationBarItem( title: Text('Settings'), icon: Icon(Icons.settings)), ],
Я хочу получить то, что указано 9X_dart на этикетке 2 с 4, прикрепленным к BottomNavigationBarItem
.
Ответ #1
Ответ на вопрос: Как добавить число значков как часть BottomNavigationBarItem в flutter
Вы также можете использовать пакет badges
, изображение 9X_iphone-os с его страницы:
А затем укажите его как icon
в 9X_android-application свой 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, ), )),
Ответ #2
Ответ на вопрос: Как добавить число значков как часть BottomNavigationBarItem в flutter
Попробуйте это:
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 { StreamController _countController = StreamController(); int _currentIndex = 0; int _tabBarCount = 0; List _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:
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: [ 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, ), ), ); }
9X_apple-ios
-
3
-
4
-
15
-
6
-
1
-
6
-
5
-
10
-
2
-
6
-
5
-
1
-
6
-
17
-
2
-
9
-
7
-
3
-
5
-
2
-
8
-
1
-
6
-
9
-
5
-
1
-
4
-
5
-
11
-
2
-
5
-
10
-
5
-
2
-
2
-
2
-
2
-
4
-
3
-
8
-
4
-
5
-
2
-
4
-
7
-
1
-
2
-
2
-
2
-
6