0

To elaborate, I want a gridview builder with two items on cross axis. The problem is I want the last item to be in the middle as shown in the first image. I can't find a way to do it.

enter image description here

I have done some researches about it. But I cant find a way to do it. Here is my code and my UI right now.

GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.5,
          crossAxisSpacing: 20,
          mainAxisSpacing: 20,
        ),
        itemCount: 3,
        itemBuilder: (BuildContext context, int index) {
          return Column(
            children: [
              GestureDetector(
                onTap: (() => {
          
                    }),
                child: SizedBox(
                  width: 160,
                  height: 300,
                  child: Stack(
                    children: [
                      Positioned(
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            color: Colors.white,
                            image: DecorationImage(
                              image: AssetImage(
                                  bgImagePath ?? themeLists[index]),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ),
                       Positioned(
                              top: 20,
                              left: 0,
                              right: 90,
                              child: Transform.rotate(
                                  angle: -math.pi / 4,
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: const Color(0xffD1D1D1),
                                        borderRadius:
                                            BorderRadius.circular(10)),
                                    width: 180,
                                    height: 20,
                                    child:
                                        const Center(child: Text("OWNED")),
                                  )))
                        
                    ],
                  ),
                ),
              ),
              const Text("100 points"),
            ],
          );
        },
      ),

enter image description here

1
  • How about using different widget? Commented Jun 1, 2022 at 15:05

1 Answer 1

1

As for UI you can use Wrap with alignment:WrapAlignment.center,. For the constraints I am using LayoutBuilder. If you using spacing minimize width of children.

return LayoutBuilder(
  builder: (context, constraints) {
    return Wrap(
      alignment: WrapAlignment.center,
      spacing: 12,
      runSpacing: 12,
      children: List.generate(
        3,
        (index) => Container(
          width: constraints.maxWidth / 2 - 12,
          height: constraints.maxHeight / 2 - 12,
          alignment: Alignment.center,
          color: index.isEven ? Colors.green : Colors.red,
          child: Text(index.toString()),
        ),
      ),
    );
  },
);

enter image description here

You can also use A inner row with Column for simple case.

return LayoutBuilder(
  builder: (context, constraints) {
    return Column(children: [
      Row(
        children: [
          Container(
            width: constraints.maxWidth / 2,
            height: constraints.maxHeight / 2,
            color: Colors.red,
          ),
          Container(
            width: constraints.maxWidth / 2,
            height: constraints.maxHeight / 2,
            color: Colors.green,
          ),
        ],
      ),
      Container(
        width: constraints.maxWidth / 2,
        height: constraints.maxHeight / 2,
        color: Colors.orange,
      ),
    ]);
  },
);
Sign up to request clarification or add additional context in comments.

Comments

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.