【问题标题】:How to change the orientation of on page in my app如何在我的应用中更改页面上的方向
【发布时间】:2019-10-02 14:25:48
【问题描述】:

我想将我的应用中只有一个页面的方向更改为横向以查看更大的图片

我只能找到为整个应用程序设置方向的代码。我不知道如何将这个(Flutter: How to set and lock screen orientation on-demand)代码实现到现有代码中

//    ========  CAROUSEL  =========
    return Scaffold(
      appBar: AppBar(
        backgroundColor: new Color(0xFFFA983A),
        title: Image.asset(
          'assets/images/logo_white.png',
          fit: BoxFit.cover,
        ),
        elevation: 0.0,
        centerTitle: true,
        actions: <Widget>[
          new IconButton(
            icon: new Icon(Icons.search),
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => new SearchPage()));
            },
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Container(
            alignment: Alignment.topCenter,
            height: 200,
            child: GestureDetector(
              child: Hero(
                tag: 'carouselHero',
                child: SizedBox.expand(
                  child: Stack(
                    children: <Widget>[
                      imageSlider,
                      banner,
                    ],
                  ),
                ),
              ),
              onDoubleTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (_) {
                  return ImageScreen();
                }));
              },
            ),
          ),
          SizedBox(height: 4),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: 12.0, top: 4.0, right: 12.0),
              child: SingleChildScrollView(
                child: Container(
                  height: MediaQuery.of(context).size.height,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            widget.propertyDetailTown,
                            style: TextStyle(
                                color: Color(0xFFFA983A),
                                fontSize: 20.0,
                                fontWeight: FontWeight.bold),
                          ),
                          Text(
                            "Ref: RBD02548649",
                            style: TextStyle(
                                color: Colors.grey,
                                fontSize: 14.0,
                                fontWeight: FontWeight.normal),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            widget.propertyDetailArea,
                            style: TextStyle(
                                fontSize: 20.0, fontWeight: FontWeight.bold),
                          ),
                          Text(
                            widget.propertyDetailPrice,
                            style: TextStyle(
                                fontSize: 20.0,
                                fontWeight: FontWeight.normal,
                                color: Color(0xFFFA983A)),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 8,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Image.asset(
                                'assets/images/icons/beds_new.png',
                                scale: 1.75,
                              ),
                              Text(" 4"),
                              SizedBox(
                                width: 8,
                              ),
                              Image.asset(
                                'assets/images/icons/bath_new.png',
                                scale: 1.75,
                              ),
                              Text(
                                " 3",
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Image.asset(
                                'assets/images/icons/parking_new.png',
                                scale: 1.75,
                              ),
                              Text(" 2"),
                            ],
                          ),
                          Text(
                            "1200 sqr ft.",
                            style: TextStyle(color: Colors.grey),
                          )
                        ],
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      Divider(
                        height: 10,
                      ),
                      SizedBox(
                        height: 4,
                      ),
//            PROPERTY DESCRIPTION ==============================
                      Text(
                        'Description',
                        style: TextStyle(
                            color: Colors.black.withOpacity(0.8),
                            fontSize: 18.0,
                            fontWeight: FontWeight.bold),
                      ),
                      Text(
                        "Lorem Ipsum is simply dummy text of the printing and typesetting "
                        "industry. Lorem Ipsum has been the industry's standard dummy "
                        "text ever since the 1500s, when an unknown printer took a "
                        "galley of type and scrambled it to make a type specimen book. "
                        "It has survived not only five centuries, but also the leap "
                        "into electronic typesetting, remaining essentially unchanged.",
                        style: TextStyle(
                            color: Colors.black.withOpacity(0.6),
                            fontSize: 16.0,
                            fontWeight: FontWeight.normal),
                      ),
                      SizedBox(height: 8),
                      Divider(height: 10),
                      Text(
                        "AMENITIES",
                        style: TextStyle(
                            color: Colors.black.withOpacity(0.8),
                            fontSize: 18.0,
                            fontWeight: FontWeight.bold),
                      ),
                      SizedBox(height: 8.0),
//            AMENITIES STARTS HERE ===============================
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.hotel,
                            color: Color(0xFFFA983A),
                          ),
                          Text(
                            "  One",
                            style: TextStyle(fontSize: 16),
                          ),
                          SizedBox(width: 100.0),
                          Icon(
                            Icons.room,
                            color: Color(0xFFFA983A),
                          ),
                          Text("  Two")
                        ],
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Text(
                        "Home Loan Calculator",
                        style: TextStyle(
                            color: Colors.black.withOpacity(0.8),
                            fontSize: 18.0,
                            fontWeight: FontWeight.bold),
                      ),

//                        Image.asset("assets/google_map.jpg"),
                    ],
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
//    ===========SECOND PAGE NEEDS TO BE LANDSCAPE ==================
class ImageScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget imageSlider = Container(
      height: MediaQuery.of(context).size.height,
      child: Carousel(
        boxFit: BoxFit.cover,
        images: [
          AssetImage('assets/images/houses/house.jpg'),
          AssetImage('assets/images/houses/house1.jpg'),
          AssetImage('assets/images/houses/house2.jpg'),
          AssetImage('assets/images/houses/house3.jpg'),
          AssetImage('assets/images/houses/house4.jpg'),
        ],
        autoplay: false,
        dotBgColor: Colors.transparent,
        dotSize: 0,

      ),
    );

    return Scaffold(
      body: GestureDetector(
        child: Center(
          child: Hero(
            tag: 'carouselHero',
            child: imageSlider,
          ),
        ),
        onTap: () {
          Navigator.pop(context);
        },
      ),
    );
  }
}```

when i click on a picture it has to go to a new page which automatically change orientation to Landscape ans when i click back to previous page it changes again.

【问题讨论】:

  • 谢谢@HugoPassos,但它似乎不起作用,也许我在错误的地方添加了代码......?
  • 问题重复,请查看提供的答案。
  • @Hosar 是的,但我如何实现它......?请看我提供的代码...

标签: dart flutter


【解决方案1】:

您正在使用StatelessWidget,因此您无法覆盖initStatedispose。首先,将您的StatelessWidget 转换为StatefulWidget,然后实现另一个答案提供的代码。

class ImageScreen extends StatefulWidget {
  @override
  ImageScreenState createState() => ImageScreenState();
}

class ImageScreenState extends State<ImageScreen> {
  @override
  void initState(){
    super.initState();
    SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  dispose(){
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Widget imageSlider = Container(
      height: MediaQuery.of(context).size.height,
      child: Carousel(
        boxFit: BoxFit.cover,
        images: [
          AssetImage('assets/images/houses/house.jpg'),
          AssetImage('assets/images/houses/house1.jpg'),
          AssetImage('assets/images/houses/house2.jpg'),
          AssetImage('assets/images/houses/house3.jpg'),
          AssetImage('assets/images/houses/house4.jpg'),
        ],
        autoplay: false,
        dotBgColor: Colors.transparent,
        dotSize: 0,

      ),
    );

    return Scaffold(
      body: GestureDetector(
        child: Center(
          child: Hero(
            tag: 'carouselHero',
            child: imageSlider,
          ),
        ),
        onTap: () {
          Navigator.pop(context);
        },
      ),
    );
  }
}

【讨论】:

  • 谢谢@HugoPassos 高度赞赏。你能给我建议如何处理我在第一页的底部溢出.. 已经添加 SingleChildScrollView 但如果我添加内容它会溢出。
  • 如果我的回答对您有用,请将其标记为正确。我无法运行您的代码来检查导致溢出的原因,因为有很多未知变量,例如imageSliderwidget.propertyDetailArea。请使用可重现的代码编辑您的问题。
  • 请原谅我对这一切还很陌生,但现在就标记你的答案。 ImageSlide 只是 200 高容器中的轮播小部件,widget.propertyDetails 只是我的 json 文件的 getter 之一。旋转木马后的区域是可书写的,但如果我添加更多内容,它就会溢出。再次感谢您的帮助
  • 所以它基本上来自expanded,我希望它可以根据需要滚动包含尽可能多的内容。 widget.propertyDetailPrice 可以替换为任何文本...
  • 先生,很抱歉,我不能在这上面浪费太多时间。如果您可以使用可重现的代码编辑您的问题,我很乐意为您提供帮助。实际上,您应该提出另一个问题。我不能将两个不同的答案混在一个问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多