是否有任何方法来监听屏幕方向--屏幕正在旋转的时间,而不是屏幕完全旋转的时候(在本例中,OrientationBuilder完成了工作)。
码
class _MyPageState extends State<MyPage> {
static Widget _portrait = Column(
children: <Widget>[
Container(height: 240, color: Colors.blue),
Container(height: 240, color: Colors.red),
],
);
Widget _landscape = Center(child: FlutterLogo(size: 200));
Widget _child = _portrait;
Widget build(BuildContext context) {
return Scaffold(
body: OrientationBuilder(builder: (_, orientation) {
if (orientation == Orientation.portrait)
_child = _portrait;
else
_child = _landscape;
return AnimatedSwitcher(
duration: Duration(seconds: 2),
child: _child,
);
}),
);
}
}输出:

问题
正如您所看到的,当屏幕从肖像旋转到景观时,我会得到溢出错误,这是因为AnimatedSwitcher,如果我在duration中使用seconds: 0,就没有错误。有没有任何方法可以监听屏幕旋转的变化,以便我可以将seconds设置为0?或者允许我使用AnimatedSwitcher而不引起溢出问题的任何其他解决方案。
注:
我并不是在寻找解决办法,比如使用Wrap而不是Column,为什么需要使用AnimatedSwitcher等等。我正在开发的应用程序要求它保持原样,我只是创建了一个易于复制的回购,以反映我的代码。谢谢
发布于 2019-08-10 09:29:01
将主体内容封装到SingleChildScrollView中
class _MyPageState extends State<MyPage> {
static Widget _portrait = Column(
children: <Widget>[
Container(height: 240, color: Colors.blue),
Container(height: 240, color: Colors.red),
],
);
Widget _landscape = Center(child: FlutterLogo(size: 200));
Widget _child = _portrait;
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView (
child: OrientationBuilder(builder: (_, orientation) {
if (orientation == Orientation.portrait)
_child = _portrait;
else
_child = _landscape;
return AnimatedSwitcher(
duration: Duration(seconds: 2),
child: _child,
);
}),
),
);
}
}https://stackoverflow.com/questions/57440819
复制相似问题