如何在我的应用程序中更改页面方向

时间:2019-05-15 12:29:26

标签: dart flutter

我想将我的应用程序中仅一页的方向更改为横向以查看较大的图片

我只能找到用于设置整个应用方向的代码。我不知道如何在现有代码中实现此(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.

1 个答案:

答案 0 :(得分:0)

您正在使用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);
        },
      ),
    );
  }
}