Flutter中Expanded组件ParentDataWidget使用错误求助
错误信息
Incorrect use of ParentDataWidget.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a RepaintBoundary widget.
问题原因
错误源于第一个TabBarView子组件的错误嵌套:Expanded是专门用于Flex类容器(如Row、Column)的组件,用来分配剩余空间,但你直接把Expanded作为TabBarView的子组件,而TabBarView的子组件容器并非Flex类型,导致Expanded无法正确传递父数据。
另外,ListView本身具备自动填充可用空间的特性,你已经设置了shrinkWrap: true,完全不需要额外用Expanded包裹。
修复后的代码
@override Widget build(BuildContext context) { return DefaultTabController( initialIndex: 0, length: 2, child: Scaffold( appBar: AppBar( actions: [ Align( alignment: Alignment.center, child: Text( "$energy", style: TextStyle( color: Colors.black, fontSize: 25.0, fontWeight: FontWeight.w500, ), ), ), SizedBox( height: 10, width: 10, ), SafeArea( child: Stack( fit: StackFit.passthrough, children: [ IconButton( onPressed: () { _showRewardedAd(); }, icon: Icon( Icons.bolt_sharp, size: 40, color: Colors.yellowAccent, ), ), Positioned( height: 3.0, right: 3.0, child: Icon(Icons.ads_click, size: 20), ), ], ), ), ], title: const Text('Example'), bottom: const TabBar( indicatorColor: Colors.grey, tabs: <Widget>[ Tab( icon: Icon(Icons.cloud_outlined), ), Tab( icon: Icon(Icons.beach_access_sharp), ), ], ), ), body: TabBarView( children: <Widget>[ // 移除外层的Expanded,直接返回ListView ListView.separated( physics: BouncingScrollPhysics(), itemCount: 5, shrinkWrap: true, separatorBuilder: (BuildContext context, int index) => const Divider( height: 15, ), itemBuilder: (BuildContext context, int index) { return ListTile( onTap: () { setState( () { Navigator.of(context).push( MaterialPageRoute( builder: (context) { return ConversationPage(index, energy); }, ), ); }, ); }, leading: CircleAvatar( radius: 30, backgroundImage: AssetImage(charactersRepository .characters[index].circleAvatarImage), ), trailing: Icon(Icons.push_pin), title: Text( charactersRepository.characters[index] .nameWithSurname(), style: TextStyle( fontSize: 15.0, ), ), subtitle: FutureBuilder( future: subTitleTxt(index), builder: (BuildContext context, AsyncSnapshot<String> text) { return Text(text.data ?? "- - -"); }), ); }, ), Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, children: [ PhysicalModel( color: Colors.white30, elevation: 8.0, shadowColor: Colors.purpleAccent, shape: BoxShape.circle, child: Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(5.0), child: CircleAvatar( backgroundColor: Colors.white30, radius: 50, child: Text("Gün $newDay", textDirection: TextDirection.ltr, style: GoogleFonts.breeSerif(color:Colors.black87,fontSize: 25),), ), ), Align( alignment: AlignmentDirectional.topStart, widthFactor:0, heightFactor: 0, child: Padding( padding: EdgeInsets.all(10.0), child: SizedBox( width: 200, height: 100, child: Column( children: [ Text("OKUL GAZETESİ", style: GoogleFonts.pacifico(fontSize: 20)), Text(newsRepository.news[0].newTitle[newDay-1],style: GoogleFonts.courgette(fontSize: 18)), ], ), ), ), ), ], ), ), Expanded( child: Column( children: [ Container( decoration: BoxDecoration( image: new DecorationImage(image: new AssetImage("images/bg2.png"), fit: BoxFit.cover,opacity:200) ), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( child: ListView.builder( shrinkWrap: true, reverse: true, itemCount: newLenght, itemBuilder: (BuildContext context, int index) { return ListTile(title: Align( alignment: Alignment.center, child: ClipRRect( borderRadius: BorderRadius.circular(15.0), child: Container( padding: EdgeInsets.all(8.0), decoration: BoxDecoration( color: Colors.deepPurpleAccent.shade100, ), child: Text(newsRepository.news[0].newsSubtitle[index], style: TextStyle(color: Colors.white), ), ),)),);}),), SizedBox(height: 10,),],), ),],),), ], ), ], ), ), ); }
额外说明
第二个Tab中的Expanded是正确的,因为它嵌套在Column(Flex类容器)内部,用来分配剩余空间给下方的内容,不需要修改。
内容的提问来源于stack exchange,提问作者Emre

