我试图在我的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(
完整的代码
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);
},
),
);
})
]),
),
),
),
),
),
),
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句