【问题标题】:BottomNavigationBarItem - Unable to link widgetsBottomNavigationBarItem - 无法链接小部件
【发布时间】:2023-01-27 16:27:45
【问题描述】:

我需要将交易屏幕和类别屏幕小部件作为BottomNavigationBarItems

但是,我收到此错误

方法“Transactions”没有为类型“HomeState”定义。尝试更正现有方法的名称,或定义一个名为“Transactions”的方法。

lib\screens\home.dart

import 'package:demo_app/screens/categories.dart';
import 'package:demo_app/screens/transactions.dart';
import 'package:flutter/material.dart';

class Home extends StatefulWidget{
  const Home({super.key});

  @override
  State<Home> createState() => HomeState();
}

class HomeState extends State<Home> {
  List<Widget> widgetOptions = [Transactions(), Categories()];
  int selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Logged in!'),
        ),
        body: widgetOptions.elementAt(selectedIndex),
        bottomNavigationBar: BottomAppBar(
          shape: const CircularNotchedRectangle(),
          notchMargin: 4,
          child: BottomNavigationBar(
            backgroundColor: Theme.of(context).primaryColor.withAlpha(0),
            elevation: 0,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.account_balance_wallet),
                label: 'Transactions'),
              BottomNavigationBarItem(
                icon: Icon(Icons.category),
                label: 'Categories'),
              BottomNavigationBarItem(
                icon: Icon(Icons.logout),
                label: 'Log out'),              
            ],
            currentIndex: selectedIndex,
            onTap: onItemTapped,
          )
        )
      ),
    );
  }

  void onItemTapped(int index){
    if(index == 2){

    }else{
      setState((){
        selectedIndex = index;
      });
    }
  }
  

}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您正在尝试创建一个包含 3 个项目的 BottomNavigationBar:交易、类别和注销。 但是,在 widgetOptions 列表中,您只包括 Transactions 和 Categories 小部件。 此外,在 onItemTapped function 中,您有一个 if statement 检查所选索引是否为 2(对应于注销项),但 if 语句中没有代码。

    要解决此问题,您应该将第三个小部件添加到与 Log out 项对应的 widgetOptions 列表中。

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多