gpt4 book ai didi

dart - 在 CupertinoNavigationBar 中,我如何在前导中显示除后退按钮之外的按钮?

转载 作者:IT王子 更新时间:2023-10-29 06:57:36 25 4
gpt4 key购买 nike

我试过这样的:

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

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

提前致谢。

最佳答案

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

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

关于dart - 在 CupertinoNavigationBar 中,我如何在前导中显示除后退按钮之外的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com