【问题标题】:Flutter: Navigate to other page not workingFlutter:导航到其他页面不起作用
【发布时间】:2020-07-31 17:18:59
【问题描述】:

我在单独的页面上创建了一个 AppBar。所以,我可以在任何需要的地方包含它。 此应用栏包含一个弹出菜单。我希望如果用户从菜单中选择项目,那么它将重定向到该页面。

AppBar.dart

import 'package:flutter/material.dart';
import 'package:fnapp/home_screen.dart';

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];


class BaseAppBar extends StatelessWidget with PreferredSizeWidget {
  final Color backgroundColor = Colors.red;
   @override
  Widget build(BuildContext context) {

    return AppBar(
      backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: BackButton(
          color: Colors.black87,
          onPressed: (){//  onPressed: () => Navigator.of(context).pop(),
            print('back pressed');
          },
        ), 
        actions: <Widget>[ 

        PopupMenuButton<Choice>(
              icon: Icon(
                Icons.more_vert,
                color: Colors.black,
              ),
              onSelected: _select,
              itemBuilder: (BuildContext context) {
              return choices.skip(0).map((Choice choice) {
                return PopupMenuItem<Choice>(
                  value: choice,
                  child: Text(choice.title)
                  );
                }).toList();
              },
        ),
        ],

    );

  }

  @override
 // Size get preferredSize => new Size.fromHeight(appBar.preferredSize.height);
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
void _select(Choice choice) {
    if(choice.title == 'Bus'){
      print('Bus');
    }else if(choice.title == 'Car'){
     // print('Car');
     _navigateToHome();
    }

  }

_navigateToHome() 计划使用这个函数基本上将其重定向到另一个页面。

void _navigateToHome(){
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (BuildContext context) => HomeScreen()
      )
    );
  }

HomeScreen() 页面添加到 AppBar.dart 文件中。

Profile.dart

import 'dart:math';
import 'package:fnapp/appbar.dart';
import 'package:flutter/material.dart';
import 'package:fnapp/util/data.dart';
import 'package:flutter/services.dart';
import 'package:fnapp/home_screen.dart';

class OthersProfile extends StatefulWidget {
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<OthersProfile> {
  @override
    void _navigateToHome(){
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (BuildContext context) => HomeScreen()
      )
    );
  }
  int currentIndex = 0;
@override
  Widget build(BuildContext context) {

    return Scaffold(

        appBar: BaseAppBar(),

      body:----some long codes here----
),
},
},

我尝试使用 _navigateToHome() 函数在 Scaffold/Widget Build 内部/外部,即使在两个文件(AppBar 和 Profile)中也是如此。 但它给了我以下错误。

编译器消息:lib/appbar.dart:66:6:错误:找不到方法: '_navigateToHome'。 _navigateToHome();

编译器消息:lib/appbar.dart:88:18:错误:找不到 Getter: '语境'。 Navigator.of(context).pushReplacement(

我该如何解决这个问题?

编辑

根据建议,我从函数中删除了下划线。但它仍然给我一个错误。

我从两个文件中删除了 _。我在 _Select 函数下从 AppBar 中删除。但它不工作给出同样的错误。另外,我尝试将 Select 功能从 AppBar 移动到 Profile,但它也给出了错误。

我认为用户在 GitHub 中提出了类似的问题。 https://github.com/flutter/flutter/issues/21728

【问题讨论】:

  • 尝试使用普通方法,而不是私有方法。我之前遇到过同样的问题并通过这种方法解决了。
  • 感谢 Sanket 的评论。任何相同的例子。我是 Flutter 的新手。现在倾斜。
  • 去掉方法名上的首字母_。 _ 表示它是私有的,因此只能在同一个文件中使用。
  • 现在举个例子会很复杂..但是您至少可以尝试从所有方法和实现中删除该下划线...如果它不起作用,我一定会举个例子。 :)
  • 感谢 cmets 伙计们。只有一个帮助。我需要从两个文件中删除 _ 还是仅从 AppBar.dart 中删除。我在 _Select 函数下从 AppBar 中删除。但它不工作给出同样的错误。我也尝试将 Select 功能从 AppBar 移动到 Profile,但它也给出了错误。

标签: flutter dart


【解决方案1】:

日志中的错误很明显:Error: Method not found: '_navigateToHome'. _navigateToHome(); - 您尝试调用的方法不可访问。

如果您想从不同的类调用 _navigateToHome() 函数,您可以遵循类似的Stack Overflow answer,其中该函数作为构造函数传递以便可访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2022-01-06
    • 2020-07-29
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多