web-dev-qa-db-fra.com

Comment résoudre «RenderBox n'a pas été présenté:» dans le flottement dans un widget de carte

J'ai une carte qui contient trois conteneurs. Les deux premiers ont du texte et le dernier est censé contenir un TextFormField avec du texte. J'ai donc une rangée pour tenir les deux l'un sur l'autre. La seule chose est que lorsque j'ajoute le widget TextFormField, il n'apparaît pas et la console affiche une erreur

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY 
╞═════════════════════════════════════════════════════════
I/flutter (14101): The following assertion was thrown during 
performLayout():
I/flutter (14101): BoxConstraints forces an infinite width.
I/flutter (14101): These invalid constraints were provided to 
RenderRepaintBoundary's layout() function by the
I/flutter (14101): following function, which probably computed the invalid 
constraints in question:
I/flutter (14101):   _RenderDecoration._layout.layoutLineBox 
(package:flutter/src/material/input_decorator.Dart:750:11)
I/flutter (14101): The offending constraints were:
I/flutter (14101):   BoxConstraints(w=Infinity, 0.0<=h<=100.0)



I/flutter (14101): Another exception was thrown: RenderBox was not laid out: 
RenderPadding#150b0 relayoutBoundary=up3 NEEDS-Paint
I/flutter (14101): Another exception was thrown: 
'package:flutter/src/rendering/shifted_box.Dart': Failed assertion: line 310 
pos 12: 'child.hasSize': is not true.
I/flutter (14101): Another exception was thrown: RenderBox was not laid out: 
RenderPhysicalShape#1d998 relayoutBoundary=up4

Le code ressemble

import 'package:flutter/material.Dart';

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Container(
    child: Center(
  child: Card(
    elevation: 2.0,
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          height: 100.0,
          color: Colors.purple,
        ),
        Container(
          height: 200.0,
          color: Colors.pink,
          child: Column(
            children: <Widget>[
              new RichText(
                textAlign: TextAlign.center,
                text: TextSpan(
                  text: 'Some Text',
                  style: TextStyle(
                      color: Colors.grey[800],
                      fontWeight: FontWeight.bold,
                      fontSize: 17.0),
                ),
              ),
              new RichText(
                softWrap: true,
                textAlign: TextAlign.center,
                text: TextSpan(
                  text:
                      'Some other text',
                  style: TextStyle(
                      color: Colors.black54,
                      fontWeight: FontWeight.normal,
                      fontSize: 17.0),
                  ),
                    ),
                  Row(
                children: <Widget>[
                  Text('adfa'),
                  Text('data'),
                  Form(
                    child: TextFormField(),
                  ),
                ],
              )
            ],
          ),
        ),
      ],
    ),
  ),
));
}
 }
7
Taio

TextFormField provoque le problème. Il a besoin de contraintes de largeur. Par exemple. enveloppez-le dans un widget étendu ou un conteneur avec une largeur.

11
German Saprykin

Je réponds en amélioration de German Saprykin post, je recevais également la même erreur ci-dessous

════════ (2) Exception interceptée par la bibliothèque de rendu ══════════════════════════ Un InputDecorator, qui est généralement créé par un TextField, ne peut pas avoir une largeur illimitée. Cela se produit lorsque le widget parent ne fournit pas de contrainte de largeur finie. Par exemple, si InputDecorator est contenu par une ligne, sa largeur doit être contrainte. Un widget Expanded ou un SizedBox peut être utilisé pour limiter la largeur du InputDecorator ou du TextField qui le contient. 'package: flutter/src/material/input_decorator.Dart': Assertion échouée: ligne 910 pos 7: 'layoutConstraints.maxWidth <double.infinity' L'ancêtre créé par l'utilisateur du widget à l'origine des erreurs était: Fichier TextField: /// C : /CommBack/My_Workplace/Flutter/wiremusic/wiremusic_dev/lib/wire/widgets/searchfield.Dart: 15: 14 ═════════════════════════ ═════════════════════════════════

════════ (3) Exception interceptée par la bibliothèque de rendu ══════════════════════════ RenderBox n'a pas été présenté: _RenderDecoration # b1ce0 relayoutBoundary = up26 NEEDS-Paint NEEDS-COMPOSITING-BITS-UPDATE 'package: flutter/src/rendering/box.Dart': Échec de l'assertion: ligne 1681 pos 12: 'hasSize' L'ancêtre créé par l'utilisateur du widget provoquant l'erreur était : Fichier TextField: /// C: /CommBack/My_Workplace/Flutter/wiremusic/wiremusic_dev/lib/wire/widgets/searchfield.Dart: 15: 14 ═════════════════ ═════════════════════════════════════════

à cause de cela ci-dessous les lignes sources précédentes.

return Container(
  color: Colors.yellow,
  constraints: BoxConstraints(minWidth: 230.0, minHeight: 25.0),
  child: TextField(),
);

parce que le TextField requiert explicitement l'ancêtre hasSize et après avoir mentionné explicitement la largeur à Container, l'erreur a disparu comme Thanos

return Container(
  color: Colors.yellow,
  width: 230,
  constraints: BoxConstraints(minWidth: 230.0, minHeight: 25.0),
  child: TextField(),
);

J'espère que cela aiderait quelqu'un.

1
ArifMustafa