【问题标题】:Proper way of changing scaffold content depending on orientation根据方向改变脚手架内容的正确方法
【发布时间】:2019-05-12 19:33:34
【问题描述】:

我有一个带有 GoogleMap 小部件和几个文本小部件的屏幕。我正在尝试做的是让地图在没有 AppBar 的情况下横向利用整个屏幕,同时在纵向方向限制为容器大小。

现在我只有 2 个脚手架小部件,它们在每次方向更改时都会重新绘制,在几次旋转之后,整个设备会冻结,我必须重新启动它。

Widget build(BuildContext context) {
final mediaQueryData = MediaQuery.of(context);
if (mediaQueryData.orientation == Orientation.landscape) {
  return Scaffold(
    body:GoogleMap(
            ...
          ),
  );
}
else{
  return Scaffold(
    appBar: AppBar(
      title: Text('Title'),
    ),
    body: ListView(
      scrollDirection: Axis.vertical,
      physics: NeverScrollableScrollPhysics(),
      children: <Widget>[
        Text('Text'),
        Text('Text'),
        Container(
          height: MediaQuery.of(context).size.height/3,
          child: GoogleMap(
            ...
          ),
        ),
      ],
    ),
  );
}

如果有人能指导我以更有效的方式做这样的事情,我将不胜感激。

另外,如果没有,我还想知道是否可以在嵌套在可滚动 ListView 中的 GoogleMap 小部件中使用工作手势(滚动、平移等)。

【问题讨论】:

    标签: dart flutter maps


    【解决方案1】:

    如果有人感兴趣,有人建议使用此方法,但评论被删除:
    Widget build(BuildContext context) { final isLandScape = MediaQuery.of(context).orientation == Orientation.landscape; return Scaffold( appBar: isLandScape ? null : AppBar( title: Text('Title'), ),
    我对包含地图以外的其他小部件的可见性小部件也使用了相同的方法:

     visible: isLandScape ? false : true,
    

    对于带有地图的容器也是如此:

    Container(
              height: isLandScape ? MediaQuery.of(context).size.height : MediaQuery.of(context).size.height/3,
              child: GoogleMap(
                ///
              ),
            ),
    

    现在似乎工作稳定了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多