我试图在脚手架上创建一个snackbar,但是使用不包含scaffold的上下文调用了Scaffold.of()。我无法解决它,我试着放一个键,但是上面有一个错误,不能设置一个键,这是我的代码:
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text('Log in',
style: TextStyle(color: Colors.black),
textAlign: TextAlign.center),
),
SizedBox(
width: 500,
height: 50.0,
child: RaisedButton(
textColor: Colors.white,
color: Colors.blue,
child: Text('Log In'),
onPressed: () => {
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Done!'),))
}))
])
],
),
),
),
));
}
}发布于 2020-08-11 17:10:32
可以通过在RaisedButton周围添加一个名为Builder的小部件来解决这个问题。这会导致出现一个新的上下文来解决问题,因为您使用的是实例化Scaffold的小部件的上下文,而不是Scaffold的子上下文。希望这会有所帮助,我已经在下面包含了一个更新的代码段来帮助!
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text('Log in',
style: TextStyle(color: Colors.black),
textAlign: TextAlign.center),
),
SizedBox(
width: 500,
height: 50.0,
child: Builder(
builder: (context) {
return RaisedButton(
textColor: Colors.white,
color: Colors.blue,
child: Text('Log In'),
onPressed: () => {
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Done!'),))
})
}
)
)
])
],
),
),
),
));
}
}https://stackoverflow.com/questions/63362957
复制相似问题