Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

divide screen into two equal parts in flutter

Below is my screen, I am trying to get

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()
                  ],
            ),
            ),
          ],
        )
    ),
  ),

But I am not getting how to divide a screen into parts and adding widgets for them. Please help me with this.

Above is my code what I have tried so far.

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}');
      }
    }
  }
}

This is my imagelist class file I tried what all of you said but I didn't get the output.Please check with this code once.

like image 394
Mounika Avatar asked Feb 28 '19 06:02

Mounika


People also ask

How do you split the screen equally in flutter?

You can Use - MediaQuery to get the size of screen then divide it by 2 to get the First half.

How do you split widgets in flutter?

Splitting An App:Use the Flutter Outline tool to split an app into widgets as shown below: Flutter Outline is present on the right-hand side as a hidden tab. After opening the tab we can see the Widget Tree of the current dart file. Right-click on the widget we want to extract -> click on Extract Widget.


1 Answers

Using an Expanded widget makes a child of a Row, Column, or Flex expand to fill the available space in the main axis (e.g., horizontally for a Row or vertically for a Column). If multiple children are expanded, the available space is divided among them according to the flex factor.

https://docs.flutter.io/flutter/widgets/Expanded-class.html

Column(
  children: <Widget>[
    Expanded(
      child: TopWidget()
    ),
    CenterWidget(),
    Expanded(
      child: BottomWidget()
    ),
  ]
)

Edit: full source code here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @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> {

  @override
  Widget build(BuildContext context) {
    return Column(
        children: <Widget>[
          Expanded(
              child: Container(
                color: Colors.green,
              )
          ),
          Container(
              height: 40,
              color: Colors.grey
          ),
          Expanded(
              child: Container(
                color: Colors.blueGrey,
              )
          ),
        ]
    );
  }
}

Edit 2: and result here

result

like image 168
hiepav Avatar answered Sep 20 '22 04:09

hiepav