How to solve BoxConstraints forces an infinite width exception in flutter?

Issue

Hello i was just trying to include a switch button on the right side of a list item. While doing so i ended up with this BoxConstraints forces an infinite width error. As im a complete beginner to flutter Can someone please let me know what does it means? and what changes i have to do in my code to get rid of this error?

import 'package:attendee/constants.dart';
import 'package:flutter/material.dart';
import 'package:attendee/models/userdeails.dart';

class userdetailsTile extends StatelessWidget {
  final userdetails userdetail;
  userdetailsTile({this.userdetail});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Card(
            //margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
            child: GestureDetector(
              child: ListTile(
                leading: CircleAvatar(
                  radius: 25.0,
                  backgroundColor: Colors.lightGreen,
                ),
                title: Text(userdetail.fullname),
              ),
            ),
          ),
          Switch(
            value: present,
            onChanged: (value) {
              //setState(() {
              present = value;
              print(present);
              //});
            },
            activeTrackColor: Colors.lightGreenAccent,
            activeColor: Colors.green,
          ),
          //),
        ],
      ),
    );
  }
}

Solution

Please check the following solutions.

  • Solution 1

     return Material(child: Padding(
        padding: EdgeInsets.only(top: 8.0),
        child: ListTile(
                leading: CircleAvatar(
                  radius: 25.0,
                  backgroundColor: Colors.lightGreen,
                ),
                title: Text(userdetail.fullname),
                trailing: Switch(
                  value: present,
                  onChanged: (value) {
                    //setState(() {
                    present = value;
                    print(present);
                    //});
                  },
                  activeTrackColor: Colors.lightGreenAccent,
                  activeColor: Colors.green,
                ),
              ),
        ),
      );
    
  • Solution 2

     return Material(child: 
       Padding(
        padding: EdgeInsets.only(top: 8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: Card(
                //margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
                child: Row(
                  children: [
                    CircleAvatar(
                      radius: 25.0,
                      backgroundColor: Colors.lightGreen,
                    ), Text(userdetail.fullname)
                  ],
                ),
              ),
            ),
            Switch(
              value: present,
              onChanged: (value) {
                //setState(() {
                present = value;
                print(present);
                //});
              },
              activeTrackColor: Colors.lightGreenAccent,
              activeColor: Colors.green,
            ),
            //),
          ],
        ),
       ),
      );
    

Answered By – Jimenez

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.