flutter底部导航栏添加角标

import 'package:flutter/material.dart';

class TabsComponent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new TabsComponentState();
}

class TabsComponentState extends State<TabsComponent> {
  int _selectedIndex = 0;

  final List<Widget> _children = [
    new PlaceholderWidget('Home'),
    new PlaceholderWidget('Love'),
    new PlaceholderWidget('Cart'),
    new PlaceholderWidget('Order'),
    new PlaceholderWidget('My'),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: _children[_selectedIndex],
      bottomNavigationBar: BottomAppBar(
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            returnBottomItem(_selectedIndex, 0, Icons.home, "首页", 0),
            returnBottomItem(_selectedIndex, 1, Icons.favorite_border, "收藏", 0),
            returnBottomItem(
                _selectedIndex, 2, Icons.local_grocery_store, "购物车", 1),
            returnBottomItem(_selectedIndex, 3, Icons.library_books, "订单", 0),
            returnBottomItem(_selectedIndex, 4, Icons.person, "我的", 0),
          ],
        ),
      ),
    );
  }

  returnBottomItem(
      int selectIndex, int index, IconData iconData, String title, int badge) {
    //未选中状态的样式
    TextStyle textStyle = TextStyle(fontSize: 12.0, color: Colors.grey);
    MaterialColor iconColor = Colors.grey;
    double iconSize = 20;
    EdgeInsetsGeometry padding = EdgeInsets.only(top: 8.0);

    if (selectIndex == index) {
      //选中状态的文字样式
      textStyle =
          TextStyle(fontSize: 12.0, color: Theme.of(context).primaryColor);
      //选中状态的按钮样式
      iconColor = Theme.of(context).primaryColor;
    }
    Widget padItem = SizedBox();
    if (iconData != null) {
      padItem = Padding(
          padding: padding,
          child: Stack(
            children: <Widget>[
              Container(
                color: Colors.white,
                child: Center(
                  child: Column(
                    children: <Widget>[
                      Icon(
                        iconData,
                        color: iconColor,
                        size: iconSize,
                      ),
                      Text(
                        title,
                        style: textStyle,
                      )
                    ],
                  ),
                ),
              ),
              Positioned(
                child: badge > 0
                    ? Container(
                        width: 12,
                        height: 12,
                        child: Center(
                          child: Text(badge.toString(),
                              style: TextStyle(
                                  fontSize: 8.0, color: Colors.white)),
                        ),
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: new BorderRadius.all(
                            const Radius.circular(16.0),
                          ),
                        ),
                      )
                    : Text(""),
                right: 22.0,
                top: 0.0,
              ),
            ],
          ));
    }
    Widget item = Expanded(
      flex: 1,
      child: new GestureDetector(
        onTap: () {
          if (index != _selectedIndex) {
            setState(() {
              _selectedIndex = index;
            });
          }
        },
        child: SizedBox(
          height: 52,
          child: padItem,
        ),
      ),
    );
    return item;
  }
}

class PlaceholderWidget extends StatelessWidget {
  final String text;

  PlaceholderWidget(this.text);

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new Text(text),
    );
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值