Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter Gridview未在景观中填充主体_Flutter_Flutter Layout - Fatal编程技术网

Flutter Gridview未在景观中填充主体

Flutter Gridview未在景观中填充主体,flutter,flutter-layout,Flutter,Flutter Layout,我使用的是在横向和纵向模式下都需要响应的视图,当我切换到横向模式时,我使用的是gridview,但是在横向模式下,gridview不能填充整个身体 Gridview似乎没有默认的填充或其他内容 导入“包装:颤振/材料.省道”; 导入“包:flifter/services.dart”; void main()异步{ SystemChrome.setPreferredOrientations([ DeviceOrientation.Up, DeviceOrientation.landscapeL

我使用的是在横向和纵向模式下都需要响应的视图,当我切换到横向模式时,我使用的是gridview,但是在横向模式下,gridview不能填充整个身体

Gridview似乎没有默认的填充或其他内容

导入“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main()异步{
SystemChrome.setPreferredOrientations([
DeviceOrientation.Up,
DeviceOrientation.landscapeLeft,
设备定向。景观权
]);
runApp(
MyApp(),
);
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
双高;
final key=GlobalKey();
@凌驾
initState(){
//在呈现布局后调用getHeight函数
WidgetsBinding.instance.addPostFrameCallback((\u)=>getHeight());
super.initState();
}
getHeight(){
final RenderBox renderBoxRed=key.currentContext.findRenderObject();
最终尺寸=renderBoxRed.size.height;
打印(“容器尺寸:$SIZE”);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:布局生成器(
生成器:(BuildContext上下文,BoxConstraints){
高度=约束。最大高度;
return(MediaQuery.of(context).size.width>=600&&
MediaQuery.of(context.orientation==orientation.scape)
?GridView.count(
交叉轴计数:2,
收缩膜:对,
儿童:[
容器(
高度:高度,,
颜色:颜色,黑色,
),
容器(
钥匙:钥匙,
高度:高度,,
颜色:颜色,红色,
child:buildChildren(),
)
],
)
:列(
儿童:[
容器(
高度:高度/2,
颜色:颜色,黑色,
),
容器(
钥匙:钥匙,
高度:高度/2,
颜色:颜色,红色,
child:buildChildren(),
)
],
);
},
),
背景颜色:Colors.yellow,
);
}
建筑儿童(){
返回列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
居中(
子:文本(
“车身高度:”+高度.toString(),
样式:TextStyle(颜色:Colors.white,字体大小:30),
),
),
居中(
子项:MediaQuery.of(context.orientation==orientation.scape?文本(
“容器高度:”+640.0.toString(),
样式:TextStyle(颜色:Colors.white,字体大小:30),
):文本(
“容器高度:”+576.0.toString(),
样式:TextStyle(颜色:Colors.white,字体大小:30),
),
),
居中(
子:文本(
“设备高度:”+MediaQuery.of(context).size.height.toString(),
样式:TextStyle(颜色:Colors.white,字体大小:30),
),
),
],
);
}
}
这是重现此问题所需的最少代码。预期输出是黑色容器占据整个可用屏幕空间,因此脚手架背景色都不可见。感谢您的帮助


我的目标是在nexus 10 emu上测试的所有平板电脑上获得一张图像,以填充整个黑色容器,该容器应以横向模式填充整个身体
emulator速度很慢,我必须按下按钮让emulator知道我更改为横向

编辑我已经编辑了我的完整代码,在图像部分,您需要

FittedBox(
          child: Image.asset(
          'assets/images/bg.jpg',
          ),
          fit: BoxFit.fill,
         ))
完整代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() async {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double height;
  double width;
  final key = GlobalKey();

  @override
  initState() {
    //calling the getHeight Function after the Layout is Rendered
    WidgetsBinding.instance.addPostFrameCallback((_) => getHeight());

    super.initState();
  }

  getHeight() {
    final RenderBox renderBoxRed = key.currentContext.findRenderObject();
    final size = renderBoxRed.size.height;
    print("SIZE of container: $size");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          height = constraints.maxHeight;
          width  = constraints.maxWidth;
          return (MediaQuery.of(context).size.width >= 600 &&
                  MediaQuery.of(context).orientation == Orientation.landscape)
              ? GridView.count(
                  crossAxisCount: 2,
                  shrinkWrap: true,
                  children: [
                    Container(
                        height: height,
                        width: width / 2,
                        color: Colors.black,
                        child: FittedBox(
                          child: Image.asset(
                            'assets/images/bg.jpg',
                          ),
                          fit: BoxFit.fill,
                        )),
                    Container(
                      key: key,
                      height: height,
                      color: Colors.red,
                      child: buildChildren(),
                    )
                  ],
                )
              : Column(
                  children: <Widget>[
                    Container(
                        height: height / 2,
                        width: width ,
                        color: Colors.black,
                        child: FittedBox(
                          child: Image.asset(
                            'assets/images/bg.jpg',
                          ),
                          fit: BoxFit.fill,
                        )),
                    Container(
                      key: key,
                      height: height / 2,
                      width: width ,
                      color: Colors.red,
                      child: buildChildren(),
                    )
                  ],
                );
        },
      ),
      backgroundColor: Colors.yellow,
    );
  }

  buildChildren() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Center(
          child: Text(
            "Body height: " + height.toString(),
            style: TextStyle(color: Colors.white, fontSize: 30),
          ),
        ),
        Center(
          child: MediaQuery.of(context).orientation == Orientation.landscape
              ? Text(
                  "Container height: " + 640.0.toString(),
                  style: TextStyle(color: Colors.white, fontSize: 30),
                )
              : Text(
                  "Container height: " + 576.0.toString(),
                  style: TextStyle(color: Colors.white, fontSize: 30),
                ),
        ),
        Center(
          child: Text(
            "Device height: " + MediaQuery.of(context).size.height.toString(),
            style: TextStyle(color: Colors.white, fontSize: 30),
          ),
        ),
      ],
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main()异步{
SystemChrome.setPreferredOrientations([
DeviceOrientation.Up,
DeviceOrientation.landscapeLeft,
设备定向。景观权
]);
runApp(
MyApp(),
);
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
双高;
双倍宽度;
final key=GlobalKey();
@凌驾
initState(){
//在呈现布局后调用getHeight函数
WidgetsBinding.instance.addPostFrameCallback((\u)=>getHeight());
super.initState();
}
getHeight(){
final RenderBox renderBoxRed=key.currentContext.findRenderObject();
最终尺寸=renderBoxRed.size.height;
打印(“容器尺寸:$SIZE”);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:布局生成器(
生成器:(BuildContext上下文,BoxCons)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() async {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double height;
  double width;
  final key = GlobalKey();

  @override
  initState() {
    //calling the getHeight Function after the Layout is Rendered
    WidgetsBinding.instance.addPostFrameCallback((_) => getHeight());

    super.initState();
  }

  getHeight() {
    final RenderBox renderBoxRed = key.currentContext.findRenderObject();
    final size = renderBoxRed.size.height;
    print("SIZE of container: $size");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          height = constraints.maxHeight;
          width  = constraints.maxWidth;
          return (MediaQuery.of(context).size.width >= 600 &&
                  MediaQuery.of(context).orientation == Orientation.landscape)
              ? GridView.count(
                  crossAxisCount: 2,
                  shrinkWrap: true,
                  children: [
                    Container(
                        height: height,
                        width: width / 2,
                        color: Colors.black,
                        child: FittedBox(
                          child: Image.asset(
                            'assets/images/bg.jpg',
                          ),
                          fit: BoxFit.fill,
                        )),
                    Container(
                      key: key,
                      height: height,
                      color: Colors.red,
                      child: buildChildren(),
                    )
                  ],
                )
              : Column(
                  children: <Widget>[
                    Container(
                        height: height / 2,
                        width: width ,
                        color: Colors.black,
                        child: FittedBox(
                          child: Image.asset(
                            'assets/images/bg.jpg',
                          ),
                          fit: BoxFit.fill,
                        )),
                    Container(
                      key: key,
                      height: height / 2,
                      width: width ,
                      color: Colors.red,
                      child: buildChildren(),
                    )
                  ],
                );
        },
      ),
      backgroundColor: Colors.yellow,
    );
  }

  buildChildren() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Center(
          child: Text(
            "Body height: " + height.toString(),
            style: TextStyle(color: Colors.white, fontSize: 30),
          ),
        ),
        Center(
          child: MediaQuery.of(context).orientation == Orientation.landscape
              ? Text(
                  "Container height: " + 640.0.toString(),
                  style: TextStyle(color: Colors.white, fontSize: 30),
                )
              : Text(
                  "Container height: " + 576.0.toString(),
                  style: TextStyle(color: Colors.white, fontSize: 30),
                ),
        ),
        Center(
          child: Text(
            "Device height: " + MediaQuery.of(context).size.height.toString(),
            style: TextStyle(color: Colors.white, fontSize: 30),
          ),
        ),
      ],
    );
  }
}