act as a flutter developer im getting a error saying this Usually, this means that the…
- User
act as a flutter developer im getting a error saying this Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. Typically, Positioned widgets are placed directly inside Stack widgets. The offending Positioned is currently placed inside a Row widget. in this code
class NewsScreen extends StatelessWidget { NewsScreen({Key? key}) : super(key: key); final NewsController _con = Get.put(NewsController());
Future _pullRefresh() async { return _con.getNewsList(true); }
@override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { if (_con.fromHome.isTrue) { Get.offAllNamed(AppRoutes.bottomBarScreen, arguments: [0]); } else { Get.back(); } return true; }, child: Scaffold( backgroundColor: AppColors.appBackGroundColor, key: _con.scaffoldkey, appBar: appBar(), drawer: DrawerScreen(), body: Obx( () => _con.isLoading.value || _con.isEmojiLoading.value ? ListView.builder( physics: BouncingScrollPhysics(), itemCount: 10, itemBuilder: (BuildContext context, int index) => AppShimmer( height: Get.height * 0.36, width: Get.width, color: AppColors.appColor.withOpacity(0.2), ), ) : _con.newsList.isEmpty ? Center( child: Text( "No stories to share yet", style: TextStyle( fontSize: 22, fontWeight: FontWeight.w500, ), ), ) : RefreshIndicator( backgroundColor: AppColors.appColor, color: Colors.white, onRefresh: () { return _pullRefresh(); }, child: Column( children: [ Expanded( child: ListView.builder( physics: BouncingScrollPhysics(), controller: _con.scrollController, itemCount: _con.newsList.length, itemBuilder: (BuildContext context, int index) => newsTile(index), ), ), Obx( () => _con.paginationLoading.value ? AppLoader(color: AppColors.appColor) : SizedBox(), ), ], ), ), ), ), ); }
Widget newsTile(int index) => Container( decoration: BoxDecoration(color: Colors.white), padding: EdgeInsets.symmetric(vertical: 15), margin: EdgeInsets.only(bottom: 10), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Padding( padding: EdgeInsets.symmetric(horizontal: Get.width * 0.03), child: Text( _con.newsList[index].title ?? '', maxLines: 2, style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black, ), ), ), SizedBox(height: 5), Padding( padding: EdgeInsets.symmetric(horizontal: Get.width * 0.03), child: Text( _con.formatter.format( DateTime.parse(_con.newsList[index].createdAt.toString())), style: TextStyle( fontSize: 12, fontWeight: FontWeight.w400, color: Colors.grey, ), ), ), SizedBox(height: 14), Padding( padding: EdgeInsets.symmetric(horizontal: Get.width * 0.03), child: ReadMoreText( _con.newsList[index].description ?? '', moreStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.w700), style: TextStyle( color: Colors.black, fontSize: 14, fontWeight: FontWeight.w400, ), trimLines: 2, colorClickableText: Colors.pink, trimMode: TrimMode.Line, trimCollapsedText: 'More', trimExpandedText: ' Show Less ', lessStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.w700), ), ), SizedBox(height: 10), GestureDetector( onTap: () { if (_con.newsList[index].newsFeedAttachments?[0].thumbnail != null) Get.toNamed(AppRoutes.videoScreen, arguments: _con .newsList[index].newsFeedAttachments![0].attachment); }, child: _con.newsList[index].newsFeedAttachments!.isEmpty ? SizedBox() : _con.newsList[index].newsFeedAttachments?[0].thumbnail == null ? CarouselSlider( items: List.generate( _con.newsList[index].newsFeedAttachments?.length ?? 0, (i) => imageTile( _con.newsList[index].newsFeedAttachments![i], index: index, ), ), options: CarouselOptions( viewportFraction: 1, initialPage: _con.imageIndex.value, height: Get.height * 0.25, onPageChanged: (i, _) { _con.imageIndex.value = i; }, ), ) : imageTile(_con.newsList[index].newsFeedAttachments![0]), ),
Row( mainAxisSize: MainAxisSize.max, children: [ Container( height: 60, width: Get.width, child: Expanded( child: Stack( children: [ Positioned( child: Container( color: AppColors.appColor, height: 30, width: Get.width, ), bottom: 0, ), Positioned( bottom: 6, child: InkWell( onTap: () { Get.toNamed(AppRoutes.likeListController, arguments: [ _con.newsList[index].newsLikesCount?.value, _con.newsList[index].id.toString(), ]); }, child: Row( children: [ SizedBox(width: 10,), Obx( () => Text( "${_con.newsList[index].newsLikesCount?.value}", style: TextStyle( fontWeight: FontWeight.w700, color: Colors.white, ), ), ), Text( " Reacted", style: TextStyle( fontWeight: FontWeight.w500, color: Colors.white, ), ), SizedBox(width: 10,), Obx( () => Text( "${_con.newsList[index].newsCommentsCount?.value}", style: TextStyle( fontWeight: FontWeight.w700, color: Colors.white, ), ), ), Text( " Comments", style: TextStyle( fontWeight: FontWeight.w500, // height: 8, color: Colors.white, ), ), ], ), ), ), Positioned( right: 3, child: Row( children: [ commentButton(index), SizedBox( width: 10, ), likesButton(_con.emojiList.elementAt(index).id, index), SizedBox( width: 10, ), ], ), ), ], ), ), ), ], ), d(), SizedBox(height: 5), // commentBox(index), ], ), );Widget imageTile(NewsFeedAttachment image, {int? index}) => Stack( alignment: image.thumbnail != null ? Alignment.center : Alignment.bottomCenter, children: [ Container( height: Get.height * 0.25, width: Get.width, margin: EdgeInsets.symmetric(horizontal: Get.width * 0.03), child: imageNetwork( image.thumbnail == null ? image.attachment! : image.thumbnail!, boxFit: BoxFit.contain, ), ), if (image.thumbnail != null) playIcon(), if (index != null && _con.newsList[index].newsFeedAttachments!.length > 1) ClipRRect( child: BackdropFilter( filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), child: Container( padding: EdgeInsets.all(4), decoration: BoxDecoration( color: Colors.black.withOpacity(0.2), borderRadius: BorderRadius.circular(50), ), margin: EdgeInsets.only(bottom: 8), child: Row( mainAxisSize: MainAxisSize.min, children: List.generate( _con.newsList[index].newsFeedAttachments!.length, (i) => Padding( padding: const EdgeInsets.symmetric(horizontal: 3), child: Obx( () => Container( decoration: BoxDecoration( border: Border.all(color: Colors.white, width: 2), shape: BoxShape.circle, ), child: CircleAvatar( radius: 3, backgroundColor: i == _con.imageIndex.value ? Colors.white : Colors.transparent, ), ), ), ), ), ), ), ), ) ], );
reactionIcon({ required String icon, required String value, }) => Reaction( icon: Obx( () => Image.network(icon, height: 35, width: 35, opacity: _con.selectedEmoji.value == value ? AlwaysStoppedAnimation(1) : AlwaysStoppedAnimation(0.5), errorBuilder: (context, url, error) => new Icon(Icons.error), loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) { if (loadingProgress == null) return child; return Center( child: CircularProgressIndicator( color: AppColors.appColor, value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / int.parse( loadingProgress.expectedTotalBytes.toString()) : null, ), ); }), ), value: value, );
ClipRRect playIcon() { return ClipRRect( borderRadius: BorderRadius.circular(50), child: BackdropFilter( filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), child: Container( decoration: BoxDecoration( shape: BoxShape.circle, border: Border.all(color: Colors.white.withOpacity(0.5), width: 5), ), height: 60, width: 60, child: ClipRRect( borderRadius: BorderRadius.circular(50), child: BackdropFilter( filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), child: Icon( Icons.play_arrow, color: Colors.white, size: 25, ), ), ), ), ), ); }
Container d() { return Container( width: Get.width, height: 1, color: Color(0xffB3B3B3).withOpacity(0.2), ); }
Widget commentBox(index) => Container( padding: EdgeInsets.symmetric(horizontal: Get.width * 0.03), child: GestureDetector( onTap: () { Get.toNamed( AppRoutes.newscommentsScreen, arguments: _con.newsList[index].id, ); }, child: Row( children: [ ImageIcon( AssetImage(AppImages.comment), size: 20, color: AppColors.appColor, ), SizedBox(width: 10), Text( "Type comment here...", style: TextStyle( fontSize: 15, fontWeight: FontWeight.w600, color: AppColors.appColor, ), ), Spacer(), IconButton( highlightColor: Colors.transparent, splashColor: Colors.transparent, constraints: BoxConstraints(), padding: EdgeInsets.zero, icon: ImageIcon( AssetImage(AppImages.share), ), onPressed: () { Share.share( "{_con.newsList[index].description}"); }, ) ], ), ), );
PreferredSizeWidget appBar() { return AppBar( shadowColor: Color(0xfff2f2f2), backgroundColor: Colors.white, centerTitle: false, toolbarHeight: 60, elevation: 1, leading: _con.fromHome.value ? IconButton( splashColor: Colors.transparent, highlightColor: Colors.transparent, constraints: BoxConstraints(), padding: const EdgeInsets.only(left: 5, bottom: 5), icon: Icon( Icons.keyboard_arrow_left, color: Colors.black, size: 40, ), onPressed: () { Get.offAllNamed(AppRoutes.bottomBarScreen, arguments: [0]); }) : GestureDetector( onTap: () { _con.scaffoldkey.currentState?.openDrawer(); }, child: Container( margin: EdgeInsets.symmetric(vertical: 10, horizontal: 6), padding: EdgeInsets.symmetric(vertical: 8), decoration: BoxDecoration( borderRadius: BorderRadius.circular(11), color: Color(0xffE3FAFA), ), width: 40, child: ImageIcon( AssetImage( AppImages.menuOpen, ), color: AppColors.appColor, ), ), ), title: Text( 'News', style: TextStyle( color: Colors.black, fontWeight: FontWeight.w700, fontSize: 24, ), ), actions: [ InkWell( onTap: () => Get.toNamed(AppRoutes.notificationListScreen), child: Container( margin: EdgeInsets.symmetric(vertical: 10, horizontal: 6), padding: EdgeInsets.symmetric(vertical: 8), decoration: BoxDecoration( borderRadius: BorderRadius.circular(11), color: AppColors.appColor, ), width: 40, child: ImageIcon(AssetImage(AppImages.notification)), ), ), GestureDetector( onTap: () async { Get.toNamed(AppRoutes.profileScreen); }, child: Padding( padding: EdgeInsets.symmetric(vertical: 10, horizontal: 6), child: ClipRRect( borderRadius: BorderRadius.circular(100), child: Obx( () => LocalStorage.userProfile.isNotEmpty ? Image.network(LocalStorage.userProfile.value, width: 40, height: 40, fit: BoxFit.contain, errorBuilder: (context, url, error) => new Icon(Icons.error), loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) { if (loadingProgress == null) return child; return Center( child: CircularProgressIndicator( color: AppColors.appColor, value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / int.parse(loadingProgress .expectedTotalBytes .toString()) : null, ), ); }, ) : Image.asset( AppImages.userProfile, width: 40, height: 40, fit: BoxFit.contain, ), ), ), ), )
], systemOverlayStyle: SystemUiOverlayStyle.dark, );}
Widget likesButton(var value, var index) { return Positioned( bottom: 0, right: 0, child: Obx(() { return GestureDetector( onTap: () { print('valueeee $value '); if (_con.newsList[index].newsLikes!.isNotEmpty) { _con.deleteFavouriteApi( _con.newsList[index].newsLikes![0].id, index, ); } else { _con.addToFavouriteApi( _con.newsList[index].id, _con.emojiList[0].id, index); } }, child: Container( margin: EdgeInsets.only(bottom: 2, right: 0), padding: EdgeInsets.symmetric(vertical: 6), decoration: BoxDecoration( shape: BoxShape.circle, color: !_con.newsList[index].newsLikes!.isEmpty ? AppColors.appColor : Colors.white, border: Border.all( color: AppColors.appColor, width: 4.0, ), ), width: 60, height: 60, child: ImageIcon( AssetImage(AppImages.gheart), color: !_con.newsList[index].newsLikes!.isEmpty ? Colors.white : AppColors.appColor, ), ), ); }), ); }
///comment button Widget commentButton(index) { return GestureDetector( onTap: () => Get.toNamed( AppRoutes.newscommentsScreen, arguments: _con.newsList[index].id, ), // add route child: Container( margin: EdgeInsets.only(bottom: 2, right: 0), padding: EdgeInsets.symmetric(vertical: 6), decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.white, border: Border.all( color: AppColors.appColor, width: 4.0, ), ), width: 60, height: 60, child: ImageIcon(AssetImage(AppImages.ncomment), color: AppColors.appColor), ), ); }
Widget imageNetwork(String url, {Color? color, BoxFit? boxFit}) { return Image.network(url, fit: boxFit ?? BoxFit.contain, errorBuilder: (context, url, error) => new Icon(Icons.error), loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) { if (loadingProgress == null) return child; return Center( child: CircularProgressIndicator( color: color ?? AppColors.appColor, value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / int.parse(loadingProgress.expectedTotalBytes.toString()) : null, ), ); }); } }
- Assistant
The error you are facing is because a
Positionedwidget is used inside aRowwidget, which is not allowed. APositionedwidget should be a direct child of aStack.In your code, you have the following block that is causing the error:
Row( mainAxisSize: MainAxisSize.max, children: [ Container( height: 60, width: Get.width, child: Expanded( child: Stack( children: [ ... ], ), ), ), ], ),To fix this, you can remove the
Rowwidget and directly use theContaineras it's already taking the full width. So, the updated code should look like this:Container( height: 60, width: Get.width, child: Stack( children: [ ... ], ), ),Replace the mentioned block with this updated code, and your error should be fixed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.