6

我试过这样的事情:

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text(widget.title),
      ),
      child: Center(
        child: Container(
          child: CupertinoButton.filled(
            child: const Text('Push screen'),
            onPressed: () {
              CupertinoNavigationBar navBar = CupertinoNavigationBar(
                leading: Row(children: <Widget>[
                  const CupertinoNavigationBarBackButton(),
                  CupertinoButton(
                    child: const Text('Button 2'),
                    padding: EdgeInsets.zero,
                    onPressed: () {},
                  ),
                ]),
              );
              Navigator.push(context, CupertinoPageRoute<CupertinoPageScaffold>(
                builder: (_) => CupertinoPageScaffold(
                  navigationBar: navBar,
                  child: Center(child: const Text('Content')),
                )
              ));
            },
          ),
        ),
      ),
    );
  }

当点击按钮时,它失败了

I/flutter (30855): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (30855): The following NoSuchMethodError was thrown building CupertinoNavigationBarBackButton(dirty):
I/flutter (30855): The getter 'canPop' was called on null.
I/flutter (30855): Receiver: null
I/flutter (30855): Tried calling: canPop

原因是这个代码在CupertinoNavigationBarBackButton返回null

final ModalRoute<dynamic> currentRoute = ModalRoute.of(context);

我想知道为什么会这样?是不是因为我按下按钮的时候context还没有找到路线?

手动添加后退按钮的正确方法是什么?

提前致谢。

4

2 回答 2

5

这是一个非常简单的代码片段。我希望它可以帮助你:

class DemoView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        leading: GestureDetector(
          onTap: () {
            debugPrint('Back button tapped');
          },
          child: Row(
            children: <Widget>[
              Icon(CupertinoIcons.left_chevron),
              Text(
                'Back',
                style: TextStyle(
                  color: CupertinoColors.activeBlue,
                ),
              ),
            ],
          ),
        ),
        middle: Text('Demo'),
        trailing: GestureDetector(
          onTap: () {
            debugPrint('add icon tapped');
          },
          child: Icon(
            CupertinoIcons.add,
            color: CupertinoColors.black,
          ),
        ),
      ),
      child: Center(
        child: Text(
          'Content',
          style: TextStyle(fontSize: 36.0),
        ),
      ),
    );
  }
}
于 2019-06-23T15:44:03.283 回答
1

手动添加后退按钮的正确方法是什么?

我使用这样的代码:

@override
Widget build(BuildContext context) {
  return CupertinoPageScaffold(
    navigationBar: CupertinoNavigationBar(
      middle: Text(widget.title),
      leading: CupertinoNavigationBarBackButton(
        onPressed: () => Navigator.of(context).pop(),
      ),
      child: _myAppBody() // return widget
    ),
  );
}
于 2019-12-13T14:32:06.373 回答