【问题标题】:Why my Flutter Scaffold back button always return to root?为什么我的 Flutter Scaffold 后退按钮总是返回到 root?
【发布时间】:2021-02-09 10:53:00
【问题描述】:

如果我有一堆屏幕:A -> B -> C,当我按下模拟器上的返回按钮或按下 C 上 Scaffold 的返回按钮上的返回按钮时,我总是返回到 A,而不是 B。为什么?以及如何解决?我搜索了

这就是我如何推送和弹出。这些代码被简化为仅显示推送/弹出和屏幕构建功能。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: UserListPage()
    );
  }
}



class UserListPage extends StatefulWidget {
  @override
  _UserListPageState createState() => _UserListPageState();
}

class _UserListPageState extends State<UserListPage> {
  ...
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("User list")),
        body: Builder(builder: (context) { 
          InkWell(
            onTap: () { 
              Navigator.push(
                context, 
                MaterialPageRoute(builder: (context) => SessionListPage(userId: users[index].id)
              ),
            child: ...
          })
        }),
      );
    }
    ...
}



class SessionListPage extends StatefulWidget {

  final int userId;

  SessionListPage({ this.userId }) : super();

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

class _SessionListPageState extends State<SessionListPage> {
  ...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Session list")),
        body: Builder(builder: (context) { 
          InkWell(
            onTap: () { 
              Navigator.push(
                context, 
                MaterialPageRoute(builder: (context) => DrawingPage(userId: widget.userId, sessionId: sessions[index].id)
              ),
            child: ...
          })
      }),
    );
  }

  ...
}


class DrawingPage extends StatefulWidget {
  final int userId;
  final int sessionId;

  DrawingPage({ this.userId, this.sessionId }) : super();

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

class _State extends State<DrawingPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text("Enter the Number")),
      body: Builder(builder: (context) {
          InkWell(
            onTap: () { Navigator.pop(context); } // here it returns straight to UserListPage instead of SessionListPage
            child: ...
          })
      }
    ));
  }
}

我注意到的一件事是,提供给 push 和 pop 的上下文来自 Builder,而不是 Widget 构建函数提供的上下文。有影响吗?

【问题讨论】:

  • 你检查我的代码了吗?
  • 嗨@NikhilVadoliya。我检查了您的代码,它包含带有“按钮”演示的基本 Flutter 启动代码。你确定你给了我正确的链接吗?
  • 这个链接过期了,为什么
  • 我会分享的

标签: flutter navigation


【解决方案1】:

年轻,一切正常。请查看以下链接

Code

【讨论】:

    【解决方案2】:

    请从所有地方删除super();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多