2

I have a design which is like this :

Want to achieve

I have been reading bout the GridView and found it complex enough to get the result. I have also followed this question here : Flutter - Layout a Grid. Found out that this is for the fixed list, plus the list has different UI.

I have information coming up from the API, and I have a widget named as MoreInfo() which has a text args to be passed from the API.

I have achieved what I want to get the strings coming from the list. It is just that I'm trying to get the result but not giving the resultant very well.

Adding the list to the widget :

GridView getOtherInfo(BuildContext context){
  List<String> _yesElems = [];
  List<Widget> _widget = [];

  this.userBarModel.barModel.otherInfo.yes.forEach((item){
    _yesElems.add(item);
  });

  _yesElems.forEach((o){
    _widget.add(MoreInfoWidget(text: o));
  });

  return GridView.count(
    crossAxisCount: 4,
    children: _widget
  );
}

UI Code:

Column(
  corssAxisalignment: CrossAxisAlignment.stretch;
  children: <Widget>[
     Padding(
       padding: EdgeInsets.only(top: 9.0),
       child: Text("MORE INFO",
           style: TextStyle(color: Color.fromRGBO(183, 193, 203, 1), fontWeight: FontWeight.bold, fontSize: 11.0))
     ),
     SizedBox(height: 11.4),
     Container(
        height: 140,
        child: getOtherInfo(context)
     )
  ]
)

The result I'm getting right now is :

Image I'm getting

I'd like to get the aforementioned result. There is inbuilt top padding added to the UI. I don't know why. Can you help me, guys?

I have tried using the crossAixCount: 2, and the UI went blank. Any help would be appreciated. Thanks :)

Alok
  • 8,452
  • 13
  • 55
  • 93

1 Answers1

12

Maybe I didn't get you, can you try this code which is giving you a hint how you can do it your way and let me know if this is what you were looking for.

List<int> _list = [];

math.Random random = math.Random();

@override
void initState() {
  super.initState();

  Timer.periodic(Duration(milliseconds: 1000), (timer) {
    _list.add(random.nextInt(100));
    setState(() {});
  });
}

Widget build(context) {
  return Scaffold(
    appBar: AppBar(),
    body: GridView.count(
      crossAxisCount: 2,
      crossAxisSpacing: 160,
      childAspectRatio: 3,
      children: _list.map((value) {
        return Container(
          alignment: Alignment.center,
          margin: EdgeInsets.all(8),
          decoration: BoxDecoration(border: Border.all(color: Colors.black),),
          child: Text("Item ${value}"),
        );
      }).toList(),
    ),
  );
}

enter image description here

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • As you can see the design, I need two in a row. Could you please demonstrate with that? See the design for more info. The design is not at all matching with the current things which I'm looking for. – Alok Apr 11 '19 at 12:57
  • One more question since, I have to give out the height of the container. I have a card which is getting this data. But when I'm not giving out height it vanishses. I want the height which shoul wrap the content and make the card looks clean. I have given 100 for now, which I don't think is the correct way of doing the work. Thanks – Alok Apr 11 '19 at 13:38
  • There is no need to give `height` explicitly. You can play with `childAspectRatio: 3` and it is pretty much safe. – CopsOnRoad Apr 11 '19 at 14:12
  • Not working out for me. Throws a lot of Error. `flutter: parentData: (can use size) flutter: constraints: BoxConstraints(w=327.6, 0.0<=h<=Infinity) flutter: layer: OffsetLayer#c1b27 DETACHED flutter: size: MISSING flutter: axisDirection: down flutter: crossAxisDirection: right flutter: offset: ScrollPositionWithSingleContext#d64ad(offset: 0.0, range: null..null, viewport: null,`. I'm using `childAspectRatio: 6`, but not working out. – Alok Apr 11 '19 at 15:09
  • OK, remove `childAspectRatio` (used to adjust size) and `crossAxisSpacing` (used for giving space). – CopsOnRoad Apr 11 '19 at 15:34
  • Nothings working out. Only the explicit height solves the issue, but I don't know how to achieve the auto adjust sizing of the view without giving it an explicit height. – Alok Apr 11 '19 at 15:58
  • OK, tell me how you want it to look like, there has to be 2 items in one row? And you don't want to specify height, correct? – CopsOnRoad Apr 11 '19 at 16:08
  • Yes @CopsOnRoad. – Alok Apr 12 '19 at 05:59
  • OK, in the code I used above, you can see I haven't specified any `height`, so I am not able to get where are you using `height` property? – CopsOnRoad Apr 12 '19 at 06:17
  • See my UI code in the question, I'm specifying a height, as soon as the height has gone from the container it removes everything and gives me a lot of errors. – Alok Apr 12 '19 at 06:18
  • Can you ask a separate question showing the final UI you need I will be able to assist you better. – CopsOnRoad Apr 12 '19 at 06:20
  • Final UI is done, it is just that, this is a part of that card. I'm able to get ll the details but this thing messes up my UI which in the bottom. This is a different container, and I have shared you the code of that. The first screenshot is what I need in my question. So I have achieved my thing a bit, it is just the height issue which is hampering it. – Alok Apr 12 '19 at 06:22
  • Are you talking about `Container( height: 140, child: getOtherInfo(context) )` ? And what `getOtherInfo` is here? – CopsOnRoad Apr 12 '19 at 06:25
  • getOtherInfo is GridView widget where I'm adding all the responses from the yes array from the API, and then passing it to the `Container's child:`. The error when I remove the height from the container is : `The hitTest() method was called on this RenderBox: _RenderScrollSemantics#09740 relayoutBoundary=up15 Although this node is not marked as needing layout, its size is not set. A RenderBox object must have an explicit size before it can be hit-tested. Make sure that the RenderBox in question sets its size during layout.` **getOtherInfo code is also mentioned in my question**. – Alok Apr 12 '19 at 06:29
  • Try to use my code as primary and make changes accordingly. I'm really sorry I can't help you that much, if you still have any doubt, please raise a question, inform me and I'll try my best to answer you. Code in comments is indeed very verbose to understand. – CopsOnRoad Apr 12 '19 at 06:33
  • 1
    Okay. Thanks for the help. Very much appreciated. :) – Alok Apr 12 '19 at 06:35