gpt4 book ai didi

flutter - 实现 CupertinoActionSheet 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-05 08:51:48 25 4
gpt4 key购买 nike

我试图在我的 flutter 应用程序中实现 CupertinoActionSheet,但我总是失败。我很确定我拥有所有必要的东西,但我一直遇到以下错误:

使用不包含导航器的上下文请求的导航器操作。

我不明白为什么会出现此错误。实现 CupertinoActionSheet 的正确方法是什么?

代码:

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: Container(
color: Colors.black,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Card(
elevation: 20,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 180.0),
child: CupertinoButton(
color: Colors.black,
child: Text(
'Click me',
style: TextStyle(color: Colors.white),
),
onPressed: () {
final act = CupertinoActionSheet(
title: Text('Select Option'),
message:
Text('Which option?'),
actions: <Widget>[
CupertinoActionSheetAction(
child: Text('1'),
onPressed: () {
print('pressed');
},
)
],
cancelButton: CupertinoActionSheetAction(
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
));
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) => act);
},
),
),
]),
),
),
),
),
),
),
);
}

}

最佳答案

你可以使用 StatefulBuilder ,查看下面的完整代码
代码片段

children: <Widget>[
StatefulBuilder(builder:
(BuildContext context, StateSetter setState) {
return Padding(
padding: const EdgeInsets.only(top: 180.0),
child: CupertinoButton(

enter image description here

完整代码

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: Container(
color: Colors.black,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Card(
elevation: 20,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
StatefulBuilder(builder:
(BuildContext context, StateSetter setState) {
return Padding(
padding: const EdgeInsets.only(top: 180.0),
child: CupertinoButton(
color: Colors.black,
child: Text(
'Click me',
style: TextStyle(color: Colors.white),
),
onPressed: () {
final act = CupertinoActionSheet(
title: Text('Select Option'),
message: Text('Which option?'),
actions: <Widget>[
CupertinoActionSheetAction(
child: Text('1'),
onPressed: () {
print('pressed');
},
)
],
cancelButton: CupertinoActionSheetAction(
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
));
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) => act);
},
),
);
})
]),
),
),
),
),
),
),
);
}
}

关于flutter - 实现 CupertinoActionSheet 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58586031/

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