BoxConstraints приводит к бесконечной ширине. Возможная причина: _RenderListTile._layoutBox. Нарушающие ограничения: BoxConstraints(w=Infinity, 0.0<=h<=Infinity)

Я столкнулся с ошибкой в ​​своем приложении Flutter и обращаюсь за помощью в ее устранении. Вот сообщение об ошибке:

FlutterError (BoxConstraints forces an infinite width.
These invalid constraints were provided to RenderParagraph's layout() function by the following function, which probably computed the invalid constraints in question:
  _RenderListTile._layoutBox (package:flutter/src/material/list_tile.dart:1274:9)
The offending constraints were:
  BoxConstraints(w=Infinity, 0.0<=h<=Infinity))

Фрагмент кода:

import 'package:blood_token_app/models/services_model/blood_request_model.dart';
import 'package:blood_token_app/screens/main_screens/details_screen.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/svg.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(
        title: Text('Blood Token'),
      ),
      body: Container(
        height: size.height,
        width: size.width,
        child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(
                child: Padding(
                  padding:
                      const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  child: Center(
                    child: Card(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          SvgPicture.asset(
                            "assets/logos/032-blood_bag.svg",
                            height: size.height * 0.18,
                          ),
                          Text(
                            "Donate Blood\n Save Lives",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: size.height * 0.030,
                              color: Colors.black,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              Column(
                children: [
                  ListTile(
                    title: Text(
                      'Current Requests',
                      style: TextStyle(fontSize: 20.0),
                    ),
                    trailing: Text('See all'),
                  ),
                  Expanded(
                    child: StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance
                          .collection('blood_requests')
                          .snapshots(),
                      builder: (context, snapshot) {
                        if (snapshot.connectionState ==
                            ConnectionState.waiting) {
                          return AppUtils.customProgressIndicator();
                        }
                        if (snapshot.hasError) {
                          return Text('Error: ${snapshot.error}');
                        }
                        final List<BloodRequestModel> bloodRequests = snapshot
                            .data!.docs
                            .map((doc) => BloodRequestModel.fromJson(
                                doc.data() as Map<String, dynamic>))
                            .toList();

                        return Container(
                          width: MediaQuery.of(context)
                              .size
                              .width, // Ensure finite width
                          child: SingleChildScrollView(
                            scrollDirection: Axis.horizontal,
                            child: Row(
                              children: bloodRequests.map((value) {
                                return Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 10, vertical: 10),
                                  child: Container(
                                    width:
                                        300, // Set a finite width as per your design
                                    decoration: BoxDecoration(
                                      shape: BoxShape.rectangle,
                                      boxShadow: [
                                        BoxShadow(
                                          color: Colors.grey.withOpacity(0.5),
                                          spreadRadius: 5,
                                          blurRadius: 7,
                                          offset: Offset(0, 3),
                                        ),
                                      ],
                                      color: Theme.of(context)
                                          .scaffoldBackgroundColor,
                                      borderRadius: BorderRadius.circular(20.0),
                                      border: Border.all(
                                        color: Colors.grey.shade100,
                                        width: 0.0,
                                      ),
                                    ),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        ListTile(
                                          title: Text(
                                            "Requester Name",
                                            textAlign: TextAlign.left,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          subtitle: Text(
                                            "${value.requesterName}",
                                            textAlign: TextAlign.left,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          trailing: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text("Urgency Level"),
                                              Text(
                                                "${value.urgencyLevel}",
                                                textAlign: TextAlign.left,
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ],
                                          ),
                                        ),
                                        ListTile(
                                          title: Text("Location"),
                                          subtitle:
                                              Text("${value.customLocation}"),
                                          trailing: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text("Blood Type"),
                                              Text("${value.bloodType}"),
                                            ],
                                          ),
                                        ),
                                        ListTile(
                                          leading: Icon(Icons.watch_outlined),
                                          title: Text(
                                              "${value.formattedTimestamp()}"),
                                        ),
                                        MaterialButton(
                                          shape: RoundedRectangleBorder(
                                            borderRadius: BorderRadius.only(
                                              bottomLeft: Radius.circular(20),
                                              bottomRight: Radius.circular(20),
                                            ),
                                          ),
                                          height: size.height * 0.060,
                                          minWidth: size.width,
                                          color: AppUtils.redColor,
                                          onPressed: () {
                                            if (mounted) {
                                              Navigator.push(context,
                                                  MaterialPageRoute(
                                                      builder: (_) {
                                                return DetailsScreen(
                                                    requesterName:
                                                        value.requesterName,
                                                    bloodType: value.bloodType,
                                                    quantityNeeded:
                                                        value.quantityNeeded,
                                                    urgencyLevel:
                                                        value.urgencyLevel,
                                                    location: value.location,
                                                    contactNumber:
                                                        value.contactNumber,
                                                    patientName:
                                                        value.patientName,
                                                    date: value
                                                        .formattedTimestamp(),
                                                    customLocation:
                                                        value.customLocation,
                                                    // Convert latitude string to double
                                                    latitude: double.parse(value
                                                        .location!
                                                        .split(', ')[0]),
                                                    // Convert longitude string to double
                                                    longitude: double.parse(
                                                        value.location!
                                                            .split(', ')[1]));
                                              }));
                                            }
                                          },
                                          child: Text(
                                            "Details",
                                            style: TextStyle(
                                              color: AppUtils.whiteColor,
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              }).toList(),
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Что вы пробовали и чего ожидали?

Я попытался сделать карточку прокручиваемой по горизонтали, используя ListView, обернутый SingleChildScrollView внутри Column. Я ожидал, что карточки с запросами на сдачу крови будут прокручиваться горизонтально, позволяя пользователям просматривать несколько запросов. Однако я столкнулся с ошибкой:

Exception has occurred.
FlutterError (BoxConstraints forces an infinite width.
These invalid constraints were provided to RenderParagraph's layout() function by the following function, which probably computed the invalid constraints in question:
  _RenderListTile._layoutBox (package:flutter/src/material/list_tile.dart:1274:9)
The offending constraints were:
  BoxConstraints(w=Infinity, 0.0<=h<=Infinity))

Я ожидал, что карточки будут плавно прокручиваться, отображая все запросы на пожертвования горизонтально в пределах доступной ширины экрана. Однако сообщение об ошибке указывало на проблему с ограничениями, особенно в отношении ширины.

Одно дочернее представление прокрутки, обертывающее столбец, лучше реализовать как ListView...

Randal Schwartz 14.04.2024 22:43

@RandalSchwartz Хорошо

Muhammad Ali 16.07.2024 13:22
Стоит ли изучать PHP в 2026-2027 годах?
Стоит ли изучать PHP в 2026-2027 годах?
Привет всем, сегодня я хочу высказать свои соображения по поводу вопроса, который я уже много раз получал в своем сообществе: "Стоит ли изучать PHP в...
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
В JavaScript одним из самых запутанных понятий является поведение ключевого слова "this" в стрелочной и обычной функциях.
Приемы CSS-макетирования - floats и Flexbox
Приемы CSS-макетирования - floats и Flexbox
Здравствуйте, друзья-студенты! Готовы совершенствовать свои навыки веб-дизайна? Сегодня в нашем путешествии мы рассмотрим приемы CSS-верстки - в...
Тестирование функциональных ngrx-эффектов в Angular 16 с помощью Jest
В системе управления состояниями ngrx, совместимой с Angular 16, появились функциональные эффекты. Это здорово и делает код определенно легче для...
Концепция локализации и ее применение в приложениях React ⚡️
Концепция локализации и ее применение в приложениях React ⚡️
Локализация - это процесс адаптации приложения к различным языкам и культурным требованиям. Это позволяет пользователям получить опыт, соответствующий...
Пользовательский скаляр GraphQL
Пользовательский скаляр GraphQL
Листовые узлы системы типов GraphQL называются скалярами. Достигнув скалярного типа, невозможно спуститься дальше по иерархии типов. Скалярный тип...
0
2
79
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Удалите виджет Expanded над StreamBuilder, вы не сможете развернуть его в режиме прокрутки.

StreamBuilder все еще внутри SingleChildScrollView, ты не можешь расшириться внутри.

Я попытался запустить ваш код с фиктивными данными и вот результат:

и вот ваш виджет, я использовал фиктивный поток, чтобы имитировать ваши данные:

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(
        title: Text('Blood Token'),
      ),
      body: Container(
        height: size.height,
        width: size.width,
        child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(
                child: Padding(
                  padding:
                      const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  child: Center(
                    child: Card(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Container(
                            width: 100,
                            height: 100,
                            color: Colors.purple,
                            child: Text(
                              'Your Picture'
                          ,
                          style: TextStyle(
                            color: Colors.white,
                          )
                            )
                          ),
                          Text(
                            "Donate Blood\n Save Lives",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: size.height * 0.030,
                              color: Colors.black,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              Column(
                children: [
                  ListTile(
                    title: Text(
                      'Current Requests',
                      style: TextStyle(fontSize: 20.0),
                    ),
                    trailing: Text('See all'),
                  ),
                  StreamBuilder(
                    stream: getStream(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState ==
                          ConnectionState.waiting) {
                        return CircularProgressIndicator();
                      }
                      if (snapshot.hasError) {
                        return Text('Error: ${snapshot.error}');
                      }
                      final list = [for(int i = 0 ; i < 10 ; i++) i];

                      return Container(
                        width: MediaQuery.of(context)
                            .size
                            .width, // Ensure finite width
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: Row(
                            children: list.map((value) {
                              return Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 10, vertical: 10),
                                child: Container(
                                  width:
                                      300, // Set a finite width as per your design
                                  decoration: BoxDecoration(
                                    shape: BoxShape.rectangle,
                                    boxShadow: [
                                      BoxShadow(
                                        color: Colors.grey.withOpacity(0.5),
                                        spreadRadius: 5,
                                        blurRadius: 7,
                                        offset: Offset(0, 3),
                                      ),
                                    ],
                                    color: Theme.of(context)
                                        .scaffoldBackgroundColor,
                                    borderRadius: BorderRadius.circular(20.0),
                                    border: Border.all(
                                      color: Colors.grey.shade100,
                                      width: 0.0,
                                    ),
                                  ),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      ListTile(
                                        title: Text(
                                          "Requester Name",
                                          textAlign: TextAlign.left,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                        subtitle: Text(
                                          "${value}",
                                          textAlign: TextAlign.left,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                        trailing: Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Text("Urgency Level"),
                                            Text(
                                              "${value}",
                                              textAlign: TextAlign.left,
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          ],
                                        ),
                                      ),
                                      ListTile(
                                        title: Text("Location"),
                                        subtitle:
                                            Text("${value}"),
                                        trailing: Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Text("Blood Type"),
                                            Text("${value}"),
                                          ],
                                        ),
                                      ),
                                      ListTile(
                                        leading: Icon(Icons.watch_outlined),
                                        title: Text(
                                            "${value}"),
                                      ),
                                      MaterialButton(
                                        shape: RoundedRectangleBorder(
                                          borderRadius: BorderRadius.only(
                                            bottomLeft: Radius.circular(20),
                                            bottomRight: Radius.circular(20),
                                          ),
                                        ),
                                        height: size.height * 0.060,
                                        minWidth: size.width,
                                        color: Colors.red,
                                        onPressed: () {
                
                                          },
                                      
                                        child: Text(
                                          "Details",
                                          style: TextStyle(
                                            color: Colors.orange,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Stream<int> getStream()async*{

    for(int i = 0 ; i < 10 ; i++)
    yield i;
  }
}

Надеюсь, это вам поможет.

спасибо за помощь, у меня это сработало.

Muhammad Ali 16.07.2024 13:23

Другие вопросы по теме