0

I'm trying to recreate this button:
enter image description here

The button has two different background colors and a Text in each section. The "click" is unique, so it doesn't matter which side the user will click.

I tried this code, but the containers aren't covering the entire area of the button.

EDIT: complete widget tree below

Card(
                elevation: 3.0,
                shadowColor: Colors.grey.shade50,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0)),
                child: Container(
                 
                  child: Column(
                    children: [
                      Text("${data["teams"]}"),
                      Row(

                        children: [
                          Expanded(
                            child: TextButton(
                              onPressed: () {  },
                              child: Row(

                                children: [
                                  Container(
                                    color: Colors.redAccent,
                                    child: Text("1"),
                                  ),
                                  Container(color: Colors.white,)
                                ],
                              ),
                            )
                          ),
                          Expanded(
                              child: TextButton(
                                onPressed: () {  },
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Container(
                                        color: Colors.redAccent,
                                        child: Text("X"),
                                      ),
                                    ),
                                    Expanded(
                                      child: Container(color: Colors.white,),
                                    )
                                  ],
                                ),
                              )
                          ),
                          Expanded(
                              child: TextButton(
                                onPressed: () {  },
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Container(
                                        color: Colors.redAccent,
                                        child: Text("2"),
                                      ),
                                    ),
                                    Expanded(
                                      child: Container(color: Colors.white,),
                                    )
                                  ],
                                ),
                              )
                          ),
                        ],
                      )

                    ],
                  )
                )

            );

2 Answers 2

2

You can wrap row with clickable widget like InkWell, or GestureDetector to get tap event

SizedBox(
  height: kToolbarHeight,
  width: 200,
  child: ClipRRect(
    borderRadius: BorderRadius.circular(12),
    child: GestureDetector(
      onTap: () {},
      child: Row(
        children: [
          Flexible(
            flex: 4,
            child: Container(
              alignment: Alignment.center,
              color: Colors.cyanAccent,
              child: Text("1"),
            ),
          ),
          Flexible(
            flex: 6,
            child: Container(
              alignment: Alignment.center,
              color: Colors.white,
              child: Text("1.24"),
            ),
          )
        ],
      ),
    ),
  ),
)

enter image description here

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Card(
                elevation: 3.0,
                shadowColor: Colors.grey.shade50,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0)),
                child: Container(
                    child: Column(
                  children: [
                    Text("{data[]}"),
                    Row(
                      children: [
                        Expanded(
                            child: TextButton(
                          onPressed: () {},
                          child: Row(
                            children: [
                              Container(
                                color: Colors.redAccent,
                                child: Text("1"),
                              ),
                              Container(
                                color: Colors.white,
                              )
                            ],
                          ),
                        )),
                        Expanded(
                            child: TextButton(
                          onPressed: () {},
                          child: Row(
                            children: [
                              Expanded(
                                child: Container(
                                  color: Colors.redAccent,
                                  child: Text("X"),
                                ),
                              ),
                              Expanded(
                                child: Container(
                                  color: Colors.white,
                                ),
                              )
                            ],
                          ),
                        )),
                        Expanded(child: myButton()),
                      ],
                    )
                  ],
                )))
          ],
        ),
      ),
    );
  }

  ClipRRect myButton() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(12),
      child: GestureDetector(
        onTap: () {},
        child: Row(
          children: [
            Flexible(
              flex: 4,
              child: Container(
                alignment: Alignment.center,
                color: Colors.cyanAccent,
                child: Text("1"),
              ),
            ),
            Flexible(
              flex: 6,
              child: Container(
                alignment: Alignment.center,
                color: Colors.white,
                child: Text("1.24"),
              ),
            )
          ],
        ),
      ),
    );
  }
Sign up to request clarification or add additional context in comments.

10 Comments

My button has no fixed size but it is wrapped in Expanded. Will this work anyways?
Hope it would, but if you could provide more about parent widget, It would be easy to test it out.
I've just added the entire widget tree
Does this produce error on your test case.
I need to use Expanded because I have 3 button in a row, so I have to remove SizedBox. And if I remove it, it produce RenderBox was not laid out: RenderPhysicalShape#815a6 relayoutBoundary=up16 'package:flutter/src/rendering/box.dart': Failed assertion: line 1979 pos 12: 'hasSize'
|
0

You can solve this by adding the property crossAxisAlignment to CrossAxisAlignment.stretch in your row:

Row(
         crossAxisAlignment: CrossAxisAlignment.stretch
         children: [
              Expanded(
                   child: Container(
                         color: Colors.redAccent,
                         child: Text("1"),
                         ),
                        ),
              Expanded(
                  child: Container(color: Colors.white,),
                      )
                   ]
                ),

3 Comments

The following error is shown : RenderBox was not laid out: RenderPhysicalShape#a8e29 relayoutBoundary=up16 'package:flutter/src/rendering/box.dart': Failed assertion: line 1979 pos 12: 'hasSize'
hum I see, your Button should have an infinite height, so setting crossAxisAlignment: CrossAxisAlignment.stretch throws this. Try to wrap your Button in a SizedBox with a static width and height. If you can't do that my solution isn't going to be perfect
I have 3 button in a row that are wrapped with Expanded so it can't be static in my usecase. Thank you anyway!

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.