错误:在我的应用程序中,错误地使用了ParentDataWidget。

14 浏览
0 Comments

错误:在我的应用程序中,错误地使用了ParentDataWidget。

我正在使用Flutter制作一家报纸网站,但是每当我打开一个新闻项目时,就会出现以下错误:“Incorrect use of ParentDataWidget.”,有人知道如何解决吗?\n通知列表:\nclass ArticlesPosts {\n String title;\n String titleHome;\n String subtitle;\n String pic;\n String author;\n String notice;\n String data;\n ArticlesPosts({\n this.title,\n this.titleHome,\n this.subtitle,\n this.author,\n this.pic,\n this.notice,\n this.data,\n });\n}\nvar allPosts = [\n ArticlesPosts(\n title:\n \'XXXXXXX\',\n titleHome: \'XXXXXXXXXXXXX\',\n subtitle: \'sssssssssssssssssssssssssssssssssssssssssssssss\',\n author: \'nome\',\n pic: \'assets/1.jpg\',\n notice:\n \'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\',\n data: \'Published in 14/04/2021\',\n ),\n];\n\n主页新闻页面:\nimport \'package:flutter/material.dart\';\nimport \'package:flutter_application_1/views/home/posts.dart\';\nimport \'articlesDetails.dart\';\nclass MainScreen extends StatelessWidget {\n final posts = allPosts;\n @override\n Widget build(BuildContext context) {\n return ListView(\n children: [\n Column(children: [\n SizedBox(height: 20),\n Padding(\n padding: EdgeInsets.only(left: 15.0),\n child: Text(\n \"RobSIC News\",\n style: TextStyle(\n fontWeight: FontWeight.w800, height: 3.0, fontSize: 26),\n ),\n ),\n SizedBox(height: 30),\n Container(\n height: MediaQuery.of(context).size.height - 200,\n child: GridView.count(\n crossAxisCount: 1,\n crossAxisSpacing: 0.5,\n mainAxisSpacing: 0.5,\n childAspectRatio: 4,\n primary: false,\n children: [\n ...posts.map((e) {\n return buildPostsGrid(e, context);\n }).toList()\n ]),\n ),\n ])\n ],\n );\n }\n buildPostsGrid(ArticlesPosts posts, BuildContext context) {\n return Card(\n elevation: 9,\n child: FlatButton(\n onPressed: () {\n Navigator.of(context).push(MaterialPageRoute(\n builder: (context) => ArticlesDetail(selectedPosts: posts)));\n },\n child: Padding(\n padding: EdgeInsets.all(5.0),\n child: Stack(children: [\n Container(height: 400, width: 900.0, color: Colors.transparent),\n Positioned(\n left: 30.0,\n top: 30.0,\n child: Container(\n height: 30.0,\n width: 40.0,\n decoration: BoxDecoration(boxShadow: [\n BoxShadow(\n blurRadius: 7.0,\n color: Colors.grey.withOpacity(0.75),\n offset: Offset(5, 25),\n spreadRadius: 12.0)\n ]))),\n Positioned(\n left: 12.0,\n top: 15.0,\n child: Hero(\n tag: posts.pic,\n child: Container(\n height: 190.0,\n width: 1000,\n decoration: BoxDecoration(\n borderRadius: BorderRadius.circular(7.0),\n image: DecorationImage(\n image: AssetImage(posts.pic),\n fit: BoxFit.cover))))),\n Positioned(\n left: 15.0,\n top: 145,\n child: Column(children: [\n Text(posts.titleHome,\n style: TextStyle(\n fontWeight: FontWeight.w600,\n color: Colors.white70,\n height: 1.6,\n fontSize: 24)),\n ])),\n Positioned(\n left: 15.0,\n top: 179,\n child: Column(children: [\n Text(posts.subtitle,\n style: TextStyle(\n fontWeight: FontWeight.w400,\n color: Colors.white,\n height: 1.2,\n fontSize: 20)),\n ])),\n Positioned(\n left: 740.0,\n top: 210,\n child: Column(children: [\n Text(posts.data,\n style: TextStyle(\n fontWeight: FontWeight.w400,\n height: 1.2,\n fontSize: 14)),\n ])),\n Positioned(\n left: 15.0,\n top: 210,\n child: Column(children: [\n Text(posts.author,\n style: TextStyle(\n fontWeight: FontWeight.w400,\n height: 1.2,\n fontSize: 14)),\n ])),\n ]),\n )),\n );\n }\n}\n\n新闻详细页面:\nimport \'package:flutter/material.dart\';\nimport \'package:flutter_application_1/views/home/posts.dart\';\nclass ArticlesDetail extends StatefulWidget {\n final ArticlesPosts selectedPosts;\n const ArticlesDetail({Key key, this.selectedPosts}) : super(key: key);\n @override\n _ArticlesDetailState createState() => _ArticlesDetailState();\n}\nclass _ArticlesDetailState extends State {\n final posts = allPosts;\n @override\n Widget build(BuildContext context) {\n return Stack(\n children: [\n Container(\n padding: EdgeInsets.only(\n top: 25,\n bottom: 16,\n left: 16,\n right: 16,\n ),\n margin: EdgeInsets.only(top: 16),\n decoration: BoxDecoration(\n color: Colors.white,\n shape: BoxShape.rectangle,\n borderRadius: BorderRadius.circular(17),\n boxShadow: [\n BoxShadow(\n color: Colors.black26,\n blurRadius: 10,\n offset: Offset(0, 10),\n )\n ]),\n child: Stack(children: [\n ListView(\n children: [\n SizedBox(height: 10),\n Positioned(\n child: Container(\n height: 90,\n width: 300,\n child: Text(widget.selectedPosts.title,\n style: TextStyle(\n fontSize: 26, fontWeight: FontWeight.w600)),\n ),\n ),\n SizedBox(height: 10),\n Positioned(\n child: Container(\n height: 50,\n width: 300,\n child: Text(widget.selectedPosts.subtitle,\n style: TextStyle(\n fontSize: 18, fontWeight: FontWeight.w300)),\n ),\n ),\n SizedBox(height: 20),\n Positioned(\n child: Container(\n height: 50,\n width: 300,\n child: Text(\n \'_______________________________________________________________________________________________________________________\',\n style: TextStyle(\n fontSize: 18,\n fontWeight: FontWeight.w100,\n color: Colors.black12)),\n ),\n ),\n SizedBox(height: 20),\n Positioned(\n child: Container(\n height: 400,\n width: 200,\n decoration: BoxDecoration(\n image: DecorationImage(\n image: AssetImage(widget.selectedPosts.pic))))),\n SizedBox(height: 50),\n Positioned(\n child: Container(\n height: 150,\n width: 300,\n child: Text(widget.selectedPosts.notice,\n style: TextStyle(\n fontSize: 18, fontWeight: FontWeight.w400)),\n ),\n ),\n Align(\n alignment: Alignment.bottomRight,\n child: FlatButton(\n onPressed: () {\n Navigator.pop(context);\n },\n child: Text(\'Confirm\')))\n ],\n )\n ]),\n )\n ],\n );\n }\n}\n\n在这个意义上,我想知道是否有人遇到过这个错误,或者对如何解决这个问题有任何想法。提前感谢您的帮助!

0
0 Comments

error Incorrect use of ParentDataWidget in my app的问题出现的原因是在ListView中使用了Positioned-Widget,而Positioned-Widget只能在Stack中使用。

解决这个问题的方法是将ListView中的Positioned-Widget替换为Stack,并将Positioned-Widget放置在Stack的子组件中。下面是修复这个问题的代码示例:

Stack(

children: [

ListView(

// ListView的其他配置

children: [

// ListView的子组件

],

),

Positioned(

// Positioned-Widget的配置

),

],

)

通过将Positioned-Widget放置在Stack的子组件中,就可以在ListView中正确使用了。这样修复之后,就不会再出现error Incorrect use of ParentDataWidget的错误了。

0