gpt4 book ai didi

flutter - SimpleDialog Flutter 中的 Snackbar

转载 作者:行者123 更新时间:2023-12-02 00:23:26 24 4
gpt4 key购买 nike

在我的 Simpledialog 中将 snackbar 添加到按下方法时,我遇到了下面的错误代码。[使用不包含脚手架的上下文调用的 Scaffold.of()。]

我想就如何提供正确的上下文来解决它征求你的意见。

import 'package:flutter/material.dart';



void main() {
runApp(new MaterialApp(home: new AlertApp()));
}

class AlertApp extends StatefulWidget {
@override
_AlertAppState createState() => _AlertAppState();
}

class _AlertAppState extends State<AlertApp> {


SimpleDialog _simdalog;

void sDialog(){
_simdalog = new SimpleDialog(
title: new Text("Add To Shopping Cart"),
children: <Widget>[
new SimpleDialogOption(
child: new Text("Yes"),
onPressed: (){
final snackBar = SnackBar(content: Text('Purchase Successful'));
Scaffold.of(context).showSnackBar(snackBar);
},
),
new SimpleDialogOption(
child: new Text("Close"),
onPressed:() {Navigator.pop(context);},
),
],
);
showDialog(context: context, builder: (BuildContext context){
return _simdalog;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: new Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
child: new Text("Add to Shopping Cart [Simple]"),
onPressed:(){
sDialog();
}),
],
),
),
);
}
}

最佳答案

解决方案 1:正如 Mazin Ibrahim 在评论 Scaffold.of() called with a context that does not contain a Scaffold 中提到的那样

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
...
Scaffold(
key: _scaffoldKey,
...
onPressed: () {

_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text('Purchase Successful'),
duration: Duration(seconds: 3),
));
}

解决方案 2:使用包 flushbar,您还可以在顶部显示通知
Flushbar 链接:https://github.com/AndreHaueisen/flushbar
另一个使用 flushbar How to show snackbar after navigator.pop(context) in Flutter? 的建议 enter image description here

    Flushbar(
title: "Hey Ninja",
message: "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
flushbarPosition: FlushbarPosition.TOP,
flushbarStyle: FlushbarStyle.FLOATING,
reverseAnimationCurve: Curves.decelerate,
forwardAnimationCurve: Curves.elasticOut,
backgroundColor: Colors.red,
boxShadows: [BoxShadow(color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)],
backgroundGradient: LinearGradient(colors: [Colors.blueGrey, Colors.black]),
isDismissible: false,
duration: Duration(seconds: 4),
icon: Icon(
Icons.check,
color: Colors.greenAccent,
),
mainButton: FlatButton(
onPressed: () {},
child: Text(
"CLAP",
style: TextStyle(color: Colors.amber),
),
),
showProgressIndicator: true,
progressIndicatorBackgroundColor: Colors.blueGrey,
titleText: Text(
"Hello Hero",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.yellow[600], fontFamily: "ShadowsIntoLightTwo"),
),
messageText: Text(
"You killed that giant monster in the city. Congratulations!",
style: TextStyle(fontSize: 18.0, color: Colors.green, fontFamily: "ShadowsIntoLightTwo"),
),
)..show(context);

关于flutter - SimpleDialog Flutter 中的 Snackbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617581/

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