0

I'm making a newspaper website on Flutter, but whenever I open a news item, the following error appears: "Incorrect use of ParentDataWidget.", does anyone have any idea how to solve it?

List notices:

class ArticlesPosts {
  String title;
  String titleHome;
  String subtitle;
  String pic;
  String author;
  String notice;
  String data;

  ArticlesPosts({
    this.title,
    this.titleHome,
    this.subtitle,
    this.author,
    this.pic,
    this.notice,
    this.data,
  });
}

var allPosts = [
  ArticlesPosts(
    title:
        'XXXXXXX',
    titleHome: 'XXXXXXXXXXXXX',
    subtitle: 'sssssssssssssssssssssssssssssssssssssssssssssss',
    author: 'nome',
    pic: 'assets/1.jpg',
    notice:
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    data: 'Published in 14/04/2021',
  ),

];

Home Newspage:

import 'package:flutter/material.dart';
import 'package:flutter_application_1/views/home/posts.dart';

import 'articlesDetails.dart';

class MainScreen extends StatelessWidget {
  final posts = allPosts;

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Column(children: [
          SizedBox(height: 20),
          Padding(
            padding: EdgeInsets.only(left: 15.0),
            child: Text(
              "RobSIC News",
              style: TextStyle(
                  fontWeight: FontWeight.w800, height: 3.0, fontSize: 26),
            ),
          ),
          SizedBox(height: 30),
          Container(
            height: MediaQuery.of(context).size.height - 200,
            child: GridView.count(
                crossAxisCount: 1,
                crossAxisSpacing: 0.5,
                mainAxisSpacing: 0.5,
                childAspectRatio: 4,
                primary: false,
                children: [
                  ...posts.map((e) {
                    return buildPostsGrid(e, context);
                  }).toList()
                ]),
          ),
        ])
      ],
    );
  }

  buildPostsGrid(ArticlesPosts posts, BuildContext context) {
    return Card(
      elevation: 9,
      child: FlatButton(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => ArticlesDetail(selectedPosts: posts)));
          },
          child: Padding(
            padding: EdgeInsets.all(5.0),
            child: Stack(children: [
              Container(height: 400, width: 900.0, color: Colors.transparent),
              Positioned(
                  left: 30.0,
                  top: 30.0,
                  child: Container(
                      height: 30.0,
                      width: 40.0,
                      decoration: BoxDecoration(boxShadow: [
                        BoxShadow(
                            blurRadius: 7.0,
                            color: Colors.grey.withOpacity(0.75),
                            offset: Offset(5, 25),
                            spreadRadius: 12.0)
                      ]))),
              Positioned(
                  left: 12.0,
                  top: 15.0,
                  child: Hero(
                      tag: posts.pic,
                      child: Container(
                          height: 190.0,
                          width: 1000,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(7.0),
                              image: DecorationImage(
                                  image: AssetImage(posts.pic),
                                  fit: BoxFit.cover))))),
              Positioned(
                  left: 15.0,
                  top: 145,
                  child: Column(children: [
                    Text(posts.titleHome,
                        style: TextStyle(
                            fontWeight: FontWeight.w600,
                            color: Colors.white70,
                            height: 1.6,
                            fontSize: 24)),
                  ])),
              Positioned(
                  left: 15.0,
                  top: 179,
                  child: Column(children: [
                    Text(posts.subtitle,
                        style: TextStyle(
                            fontWeight: FontWeight.w400,
                            color: Colors.white,
                            height: 1.2,
                            fontSize: 20)),
                  ])),
              Positioned(
                  left: 740.0,
                  top: 210,
                  child: Column(children: [
                    Text(posts.data,
                        style: TextStyle(
                            fontWeight: FontWeight.w400,
                            height: 1.2,
                            fontSize: 14)),
                  ])),
              Positioned(
                  left: 15.0,
                  top: 210,
                  child: Column(children: [
                    Text(posts.author,
                        style: TextStyle(
                            fontWeight: FontWeight.w400,
                            height: 1.2,
                            fontSize: 14)),
                  ])),
            ]),
          )),
    );
  }
}

News page details

import 'package:flutter/material.dart';
import 'package:flutter_application_1/views/home/posts.dart';

class ArticlesDetail extends StatefulWidget {
  final ArticlesPosts selectedPosts;

  const ArticlesDetail({Key key, this.selectedPosts}) : super(key: key);

  @override
  _ArticlesDetailState createState() => _ArticlesDetailState();
}

class _ArticlesDetailState extends State<ArticlesDetail> {
  final posts = allPosts;

  @override
  Widget build(
    BuildContext context,
  ) {
    return Stack(
      children: [
        Container(
          padding: EdgeInsets.only(
            top: 25,
            bottom: 16,
            left: 16,
            right: 16,
          ),
          margin: EdgeInsets.only(top: 16),
          decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.rectangle,
              borderRadius: BorderRadius.circular(17),
              boxShadow: [
                BoxShadow(
                  color: Colors.black26,
                  blurRadius: 10,
                  offset: Offset(0, 10),
                )
              ]),
          child: Stack(children: [
            ListView(
              children: [
                SizedBox(height: 10),
                Positioned(
                  child: Container(
                    height: 90,
                    width: 300,
                    child: Text(widget.selectedPosts.title,
                        style: TextStyle(
                            fontSize: 26, fontWeight: FontWeight.w600)),
                  ),
                ),
                SizedBox(height: 10),
                Positioned(
                  child: Container(
                    height: 50,
                    width: 300,
                    child: Text(widget.selectedPosts.subtitle,
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.w300)),
                  ),
                ),
                SizedBox(height: 20),
                Positioned(
                  child: Container(
                    height: 50,
                    width: 300,
                    child: Text(
                        '_______________________________________________________________________________________________________________________',
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.w100,
                            color: Colors.black12)),
                  ),
                ),
                SizedBox(height: 20),
                Positioned(
                    child: Container(
                        height: 400,
                        width: 200,
                        decoration: BoxDecoration(
                            image: DecorationImage(
                                image: AssetImage(widget.selectedPosts.pic))))),
                SizedBox(height: 50),
                Positioned(
                  child: Container(
                    height: 150,
                    width: 300,
                    child: Text(widget.selectedPosts.notice,
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.w400)),
                  ),
                ),
                Align(
                    alignment: Alignment.bottomRight,
                    child: FlatButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text('Confirm')))
              ],
            )
          ]),
        )
      ],
    );
  }
}


In this sense, I would like to know if anyone has experienced this error or has any idea how to solve it from this implementation. Thank you in advance for assisting in the resolution !!

Natan Vaz
  • 21
  • 1
  • Possible duplicate of https://stackoverflow.com/questions/54905388/incorrect-use-of-parent-data-widget-expanded-widgets-must-be-placed-inside-flex – RukshanJS Apr 15 '21 at 17:44

1 Answers1

0

You can not use the Positioned-Widget inside a ListView. This only works in Stacks.

Jannik
  • 635
  • 1
  • 6
  • 14