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 :)

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

16 Comments

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.
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
There is no need to give height explicitly. You can play with childAspectRatio: 3 and it is pretty much safe.
Not working out for me. Throws a lot of Error. flutter: parentData: <none> (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.
Okay. Thanks for the help. Very much appreciated. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.