首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振:如何设置屏幕旋转监听器

问颤振:如何设置屏幕旋转监听器
EN

Stack Overflow用户
提问于 2019-08-10 09:10:04
回答 1查看 1.6K关注 0票数 0

是否有任何方法来监听屏幕方向--屏幕正在旋转的时间,而不是屏幕完全旋转的时候(在本例中,OrientationBuilder完成了工作)。

码

代码语言:javascript
复制
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等等。我正在开发的应用程序要求它保持原样,我只是创建了一个易于复制的回购,以反映我的代码。谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-08-10 09:29:01

将主体内容封装到SingleChildScrollView中

代码语言:javascript
复制
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,
          );
        }),
      ),
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57440819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档