下面是我的屏幕,我正在try

enter image description here

 body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(child:
            Chewie(
              controller: _chewieController,
            )
            ),
            TabBar(
              labelColor:Colors.black,
              tabs: categoryNames,
            ),
            Expanded(
                child:TabBarView(
                  children: [
                    ImageList()
                  ],
            ),
            ),
          ],
        )
    ),
  ),

但我不知道如何将屏幕划分为多个部分,并为它们添加小部件.请帮帮我.

以上是我迄今为止try 过的代码.

class ImageList extends StatelessWidget {
  final List<SubContentsDatum>images = [];

  //ImageModel data = new ImageModel();
  //ImageList();

  Widget build(context) {
    fetchSubCategoryContentlist(context, 20);
    print('iamgelist:$images');
    print('imagelistlengthimages:${images.length}');
    return Expanded(
      child: GridView.count(
        shrinkWrap: true,
        childAspectRatio: 2,
        scrollDirection: Axis.vertical,
        crossAxisCount: 2,
        children: new List<Widget>.generate(images.length, (index) {
          return buildImage(images[index], context, index);
        },
        ).toList(),
      ),
    );
  }

  Widget buildImage(SubContentsDatum image, BuildContext context, int index) {
    return SingleChildScrollView(
      padding: EdgeInsets.only(top: 20.0),
      child: Column(
        children: <Widget>[
          new InkResponse(
            child: Image.network(image.thumbnailUrl.replaceAll(
                "onnet-video-platform", "xxxxx")),
            onTap: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) =>
                  ChewieDemo.fromChewieDemo(subContentsData: images[index],)));
            },
          ),
          Text(image.name,
            style: TextStyle(
                color: Colors.white
            ),
          ),
        ],
      ),
    );
  }

  Future<SubContentModel> fetchSubCategoryContentlist(BuildContext context,
      int value) async {
    String url = "http://xxxx:xx/onnet_api/mediaListByCatId.php";
    var body = Map<String, String>();
    body['publisherid'] = 102.toString();
    body['tag'] = "media";
    body['subtag'] = "list";
    body['catId'] = value.toString();

    http.Response res = await http.post(url, body: body);
    final data = json.decode(res.body);
    var map = Map<String, dynamic>.from(data);
    var subCategoryContentsResponse = SubContentModel.fromJson(map);

    if (res.statusCode == 200) {
      if (subCategoryContentsResponse.status == 1) {
        var subData = data['data'] as List;
        print('subcontentsresponse:$subData');
        for (var model in subData) {
          images.add(new SubContentsDatum.fromJson(model));
        }
        print('playerlengthimages:${images.length}');
      }
    }
  }
}

这是我的Imagelist类文件,我try 了你们所有人说的,但是我没有得到输出.请判断一下这个代码.

推荐答案

你可以用-MediaQuery得到屏幕的大小,然后除以2得到上半部分.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: title,
//        theme: ThemeData.light().copyWith(
//          platform: _platform ?? Theme.of(context).platform,
//        ),
        home: DefaultTabController(
            length: 3,
            child: Scaffold(
//                appBar: AppBar(
//                  title: Text(title),
//                ),
                body: SafeArea(
                    child: Column(children: <Widget>[
                  Container(
                    color: Colors.greenAccent,
                    height: MediaQuery.of(context).size.height / 2.2,  // Also Including Tab-bar height.
//                        child: Chewie(
//                          controller: _chewieController,
//                        ),
                  ),
                  PreferredSize(
                    preferredSize: Size.fromHeight(50.0),
                    child: TabBar(
                      labelColor: Colors.black,
                      tabs: [
                        Tab(
                          text: 'One',
                        ),
                        Tab(
                          text: 'Two',
                        ),
                        Tab(
                          text: 'Three',
                        )
                      ], // list of tabs
                    ),
                  ),
                  //TabBarView(children: [ImageList(),])
                  Expanded(
                    child: TabBarView(
                      children: [
                        Container(
                          color: Colors.deepOrange,
                          child: Center(child: Text('Tab1')),
                        ),
                        Container(
                          color: Colors.red,
                          child: Center(child: Text('Tab2')),
                        ),
                        Container(
                          color: Colors.yellowAccent,
                          child: Center(child: Text('Tab3')),
                        ) // class name
                      ],
                    ),
                  ),
                ])))));
  }

输出:

enter image description here

使用AppBar-height: MediaQuery.of(context).size.height / 2.5, enter image description here

GridView.builder英寸-TabBarView

Expanded(
                    child: TabBarView(
                      children: [
                        GridView.builder(
                          itemBuilder: (context, int) {
                            return CircleAvatar(
                              backgroundImage: NetworkImage(
                                  'https://placeimg.com/640/480/any'),
                            );
                          },
                          itemCount: 20,
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 3),
                          shrinkWrap: true,
                        ),
                        Container(
                          color: Colors.red,
                          child: Center(child: Text('Tab2')),
                        ),
                        Container(
                          color: Colors.yellowAccent,
                          child: Center(child: Text('Tab3')),
                        ) // class name
                      ],
                    ),
                  ),

enter image description here

要获取异步数据,请使用-FutureBuilder

    @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      builder: (context,snap){
        if(snap.hasData){
          return Expanded(
            child: GridView.count(
              shrinkWrap: true,
              childAspectRatio: 2,
              scrollDirection: Axis.vertical,
              crossAxisCount: 2,
              children: new List<Widget>.generate(images.length, (index) {
                return buildImage(images[index], context, index);
              },
              ).toList(),
            ),
          );

        }
        return Center(child: CircularProgressIndicator())
      },
      future: fetchSubCategoryContentlist(context, 20),
    );

Flutter相关问答推荐

本机调试符号包含无效的目录调试符号.目前仅支持Android ABI

Flutter 中的多页表现

blocTest错误:没有从`when()`中调用方法存根

ReCAPTCHA Enterprise中的严重安全漏洞

点击表情符号按钮后,Flutter 文本域文本编辑控制器将文本恢复为原始值

悬停时打开Flutter 下拉按钮

在Fighter中,我如何在窗口的顶部和底部排列Widget?

try 使用fl_Chart of Ffltter,但在导入它时遇到以下错误:Error:';TextScaler';is;t a type

任务';:app:check DebugDuplicateClasss';的Ffltter Share_plus抛出执行失败

如何在点击查看措辞下方添加句子?

处理 flutter/dart 模型中的空值

如何在flutter中更改showModalBottomSheet小部件中CheckboxListTile小部件的值?

无法从一个页面导航到下一个页面 (Flutter)

Flutter如何在容器外创建环形进度条

在 Flutter 中显示有关提供者通知的对话框

应用程序和应用程序内的 webview 之间的单点登录

Android Electric eel Mac M1:运行 flutter doctor -v 时无法找到Bundle 的 java 版本

Flutter snapShot.hasData 为假但响应有数据

在自动完成中 Select 值后保持键盘焦点

如何从 Firebase Firestore 中获取具有特定字段值的文档?