0

我在使用持久性底部导航栏时遇到了一些问题,它是颤​​振包。

我所遭受的与以下相同:

▽bottomNav.dart

import 'package:flutter/material.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';

import 'package:project1_makers/main.dart';

class bottomNav extends StatelessWidget {
  PersistentTabController _controller = PersistentTabController(initialIndex: 0);
  
  @override
  Widget build(BuildContext context) {
    return PersistentTabView(
      context,
      controller: _controller,
      screens: _buildScreens(),
      items: _navBarsItems(),
      confineInSafeArea: true,
      backgroundColor: Colors.white,
      
      // Set Components with true/false
      handleAndroidBackButtonPress: true,
      resizeToAvoidBottomInset: true,
      stateManagement: true,
      hideNavigationBarWhenKeyboardShows: true,

      decoration: NavBarDecoration(
        borderRadius: BorderRadius.circular(10.0),
        colorBehindNavBar: Colors.white,
      ),
      popAllScreensOnTapOfSelectedTab: true,
      popActionScreens: PopActionScreensType.all,
      itemAnimationProperties: ItemAnimationProperties(
        duration: Duration(milliseconds: 200),
        curve: Curves.ease,
      ),
      screenTransitionAnimation: ScreenTransitionAnimation(
        animateTabTransition: true,
        curve: Curves.ease,
        duration: Duration(milliseconds: 200),
      ),
      navBarStyle: NavBarStyle.style15,
    );
  }

  List<Widget> _buildScreens(){
    return [
      MyHomePage(),
    ];
  }

  List<PersistentBottomNavBarItem> _navBarsItems(){
    return [
      PersistentBottomNavBarItem(
        icon: Icon(Icons.home),
        title: ("Home"),
        activeColorPrimary: Color(0xFF254690),
        inactiveColorPrimary: Color(0xFFD3DAE9),
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.chat_rounded),
        title: ("Chat"),
        activeColorPrimary: Color(0xFF254690),
        inactiveColorPrimary: Color(0xFFD3DAE9),
      ),
    ];
  }
}

▽ main.dart

import 'package:flutter/material.dart';

import 'package:project1_makers/bottomNav.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primaryColor: Colors.white),
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          bottomNav(),
        ],
      )
    );
  }
}

▽结果

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building bottomNav(dirty):
NavBar styles 15-18 only accept 3 or 5 PersistentBottomNavBarItem items.
'package:persistent_bottom_nav_bar/persistent-tab-view.widget.dart':
Failed assertion: line 125 pos 12: 'assertMidButtonStyles(navBarStyle, items!.length)'

The relevant error-causing widget was:
  bottomNav [My_direction]

When the exception was thrown, this was the stack:
#2      new PersistentTabView (package:persistent_bottom_nav_bar/persistent-tab-view.widget.dart:125:12)
#3      bottomNav.build (package:project1_makers/bottomNav.dart:11:12)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
#9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#10     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#11     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (19 frames)
#30     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#31     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#32     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (238 frames)
#270    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#271    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#272    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (345 frames)
#617    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#618    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
#619    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1189:16)
#620    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1159:5)
#621    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1104:18)
#622    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2535:19)
#623    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1103:13)
#624    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:937:7)
#625    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:917:7)
(elided 13 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

我想使用 NavBarStyle 15,但很难应用。

你能帮我解决这个错误吗?

如果您指出我的描述中缺少某些内容,我们将不胜感激。

提前致谢:)

4

1 回答 1

0

这很容易解决,仔细查看错误消息本身,它显示以下内容:NavBar 样式 15-18 仅接受 3 或 5 个 PersistentBottomNavBarItem 项。 但是您的_navBarsItems列表中只有 2 个 PersistentBottomNavBarItems。

于 2021-09-11T14:40:35.847 回答