Flutter 中的持久导航栏错误

问题描述 投票:0回答:2

我是 flutter 新手,想要添加一个持久导航栏,其中包含 4 个图标,通向 3 个不同页面,其中 icon-1 作为默认主屏幕。代码附在下面。

我使用的持久底部导航栏的版本是4.0.2

主屏幕

import 'package:flutter/material.dart';
import 'package:myspace/widgets/right_drawer.dart';
import 'package:myspace/widgets/bottom_navigation_bar.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';


class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        title: Text(
          'mySpace',
          style: TextStyle(
            fontSize: 24.0,
            fontWeight: FontWeight.bold,
            color: Colors.blue.shade900
          ),
        ),
        actions: [
          Builder(
            builder: (context) => IconButton(
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                icon: Icon(Icons.account_circle, color: Colors.blue.shade900, size: 30.0,)
            ),
          ),
        ],
      ),
      endDrawer: const RightDrawerWidget(),
      bottomNavigationBar: BottomListWidget(),
    );
  }
}

以及底部导航栏的小部件文件:

import 'package:flutter/material.dart';
import 'package:myspace/Screens/homescreen.dart';
import 'package:myspace/Screens/chat_system.dart';
import 'package:myspace/Screens/help_and_support.dart';
import 'package:myspace/Screens/news.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';

class BottomListWidget extends StatefulWidget {
  const BottomListWidget({Key? key}) : super(key: key);

  @override
  State<BottomListWidget> createState() => _BottomListWidgetState();
}

class _BottomListWidgetState extends State<BottomListWidget> {

  PersistentTabController _controller = PersistentTabController(initialIndex: 0);

  @override
  Widget build(BuildContext context) {
    return PersistentTabView(
        context,
        controller: _controller,
        screens: _buildScreens(),
      items: _navBarItems(),
      confineInSafeArea: true,
      itemAnimationProperties: const ItemAnimationProperties(
        duration: Duration(milliseconds: 200),
        curve: Curves.ease,
      ),
      screenTransitionAnimation: const ScreenTransitionAnimation(
        duration: Duration(milliseconds: 200),
        curve: Curves.ease,
        animateTabTransition: true
      ),
      navBarStyle: NavBarStyle.style10,
    );
  }
}

List<PersistentBottomNavBarItem> _navBarItems() {
  return [
    PersistentBottomNavBarItem(
      icon: const Icon(Icons.home),
      title: ('Home'),
      activeColorPrimary: Colors.blue.shade800,
      inactiveColorPrimary: Colors.grey,
    ),
    PersistentBottomNavBarItem(
      icon: const Icon(Icons.newspaper_sharp),
      title: ('Happenings'),
      activeColorPrimary: Colors.blue.shade800,
      inactiveColorPrimary: Colors.grey,
    ),
    PersistentBottomNavBarItem(
      icon: const Icon(Icons.support_agent_rounded),
      title: ('Help'),
      activeColorPrimary: Colors.blue.shade800,
      inactiveColorPrimary: Colors.grey,
    ),
    PersistentBottomNavBarItem(
      icon: const Icon(Icons.message),
      title: ('Chats'),
      activeColorPrimary: Colors.blue.shade800,
      inactiveColorPrimary: Colors.grey,
    ),
  ];
 }

 List<Widget> _buildScreens() {
  return [
    const HomeScreen(),
    const RecentNewsScreen(),
    const HelpAndSupport(),
    const ChatSystem()
  ];
 }

构建此应用程序后,我收到错误:

Launching lib\main.dart on EB2101 in debug mode...
Running Gradle task 'assembleDebug'...
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/persistent-tab-view.widget.dart:368:22: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
      WidgetsBinding.instance!.addPostFrameCallback((_) {
                     ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/animations/animations.dart:55:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-6-bottom-nav-bar.widget.dart:44:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-8-bottom-nav-bar.widget.dart:44:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-11-bottom-nav-bar.widget.dart:46:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-12-bottom-nav-bar.widget.dart:46:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-13-bottom-nav-bar.widget.dart:46:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/persistent_bottom_nav_bar-4.0.2/lib/nav-bar-styles/style-14-bottom-nav-bar.widget.dart:46:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^
android flutter android-studio widget flutter-bottomnavigation
2个回答
0
投票

该软件包已使用 17 个月,不支持 flutter 版本 3.0+ .. 我假设您正在运行 flutter 版本 >3,该软件包不支持该版本。. 尝试其他软件包或降级 flutter 版本。您还可以分叉存储库并更新小部件绑定代码并使用它们。


0
投票

请检查更改日志以解决问题。 https://pub.dev/packages/persistent_bottom_nav_bar/changelog

对我来说,需要进行一些重构来解决这个问题。请按照文档了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.