简体   繁体   English

Flutter 状态小部件未更新 svg 图标

[英]Flutter statefull widget not updating svg icons

Hi I'm learning flutter but having this problem.嗨,我正在学习 flutter 但遇到了这个问题。 I'm trying change svg icon when user click on menu item but icons not changing.当用户单击菜单项但图标未更改时,我正在尝试更改 svg 图标。 When click on menu item I need change icons.单击菜单项时,我需要更改图标。

class BottomNavigation extends StatefulWidget {
  const BottomNavigation({
    Key key,
  }) : super(key: key);

  @override
  _BottomNavigationState createState() => _BottomNavigationState();
}

class _BottomNavigationState extends State<BottomNavigation> {
  @override
  Widget build(BuildContext context) {
    num _currentIndex = 1;
    return Container(
      padding: EdgeInsets.only(
          left: kDefaultPadding * 2,
          right: kDefaultPadding * 2,
          bottom: kDefaultPadding),
      height: 90,
      decoration: BoxDecoration(color: Colors.white, boxShadow: [
        BoxShadow(
            offset: Offset(0, -10),
            blurRadius: 35,
            color: kPrimaryColor.withOpacity(0.1))
      ]),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          IconButton(
              icon: _currentIndex == 1
                  ? SvgPicture.asset("assets/svg/bhomeActive.svg")
                  : SvgPicture.asset("assets/svg/bhome.svg"),
              onPressed: () {
                setState(() {
                  _currentIndex = 1;
                });
                print(_currentIndex);
              }),
          IconButton(
              icon: _currentIndex == 5
                  ? SvgPicture.asset("assets/svg/bprofileActive.svg")
                  : SvgPicture.asset("assets/svg/bprofile.svg"),
              onPressed: () {
                setState(() {
                  _currentIndex = 5;
                });
                print(_currentIndex);
              })
        ],
      ),
    );
  }
}

Im doing wrong or flutter can't change svg after render?我做错了还是 flutter 在渲染后无法更改 svg?

tried with default icons also not working after state change not rendering again?尝试使用默认图标在 state 更改后不再渲染后也不起作用?

icon: _currentIndex == 1?图标:_currentIndex == 1? Icon(Icons.work): Icon(Icons.alarm),图标(Icons.work):图标(Icons.alarm),

There seems to be an Issue with SVG images in flutter, there are different packages available too, but I would recommend you to use PNG, instead of SVG, as PNG has size lessor than SVG and it also works flawlessly and effectively, both on the screen and programmatically. There seems to be an Issue with SVG images in flutter, there are different packages available too, but I would recommend you to use PNG, instead of SVG, as PNG has size lessor than SVG and it also works flawlessly and effectively, both on the屏幕和编程。 OR Use default Material Icons, which are already available.或使用已经可用的默认材质图标。 Flutter Material Icons Flutter 材质图标

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

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