This MainScreen widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
MainScreen
The widget which was currently being built when the offending call was made was:
_BottomNavigationItem)
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:third/Article.dart'; import 'package:third/carousel/carousel_slider.dart'; //import 'package:third/gen/assets.gen.dart'; import 'package:third/data.dart'; import 'package:dotted_border/dotted_border.dart'; import 'package:third/onBoarding.dart'; import 'package:third/profile.dart'; import 'package:third/splash.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { static const defaultFontFamily = 'Avenir'; const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { final primaryTextColo = Color(0xff0d253c); final secondaryTextColo = Color(0xff2d4379); final thirdcolor = Color(0xff6C63FF); return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. appBarTheme: AppBarTheme( backgroundColor: Colors.white, foregroundColor: primaryTextColo, elevation: 0, titleSpacing: 32, ), colorScheme: ColorScheme.light( primary: Colors.blue, onPrimary: Colors.white, onSurface: Colors.white, onBackground: Colors.grey, background: Color(0xfffbfcff)), textButtonTheme: TextButtonThemeData( style: ButtonStyle( textStyle: MaterialStateProperty.all(TextStyle( fontSize: 14, fontFamily: defaultFontFamily, fontWeight: FontWeight.w400)))), primarySwatch: Colors.blue, snackBarTheme: SnackBarThemeData(backgroundColor: Colors.orange.withOpacity(0.9)), textTheme: TextTheme( caption: TextStyle( fontFamily: defaultFontFamily, fontWeight: FontWeight.w400, color: Colors.blue[800], fontSize: 10), headline6: TextStyle( fontFamily: defaultFontFamily, fontWeight: FontWeight.bold, color: primaryTextColo), subtitle1: TextStyle( fontFamily: defaultFontFamily, color: secondaryTextColo, fontSize: 14), subtitle2: TextStyle( fontFamily: defaultFontFamily, color: Colors.grey[700], fontSize: 12), bodyText2: TextStyle( fontSize: 12, color: Colors.blue, fontFamily: defaultFontFamily), headline5: TextStyle( color: secondaryTextColo, fontFamily: defaultFontFamily, fontSize: 24)), ), // home: Stack(children: [ // Positioned.fill(child: const HomeScreen()), // Positioned(left: 0, right: 0, bottom: 0, child: _ButtomNavigation()), // ]), home: MainScreen(), ); } } class MainScreen extends StatefulWidget { @override State createState() => _MainScreenState(); } const int homeIndex = 0; const int articleIndex = 1; const int menuIndex = 2; const int searchIndex = 3; class _MainScreenState extends State { int selectedScreenIndex = homeIndex; @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: _ButtomNavigation(ontap: (int index){ setState(() { selectedScreenIndex=index;}); },), body: IndexedStack( index: selectedScreenIndex, children: [ HomeScreen(), ArticleScreen(), ProfileScreen(), SearchScreen(), ], ), ); } } class SearchScreen extends StatelessWidget{ @override Widget build(BuildContext context) { return Container(color: Colors.black,); } } class _ButtomNavigation extends StatelessWidget { final Function(int index) ontap; const _ButtomNavigation({Key? key,required this.ontap}) : super(key: key); @override Widget build(BuildContext context) { return Container( height: 95, child: Stack( children: [ Positioned( left: 0, bottom: 0, right: 0, child: Container( height: 65, decoration: const BoxDecoration( color: Colors.white, boxShadow: [BoxShadow(blurRadius: 10, color: Colors.grey)]), child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, // ignore: prefer_const_literals_to_create_immutables children: [ _BottomNavigationItem( iconFileName: 'Home.png', activeIconFileName: 'Home.png', title: 'Home', onTap: (){ ontap(homeIndex); },), _BottomNavigationItem( iconFileName: 'Articles.png', activeIconFileName: 'Articles.png', title: 'Articles', onTap: (){ ontap(articleIndex); }), _BottomNavigationItem( iconFileName: 'Menu.png', activeIconFileName: 'Menu.png', title: 'Menu', onTap: (){ ontap(menuIndex); }), const SizedBox( width: 8, ), _BottomNavigationItem( iconFileName: 'Search.png', activeIconFileName: 'Search.png', title: 'Search', onTap: (){ ontap(searchIndex); }), ], ), ), ), Center( child: Container( height: 85, width: 65, alignment: Alignment.topCenter, child: Container( height: 65, decoration: BoxDecoration( borderRadius: BorderRadius.circular(35), color: Colors.blue[800]), child: Image.asset('assets/img/icons/plus.png'), ), ), ) ], ), ); } } class _BottomNavigationItem extends StatelessWidget { final String iconFileName; final String activeIconFileName; final String title; final Function() onTap; const _BottomNavigationItem( {Key? key, required this.iconFileName, required this.activeIconFileName, required this.title, required this.onTap}) : super(key: key); @override Widget build(BuildContext context) { return InkWell( onTap: onTap(), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset('assets/img/icons/$iconFileName'), const SizedBox( height: 6, ), Text( title, style: Theme.of(context).textTheme.caption, ) ], ), ); } } class HomeScreen extends StatelessWidget { const HomeScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final ThemeData themeData = Theme.of(context); final stories = AppDatabase.stories; return Scaffold( body: SafeArea( child: SingleChildScrollView( physics: BouncingScrollPhysics(), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.fromLTRB(32, 12, 32, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'hi Turaj!', style: themeData.textTheme.subtitle1, ), Image.asset( 'assets/img/icons/notification.png', height: 24, width: 24, ) ], ), ), Padding( padding: const EdgeInsets.fromLTRB(32, 12, 0, 24), child: Text( 'Explore Todays', style: themeData.textTheme.headline6, ), ), _StoryList(stories: stories, themeData: themeData), const _CategoryList(), const _PostList(), ], ), ), ), ); } } class _CategoryList extends StatelessWidget { const _CategoryList({ Key? key, }) : super(key: key); @override Widget build(BuildContext context) { final categories = AppDatabase.categories; return CarouselSlider.builder( itemCount: categories.length, itemBuilder: (cotext, index, realIndex) { return _CategoryItem( left: realIndex == 0 ? 16 : 8, right: realIndex == categories.length - 1 ? 16 : 8, category: categories[realIndex], ); }, options: CarouselOptions( scrollDirection: Axis.horizontal, viewportFraction: .8, //height: 300, aspectRatio: 1.2, initialPage: 0, disableCenter: true, enableInfiniteScroll: false, enlargeCenterPage: true, enlargeStrategy: CenterPageEnlargeStrategy.height, scrollPhysics: BouncingScrollPhysics())); } } class _CategoryItem extends StatelessWidget { final Category category; final double left; final double right; const _CategoryItem({ Key? key, required this.category, required this.left, required this.right, }) : super(key: key); @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.fromLTRB(left, 0, right, 0), child: Stack( children: [ Positioned.fill( left: 60, top: 60, right: 60, bottom: 10, child: Container( decoration: BoxDecoration(boxShadow: [ BoxShadow(blurRadius: 20, color: Color(0xaa000000)) ]), )), Positioned.fill( left: left, right: right, child: Container( margin: EdgeInsets.fromLTRB(0, 0, 0, 8), child: ClipRRect( borderRadius: BorderRadius.circular(32), child: Image.asset( 'assets/img/posts/large/${category.imageFileName}', fit: BoxFit.cover, ), ), foregroundDecoration: BoxDecoration( borderRadius: BorderRadius.circular(32), gradient: const LinearGradient( begin: Alignment.bottomCenter, end: Alignment.center, colors: [Color(0xdd0d253c), Colors.transparent])), decoration: BoxDecoration( borderRadius: BorderRadius.circular(32), color: Colors.amber, ), ), ), Positioned( bottom: 45, left: 32, child: Text( category.title, style: Theme.of(context) .textTheme .headline5! .apply(color: Colors.white), ), ) ], ), ); } } class _StoryList extends StatelessWidget { const _StoryList({ Key? key, required this.stories, required this.themeData, }) : super(key: key); final List stories; final ThemeData themeData; @override Widget build(BuildContext context) { return SizedBox( width: MediaQuery.of(context).size.width, height: 110, child: ListView.builder( padding: EdgeInsets.fromLTRB(32, 0, 32, 0), itemCount: stories.length, scrollDirection: Axis.horizontal, itemBuilder: (context, index) { final story = stories[index]; return _Story(story: story, themeData: themeData); }), ); } } class _Story extends StatelessWidget { const _Story({ Key? key, required this.story, required this.themeData, }) : super(key: key); final StoryData story; final ThemeData themeData; @override Widget build(BuildContext context) { return Container( margin: const EdgeInsets.fromLTRB(4, 0, 4, 0), child: Column( children: [ Stack( children: [ story.isViewed ? _profileImageViewed(context) : _profileImageNormal(), Positioned( bottom: 0, right: 0, child: Image.asset('assets/img/icons/${story.iconFileName}'), height: 24, width: 24, ) ], ), const SizedBox( height: 8, ), Text( story.name, style: themeData.textTheme.bodyText2, ) ], ), ); } Widget _profileImageNormal() { return Container( width: 68, height: 68, decoration: BoxDecoration( borderRadius: BorderRadius.circular(24), gradient: const LinearGradient(begin: Alignment.topLeft, colors: [ Color(0xff376AED), Color(0xff49B0E2), Color(0xff9CECF8), ])), child: Container( margin: EdgeInsets.all(2), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(22), ), padding: EdgeInsets.all(5), child: _profileImage(), ), ); } Widget _profileImageViewed(BuildContext context) { return SizedBox( width: 68, height: 68, child: DottedBorder( borderType: BorderType.RRect, strokeWidth: 2, radius: Radius.circular(24), color: Theme.of(context).textTheme.bodyText2!.color!, dashPattern: [5, 3], padding: EdgeInsets.all(7), child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(24), ), child: _profileImage(), ), ), ); } Widget _profileImage() { return ClipRRect( borderRadius: BorderRadius.circular(17), child: Image.asset('assets/img/stories/${story.imageFileName}')); } } class _PostList extends StatelessWidget { const _PostList({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final posts = AppDatabase.posts; return Column( children: [ Padding( padding: const EdgeInsets.only(left: 32, right: 24), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Latest News', style: Theme.of(context) .textTheme .headline5! .apply(fontSizeFactor: .7, fontWeightDelta: 4), ), TextButton( onPressed: () {}, child: const Text('More'), ), ], ), ), ListView.builder( itemCount: posts.length, itemExtent: 140, shrinkWrap: true, physics: const ClampingScrollPhysics(), itemBuilder: (context, index) { final post = posts[index]; return _Post(post: post); }) ], ); } } class _Post extends StatelessWidget { const _Post({ Key? key, required this.post, }) : super(key: key); final PostData post; @override Widget build(BuildContext context) { return Container( margin: const EdgeInsets.fromLTRB(32, 8, 32, 8), decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), color: Colors.white, boxShadow: [ const BoxShadow(blurRadius: 10, color: Colors.blueGrey), ]), child: Row( children: [ ClipRRect( borderRadius: BorderRadius.circular(20), child: Image.asset( 'assets/img/posts/small/${post.imageFileName}')), SizedBox( width: 10, ), Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( post.caption, style: TextStyle( color: Colors.blue[800], fontFamily: MyApp.defaultFontFamily, fontWeight: FontWeight.w700), ), Text( post.title, style: Theme.of(context).textTheme.subtitle2, ), SizedBox(height: 30), Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( CupertinoIcons.hand_thumbsup, color: Colors.blue[800], size: 12, ), SizedBox(width: 2), Text( post.likes, ), SizedBox(width: 20), Icon( CupertinoIcons.time, color: Colors.blue[800], size: 12, ), SizedBox(width: 2), Text( post.time, // style: Theme.of(context).textTheme.bodyText2, ), Expanded( child: Container( alignment: Alignment.centerRight, margin: EdgeInsets.all(3), child: Icon( post.isBookmarked ? CupertinoIcons.bookmark_fill : CupertinoIcons.bookmark, color: Colors.blue[800], size: 12, ), ), ), ], ) ], ), ), ], )); } }