skip to Main Content

i tried to wrap my cintainer with a sizedBox but it threw an error. i honestly don’t know what to do

// ignore_for_file: prefer_const_constructors, file_names, prefer_const_literals_to_create_immutables, use_full_hex_values_for_flutter_colors
 import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RowItemsWidget extends StatelessWidget {
  const RowItemsWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        children: [
        Container(
          margin: EdgeInsets.only(top: 10, bottom: 10, left: 15),
          padding: EdgeInsets.symmetric(horizontal: 10),
          height: 180,
          // width: 200,
          decoration: BoxDecoration(
              color: Color(0xfff5f9fd),
              borderRadius: BorderRadius.circular(10),
              boxShadow: [
                BoxShadow(
                  color: Color(0xff47569).withOpacity(0.3),
                  blurRadius: 5,
                  spreadRadius: 1,
                )
              ]),
          child: Row(children: [
            Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  // margin: EdgeInsets.only(top: 20, right: 70),
                  height: 120,
                  width: 130,
                  decoration: BoxDecoration(
                    color: Color(0xFF475269),
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                Image.asset(
                  "images/1.png",
                  height: 150,
                  width: 150,
                  fit: BoxFit.contain,
                )
              ],
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 20),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Nike Shoe',
                      style: TextStyle(
                        color: Color(0xff475269),
                        fontSize: 23,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    SizedBox(
                      height: 5,
                    ),
                    Text(
                      "Men's Shoe",
                      style: TextStyle(
                          color: Color(0xff475269).withOpacity(0.8),
                          fontSize: 16),
                    )
                  ]),
            ),
            Spacer(),
            Row(
              children: [
                Text(
                  "$50",
                  style: TextStyle(
                    color: Colors.redAccent,
                    fontSize: 22,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                SizedBox(width: 70),
                Container(
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Color(0xff475269),
                      borderRadius: BorderRadius.circular(10)),
                  child: Icon(
                    CupertinoIcons.cart_fill_badge_plus,
                    color: Colors.white,
                    size: 25,
                  ),
                ),
              ],
            )
          ]),
        )
      ]),
    );
  }
}

2

Answers


  1. Spacer() isnt suitable for your current widget tree. Replace the Spacer with SizedBox. You can use LayoutBuilder on scaffold body to get the layout- constrains .

    class RowItemsWidget extends StatelessWidget {
      const RowItemsWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
            
            children: [
            Container(
              margin: EdgeInsets.only(top: 10, bottom: 10, left: 15),
              padding: EdgeInsets.symmetric(horizontal: 10),
              height: 180,
              // width: 200,
              decoration: BoxDecoration(
                  color: Color(0xfff5f9fd),
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      color: Color(0xff47569).withOpacity(0.3),
                      blurRadius: 5,
                      spreadRadius: 1,
                    )
                  ]),
              child: Row(children: [
                Stack(
                  alignment: Alignment.center,
                  children: [
                    Container(
                      // margin: EdgeInsets.only(top: 20, right: 70),
                      height: 120,
                      width: 130,
                      decoration: BoxDecoration(
                        color: Color(0xFF475269),
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                    Image.asset(
                      "images/1.png",
                      height: 150,
                      width: 150,
                      fit: BoxFit.contain,
                    )
                  ],
                ),
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 20),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Nike Shoe',
                          style: TextStyle(
                            color: Color(0xff475269),
                            fontSize: 23,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          "Men's Shoe",
                          style: TextStyle(
                              color: Color(0xff475269).withOpacity(0.8),
                              fontSize: 16),
                        )
                      ]),
                ),
                // Spacer(),
                Row(
                  children: [
                    Text(
                      "$50",
                      style: TextStyle(
                        color: Colors.redAccent,
                        fontSize: 22,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    SizedBox(width: 70),
                    Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          color: Color(0xff475269),
                          borderRadius: BorderRadius.circular(10)),
                      child: Icon(
                        CupertinoIcons.cart_fill_badge_plus,
                        color: Colors.white,
                        size: 25,
                      ),
                    ),
                  ],
                )
              ]),
            )
          ]),
        );
      }
    }
    
    Login or Signup to reply.
  2. You can’t use a Spacer() inside a widget that doesn’t have a defined size. There were many redundant widgets in your code, e.g. Stack() and the inner Row(). Below you can find an optimised version of your code.

    class RowItemsWidget extends StatelessWidget {
      const RowItemsWidget({super.key});
    
     @override
      Widget build(BuildContext context) {
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Container(
            margin: EdgeInsets.only(top: 10, bottom: 10, left: 15),
            padding: EdgeInsets.symmetric(horizontal: 10),
            // width: 200,
            height: 180,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              color: Color(0xfff5f9fd),
              boxShadow: [
                BoxShadow(
                  color: Color(0xff47569).withOpacity(0.3),
                  blurRadius: 5,
                  spreadRadius: 1,
                )
              ],
            ),
            child: Row(
              children: [
                Container(
                  // margin: EdgeInsets.only(top: 20, right: 70),
                  height: 120,
                  width: 130,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(0xFF475269),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Image.asset(
                    "images/1.png",
                    fit: BoxFit.contain,
                    height: 150,
                    width: 150,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Nike Shoe',
                        style: TextStyle(
                          color: Color(0xff475269),
                          fontSize: 23,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      SizedBox(
                        height: 5,
                      ),
                      Text(
                        "Men's Shoe",
                        style: TextStyle(
                            color: Color(0xff475269).withOpacity(0.8),
                            fontSize: 16),
                      )
                    ],
                  ),
                ),
                Text(
                  "$50",
                  style: TextStyle(
                    color: Colors.redAccent,
                    fontSize: 22,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                SizedBox(width: 70),
                Container(
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Color(0xff475269),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Icon(
                    CupertinoIcons.cart_fill_badge_plus,
                    color: Colors.white,
                    size: 25,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search