I’ve got to this stage of a simple app, and am kind of stuck. I’m trying to make a ListView with rows, and a button "behind" the rows, so then whenever you click on a ListView Row, it will take you to a new screen. I’ve noticed whenever I’ve added a button to a row however, that the row’s color suddenly shrinks, and I cannot "expand" the color to fill the whole row back up again. I’ve tried the Expanded() widget with the flex property, but that did not do anything at all. Here is the piece of the code that I need help with, and a picture of the problem.
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NewScreen(),
),
);
},
child: Container(
color: Colors.yellow,
height: 100.0,
child: const Center(
child: Text('Entry B'),
),
),
),
As you can see, the yellow color has shrinked to a box, and you can see the button behind the row. I need the yellow to fully fill the row, as picture 2 shows.
Picture 2: You may see it better here. Again, I would like the yellow color to look like the other 3 (which do not have a button assigned to them yet
I’ve tried the Expanded() widget, but it didn’t work.
3
Answers
ElevatedButton
has it self style property in which you can specify the color for the button like this:There is 2 ways that you can do your task, using
ElevatedButton
or usingContainer
.Example for
ElevatedButton
Example for
Container
This is not the correct use-case of
ElevatedButton
. You shouldn’t useElevatedButton
here. You can make use ofInkWell
orGestureDetector
along withContainer
(just to get your color) instead ofElevatedButton
.ElevatedButton
should be preferred to use when you have an actual button. In your case, it’s just a clickable UI Element.Hence, you can use the following code: