在CupertinoNavigationBar中,如何在引导中显示除后退按钮之外的按钮?

时间:2019-04-03 03:39:09

标签: dart flutter flutter-cupertino

我尝试过这样的事情:

  @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仍然没有得到路线吗?

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

谢谢。

2 个答案:

答案 0 :(得分:0)

这是一个非常简单的代码段。我希望它能对您有所帮助:

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),
        ),
      ),
    );
  }
}

答案 1 :(得分:0)

  

哪种是手动添加后退按钮的正确方法?

我使用这样的代码:

@override
Widget build(BuildContext context) {
  return CupertinoPageScaffold(
    navigationBar: CupertinoNavigationBar(
      middle: Text(widget.title),
      leading: CupertinoNavigationBarBackButton(
        onPressed: () => Navigator.of(context).pop(),
      ),
      child: _myAppBody() // return widget
    ),
  );
}