Flutter - RangeError (index) - flutter

I am getting "RangeError (index): Invalid value: Not in inclusive range 0..4: 5" error in my flutter app.
Error
This is my code and I hope somebody is able to help me fixing the error:
import 'package:fluttertravelapp/models/beach_model.dart';
import 'package:fluttertravelapp/models/popular_model.dart';
import 'package:fluttertravelapp/screens/selected_place_screen.dart';
import 'package:fluttertravelapp/widgets/bottom_navigation_bar.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertravelapp/models/recommended_model.dart';
import 'package:fluttertravelapp/widgets/custom_tab_indicator.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:cached_network_image/cached_network_image.dart';
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// Page Controller
final _pageController = PageController(viewportFraction: 0.877);
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBarTravel(),
body: SafeArea(
child: Container(
child: ListView(
physics: BouncingScrollPhysics(),
// Custom Navigation Drawer and Search Button
children: [
Container(
height: 57.6,
margin: EdgeInsets.only(top: 28.8, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 57.6,
width: 57.6,
padding: EdgeInsets.all(18),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(0x080a0928),
),
child: SvgPicture.asset('assets/svg/icon_drawer.svg'),
),
Container(
height: 57.6,
width: 57.6,
padding: EdgeInsets.all(18),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(0x080a0928),
),
child: SvgPicture.asset('assets/svg/icon_search.svg'),
),
],
),
),
// Text Widget for Title
Padding(
padding: EdgeInsets.only(top: 48, left: 28.8),
child: Text('Explore\nthe Nature', style: GoogleFonts.playfairDisplay(
fontSize: 45.6,
fontWeight: FontWeight.w700,
),),
),
//Custom Tab bar with Custom Indicator
Container(
height: 30,
margin: EdgeInsets.only(left: 14.4, top: 28.8),
child: DefaultTabController(
length: 4,
child: TabBar(labelPadding: EdgeInsets.only(left: 14.4, right: 14.4),
indicatorPadding: EdgeInsets.only(left: 14.4, right: 14.4),
isScrollable: true,
labelColor: Color(0xFF000000),
unselectedLabelColor: Color(0xFF8a8a8a),
labelStyle: GoogleFonts.lato(
fontSize: 14,
fontWeight: FontWeight.w700,
),
unselectedLabelStyle: GoogleFonts.lato(
fontSize: 14,
fontWeight: FontWeight.w700,
),
indicator: RoundedRectangleTabIndicator(
color: Color(0xFF000000), weight: 2.4, width: 14.4
),
tabs: [
Tab(
child: Container(
child: Text('Recommended'),
),
),
Tab(
child: Container(
child: Text('Popular'),
),
),
Tab(
child: Container(
child: Text('New Destination'),
),
),
Tab(
child: Container(
child: Text('Hidden Gems'),
),
),
],),),
),
//ListView Widget with PageView
//Recommendations section
Container(
height: 218.4,
margin: EdgeInsets.only(top: 16,),
child: PageView(
physics: BouncingScrollPhysics(),
controller: _pageController,
scrollDirection: Axis.horizontal,
children: List.generate(
recommendations.length,
(int index) => GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SelectedPlaceScreen(
recommendedModel: recommendations[index])));
},
child: Container(
margin: EdgeInsets.only(right: 28.8),
width: 333.6,
height: 218.4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: CachedNetworkImageProvider(recommendations[index].image),
),
),
child: Stack(
children: [
Positioned(
bottom: 19.2,
left: 19.2,
child: ClipRRect(
borderRadius: BorderRadius.circular(4.8),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaY: 19.2, sigmaX: 19.2),
child: Container(
height: 36,
padding: EdgeInsets.only(
left: 16.72, right: 14.4),
alignment: Alignment.centerLeft,
child: Row(
children: [
SvgPicture.asset('assets/svg/icon_location.svg'),
SizedBox(width: 9.52,),
Text(recommendations[index].name,
style: GoogleFonts.lato(
fontWeight: FontWeight.w700,
color: Colors.white,
fontSize: 16.8,),
),
],
),
),
),
),
)
],
),
),
)),
),
),
//Dots Indicator
//Using SmoothPageIndicator Library
Padding(padding: EdgeInsets.only(left: 28.8, top: 28.8),
child: SmoothPageIndicator(
controller: _pageController,
count: recommendations.length,
effect: ExpandingDotsEffect(
activeDotColor: Color(0xFF8a8a8a),
dotColor: Color(0xFFabababab),
dotHeight: 4.8,
dotWidth: 6,
spacing: 4.8,
),
),
),
//Text Widget for Popular Categories
Padding(
padding: EdgeInsets.only(top: 48, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Popular Categories',
style: GoogleFonts.playfairDisplay(
fontSize: 28,
fontWeight: FontWeight.w700,
color: Color(0xFF000000),
),
),
Text(
'Show All',
style: GoogleFonts.lato(
fontSize: 16.8,
fontWeight: FontWeight.w500,
color: Color(0xFF8a8a8a),
),
),
],
),
),
//ListView for Popular Categories Section
Container(
margin: EdgeInsets.only(top: 33.6,),
height: 45.6,
child: ListView.builder(
itemCount: populars.length,
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.only(left: 28.8, right: 9.6),
itemBuilder: (context, index){
return Container(
margin: EdgeInsets.only(right: 19.2),
height: 45.6,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(populars[index].color),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: 19.2,),
Image.asset(populars[index].image,height: 16.8,),
SizedBox(width: 19.2,),
],
),
);
},
),
),
//ListView for Beach Section
Container(
margin: EdgeInsets.only(top: 28.8, bottom: 16.8),
height: 124.8,
child: ListView.builder(
itemCount: beaches.length,
padding: EdgeInsets.only(left: 28.8, right: 12),
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index){
return Container(
height: 124,
width: 188.4,
margin: EdgeInsets.only(right: 16.8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: CachedNetworkImageProvider(beaches[index].image),
)
),
);
},
),
)
],
),
),
),
);
}
}
If you want the full code you can see the code from https://github.com/abdulazizahwan/flutter_travel_app
I tried the same code.

Related

How can i make a horizontal scrolling list View builder with dynamic height?

//ListView Builder
Container(
height: 230,
margin: const EdgeInsets.only(bottom: 30),
child: ListView.separated(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: controller.homeData!.bestSaleWebinars.length,
itemBuilder: (context, index) => InkWell(
onTap: () {
Get.toNamed(RoutesName.allCoursesPageDetails);
allCoursesController.getAllCourseDetails(
controller.homeData!.bestSaleWebinars[index].slug);
},
child: newCoursesBuilder(
controller.homeData!.bestSaleWebinars[index])),
separatorBuilder: (context, _) => const SizedBox(width: 15),
),
//widget
Widget newCoursesBuilder(var coursesCategories) {
return SizedBox(
height: 350,
width: 200,
child: Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 100,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey)),
child: CachedNetworkImage(
imageUrl: "https://livedivine.in${coursesCategories.thumbnail}",
placeholder: (context, url) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
Images.logo,
),
fit: BoxFit.fill),
),
),
errorWidget: (context, url, error) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
Images.logo,
),
fit: BoxFit.fill),
),
),
),
),
Container(
padding: const EdgeInsets.only(left: 11, top: 8),
child: Text(
coursesCategories.title,
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 15,
fontWeight: FontWeight.w900),
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const Icon(
Icons.person,
size: 16,
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
coursesCategories.teacher.fullName,
style: const TextStyle(
color: Colors.black,
fontSize: 12,
fontWeight: FontWeight.w500),
),
)
],
),
Row(
children: [
const Icon(
Icons.calendar_month,
size: 16,
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
"${coursesCategories.duration}",
style: const TextStyle(
color: Colors.black,
fontSize: 12,
fontWeight: FontWeight.w500),
),
)
],
)
],
),
),
const SizedBox(
height: 2,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
coursesCategories.price.toString(),
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 20,
fontWeight: FontWeight.w800),
),
Text(
"${coursesCategories.status}"
.replaceFirst("Status.", "")
.toLowerCase(),
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 19,
fontWeight: FontWeight.w800),
)
],
),
)
],
),
),
);
}
You can wrap the List view builder in an IntrinsicHeight widget, so the height of the ListView builder will be dynamic
You can try using SingleChildScrollView
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(
44,
(index) => Padding(
padding: EdgeInsets.only(right: 10),
child: Container(
...
),
),
)),
)

How can i make a stack nested in a scaffold scrollable

sorry if the image is too fat, I don't know how to format image in stack, so, this is what I wanna achieve, and I've done this with the following code, but I get an error and my screen goes white(showing the scaffold, refusing to render my widgets coz of the errors), I know in some way I have to constrain some widget somewhere, but I don't know which to give a height, since I need my entire screen to be scrollable, I've even tried to not make entire screen scrollable or give the children of the lower container some fixed height like 900, but still, not working, how can I achieve this,
This is my code so far
#override
Widget build(BuildContext context) {
_animationController.forward();
return Consumer<ProductsModel>(
builder: (_, pModel, __) => Scaffold(
body: SingleChildScrollView(
child: Stack(
children: [
Positioned(
left: 0,
right: 0,
child: Container(
width: double.maxFinite,
height: Dimensions.height395,
color: const Color(0xffF8F9FA),
child: Stack(
children: [
PageView.builder(
itemCount: widget.product.productImages!.length,
controller: _pageController,
onPageChanged: (value) {
_currentPage = value;
_animationController.forward();
// setState(() {});
},
itemBuilder: (context, index) {
return Center(
child: Container(
margin: EdgeInsets.only(top: Dimensions.height50),
width: Dimensions.productDetailContainerWidth,
height: Dimensions.productDetailContainerHeight,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
widget.product.productImages![index],
),
),
),
),
);
},
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: Dimensions.height20),
child: DotsIndicator(
dotsCount: widget.product.productImages!.length,
position: _currentPageValue,
decorator: DotsDecorator(
activeColor: kMainColour,
size: const Size.square(9.0),
activeSize: const Size(18.0, 9.0),
activeShape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(Dimensions.radius5)),
),
),
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: EdgeInsets.only(
bottom: Dimensions.height20,
right: Dimensions.height20),
child: GestureDetector(
onTap: () {
pModel.toggleSave(context, widget.product);
},
child: Container(
height: Dimensions.height32,
width: Dimensions.height32,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
border: Border.all(
color: const Color(0xffDADADA),
),
),
child: Icon(
widget.product.isSaved
? IconlyBold.heart
: IconlyBroken.heart,
color: kMainColour,
),
),
),
),
),
],
),
),
),
// todo: nav row
Positioned(
top: Dimensions.height45,
right: Dimensions.height20,
left: Dimensions.height20,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
BackNavButton(
onTap: () {
Navigator.pop(context);
},
),
Text(
widget.product.productSeller!,
style: TextStyle(
fontFamily: kFontFamily,
fontWeight: FontWeight.w600,
fontSize: Dimensions.font14,
),
),
SvgPicture.asset("assets/icons/chat_icon.svg")
],
),
),
// todo: review and details
Positioned(
left: 0,
right: 0,
top: Dimensions.height395 - Dimensions.height10,
child: Container(
height: 900,
padding: EdgeInsets.only(left: Dimensions.height20, right: Dimensions.height20, top: Dimensions.height20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Dimensions.radius20),
color: Colors.white,
),
child: Row(
children: [
Column(
children: [
Text(
widget.product.productName!,
style: TextStyle(
fontFamily: kFontFamily,
fontWeight: FontWeight.w500,
fontSize: Dimensions.font18,
),
),
Visibility(
visible: widget.product.discount != null,
child: Text(
"(${widget.product.discount}% Discount)",
style: TextStyle(
fontFamily: kFontFamily,
fontWeight: FontWeight.w500,
fontSize: Dimensions.font13,
color: const Color(0xff9098B1),
),
),
),
const FilterRating(
stars: 4,
),
],
),
Column(
children: [
],
),
],
),
),
),
],
),
),
),
);
}

Why I'm not able to get the updated variable even using setstate inside the stateful widget. As I want to update my Container on new TabBar option

Basically I want to change content on selecting the TabBar options. Here's my code. Also I'm new to flutter. I've also created a _incrementCounter() and getter setter methods. By using _incrementCounter() I'm returning an integer in order to update the PAgeView of my screen which is implemented inside a List called tabsCat. But on calling the _incrementCounter() method on line 264 it does not give the updated value please guide.
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertravelapp/models/beach_model.dart';
import 'package:fluttertravelapp/models/new_category_model.dart';
import 'package:fluttertravelapp/models/popular_model.dart';
import 'package:fluttertravelapp/models/recommended_model.dart';
import 'package:fluttertravelapp/screens/selected_category_screen.dart';
import 'package:fluttertravelapp/screens/selected_place_screen.dart';
import 'package:fluttertravelapp/widgets/bottom_navigation_bar.dart';
import 'package:fluttertravelapp/widgets/custom_tab_indicator.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int count = 0;
// Creating the getter method
// to get input from Field/Property
int get getCount {
return count;
}
// Creating the setter method
// to set the input in Field/Property
set setCount(int newCount) {
count = newCount;
}
int _incrementCounter() {
int index = 0;
setState(() {
index = getCount;
print(index);
});
return index;
}
/// Page Controller
final _pageController = PageController(viewportFraction: 0.877);
#override
Widget build(BuildContext context) {
int tabIndex = 0;
List tabsCat = [
PageView(
physics: BouncingScrollPhysics(),
controller: _pageController,
scrollDirection: Axis.horizontal,
children: List.generate(
recommendations.length,
(int index) => GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SelectedCategoryScreen(
newCategoryModel: newCategories[index])));
},
child: Container(
margin: EdgeInsets.only(right: 28.8),
width: 333.6,
height: 218.4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(newCategories[index].image),
),
),
child: Stack(
children: <Widget>[
Positioned(
bottom: 19.2,
left: 19.2,
child: ClipRRect(
borderRadius: BorderRadius.circular(4.8),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaY: 19.2, sigmaX: 19.2),
child: Container(
height: 36,
padding: EdgeInsets.only(
left: 16.72, right: 14.4),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
SvgPicture.asset(
'assets/svg/icon_location.svg'),
SizedBox(
width: 9.52,
),
Text(
recommendations[index].name,
style: GoogleFonts.lato(
fontWeight: FontWeight.w700,
color: Colors.white,
fontSize: 16.8),
)
],
),
),
),
),
)
],
),
),
),
),
),
PageView(
physics: BouncingScrollPhysics(),
controller: _pageController,
scrollDirection: Axis.horizontal,
children: List.generate(
recommendations.length,
(int index) => GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SelectedCategoryScreen(
newCategoryModel: newCategories[index])));
},
child: Container(
margin: EdgeInsets.only(right: 28.8),
width: 333.6,
height: 218.4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: CachedNetworkImageProvider(recommendations[index].image),
),
),
child: Stack(
children: <Widget>[
Positioned(
bottom: 19.2,
left: 19.2,
child: ClipRRect(
borderRadius: BorderRadius.circular(4.8),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaY: 19.2, sigmaX: 19.2),
child: Container(
height: 36,
padding: EdgeInsets.only(
left: 16.72, right: 14.4),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
SvgPicture.asset(
'assets/svg/icon_location.svg'),
SizedBox(
width: 9.52,
),
Text(
recommendations[index].name,
style: GoogleFonts.lato(
fontWeight: FontWeight.w700,
color: Colors.white,
fontSize: 16.8),
)
],
),
),
),
),
)
],
),
),
),
),
),
];
return Scaffold(
//bottomNavigationBar: BottomNavigationBarTravel(),
body: SafeArea(
child: Container(
child: ListView(
physics: BouncingScrollPhysics(),
children: <Widget>[
/// Custom Navigation Drawer and Search Button
Container(
height: 57.6,
margin: EdgeInsets.only(top: 28.8, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
),
/// Text Widget for Title
Padding(
padding: EdgeInsets.only(top: 0, left: 28.8),
child: Text(
'Explore\nPakistan',
style: GoogleFonts.playfairDisplay(
fontSize: 45.6, fontWeight: FontWeight.w700),
),
),
/// Custom Tab bar with Custom Indicator
Column(
children:
[DefaultTabController(
length: 4,
child: TabBar(
labelPadding: EdgeInsets.only(left: 14.4, right: 14.4),
indicatorPadding:
EdgeInsets.only(left: 14.4, right: 14.4),
isScrollable: true,
labelColor: Color(0xFF000000),
unselectedLabelColor: Color(0xFF8a8a8a),
labelStyle: GoogleFonts.lato(
fontSize: 14, fontWeight: FontWeight.w700),
unselectedLabelStyle: GoogleFonts.lato(
fontSize: 14, fontWeight: FontWeight.w700),
indicator: RoundedRectangleTabIndicator(
color: Color(0xFF000000), weight: 2.4, width: 14.4),
onTap: (index){
setCount = index;
//print(getCount);
},
tabs: [
Tab(
child: Container(
child: Text('Recommended'),
),
),
Tab(
child: Container(
child: Text('Popular'),
),
),
Tab(
child: Container(
child: Text('New Destination'),
),
),
Tab(
child: Container(
child: Text('Hidden Gems'),
),
)
]),
),
/// ListView widget with PageView
/// Recommendations Section
Container(
height: 218.4,
margin: EdgeInsets.only(top: 16),
child: tabsCat[_incrementCounter()],
),
]),
/// Dots Indicator
/// Using SmoothPageIndicator Library
Padding(
padding: EdgeInsets.only(left: 28.8, top: 28.8),
child: SmoothPageIndicator(
controller: _pageController,
count: recommendations.length,
effect: ExpandingDotsEffect(
activeDotColor: Color(0xFF8a8a8a),
dotColor: Color(0xFFababab),
dotHeight: 4.8,
dotWidth: 6,
spacing: 4.8),
),
),
/// Text Widget for Popular Categories
Padding(
padding: EdgeInsets.only(top: 48, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width*0.4,
child: FittedBox(
child: Text(
'Popular Categories',
style: GoogleFonts.playfairDisplay(
fontWeight: FontWeight.w700,
color: Color(0xFF000000),
),
),
),
),
Container(width: MediaQuery.of(context).size.width*0.1,
child: FittedBox(
child: Text(
'Show All ',
style: GoogleFonts.lato(
fontWeight: FontWeight.w500,
color: Color(0xFF8a8a8a),
),
),
),
)
],
),
),
/// ListView for Popular Categories Section
Container(
margin: EdgeInsets.only(top: 33.6),
height: 45.6,
child: ListView.builder(
itemCount: populars.length,
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.only(left: 28.8, right: 9.6),
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.only(right: 19.2),
height: 45.6,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(populars[index].color),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 19.2,
),
Image.asset(
populars[index].image,
height: 16.8,
),
SizedBox(
width: 19.2,
)
],
),
);
},
),
),
/// ListView for Beach Section
Container(
margin: EdgeInsets.only(top: 28.8, bottom: 16.8),
height: 124.8,
child: ListView.builder(
itemCount: beaches.length,
padding: EdgeInsets.only(left: 28.8, right: 12),
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
return Container(
height: 124.8,
width: 188.4,
margin: EdgeInsets.only(right: 16.8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image:
CachedNetworkImageProvider(beaches[index].image),
),
),
);
},
),
)
],
),
),
),
);
}
}
Solve the error by calling the _incrementcounter method onto OnTap section. Here's my code changes:
Edited incrementcounter method and declare new method setNewCount,
void setNewCount(int newCount) {
count = newCount;
}
void _incrementCounter() {
setState(() {
count = getCount;
});
}
2nd change :
onTap: (index){
setNewCount(index);
_incrementCounter();
},
3rd change:
Container(
height: 218.4,
margin: EdgeInsets.only(top: 16),
child: tabsCat[count],
),
Try
onTap: (index){
setCount(index);
//print(getCount);
},
instead of
onTap: (index){
setCount = index;
//print(getCount);
},

How to make a scrollable screen in flutter?

As you can see, Actually I want to make the screen scrollable, In my Flutter project, on one screen I have used some rows, columns, and listview but when I scroll my screen this is not happening, while I have to take SingleChildScrollView but it didn't work. and I have tried replacing the column with Listview but it didn't work. I don't understand where is the problem, please give me a solution. and I have attached my code and share a screenshot of the app screen below.
it shows pixel error.
.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:movie_app/core/constants/constants.dart';
import 'package:movie_app/ui/views/movielist/movie_detail_view.dart';
class MovieListView extends StatefulWidget {
const MovieListView({Key? key}) : super(key: key);
#override
_MovieListViewState createState() => _MovieListViewState();
}
class _MovieListViewState extends State<MovieListView> {
int counter = 0;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xff070d2d),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Container(
child: Column(
children: [
_buildHeaderSection(),
_buildSearchBar(),
SizedBox(height: 30),
buildCategoriesSection(),
SizedBox(height: 20),
buildListCategories(),
SizedBox(height: 30),
_buildPopularTitle(),
SizedBox(height: 30),
_buildPopularSection(),
],
),
),
),
),
],
),
),
);
}
Widget _buildHeaderSection() {
return Container(
padding: EdgeInsets.fromLTRB(30.0, 80.0, 30.0, 60.0),
// color: Colors.red,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MovieDetailView()),
);
},
child: Text(
"Hi, Edwards!",
style: TextStyle(
fontSize: 25,
//fontWeight: FontWeight.bold,
color: Colors.white),
),
),
Stack(
children: [
new IconButton(
icon: Icon(Icons.notifications),
onPressed: () {
setState(() {
//counter = 0;
});
}),
counter != 0
? new Positioned(
left: 20,
top: 20,
child: new Container(
padding: EdgeInsets.all(2),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 14,
minHeight: 14,
),
child: Text(
'$counter',
style: TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
)
: new Container(),
ClipRRect(
borderRadius: BorderRadius.circular(50.0),
child: Image.network(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ9CbZTM7W7VmNIGF36hIjpJcVoWEhbPGEGSw&usqp=CAU",
height: 50.0,
width: 50.0,
),
),
],
)
],
),
);
}
Widget _buildSearchBar() {
return Container(
height: 50,
margin: EdgeInsets.only(left: 30, right: 30),
child: TextField(
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: Colors.white,
),
hintText: "search your movie",
hintStyle: TextStyle(color: Colors.white),
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Colors.white,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Colors.grey,
width: 2.0,
),
),
)),
);
}
Widget buildCategoriesSection() {
return Container(
padding: EdgeInsets.only(left: 20, right: 20),
child: Row(
children: [
Text(
"Categories",
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
SizedBox(
width: 150,
),
Text(
"See more",
style: TextStyle(
color: Colors.grey,
fontSize: 15,
),
)
],
),
);
}
Widget buildListCategories() {
return Container(
height: 80,
padding: EdgeInsets.only(left: 20),
width: MediaQuery.of(context).size.width,
child: Container(
child: ListView.builder(
itemCount: categoryList.length,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context, position) {
return _buildCategoryItem(categoryList[position]);
},
),
),
);
}
Widget _buildCategoryItem(String category) {
return Container(
height: 80,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.red.withOpacity(0.3),
),
margin: EdgeInsets.only(right: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.adb, color: Colors.white,),
SizedBox(height: 8),
Text(
category,
style: TextStyle(color: Colors.white),
),
],
),
);
}
_buildPopularTitle() {
return Container(
padding: EdgeInsets.only(left: 20, right: 20),
child: Row(
children: [
Text(
"Popular",
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
SizedBox(
width: 150,
),
Text(
"See more",
style: TextStyle(
color: Colors.grey,
fontSize: 15,
),
)
],
),
);
}
_buildPopularSection() {
return Container(
height: 200,
padding: EdgeInsets.only(left: 20),
width: MediaQuery.of(context).size.width,
child: Container(
child: ListView.builder(
itemCount: 6,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context, position) {
return _buildPopularItem();
},
),
),
);
}
_buildPopularItem() {
return Column(
children: [
Container(
height: 200,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.red,
),
margin: EdgeInsets.only(right: 16),
child: Image.network(
'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/movie-poster-template-design-21a1c803fe4ff4b858de24f5c91ec57f_screen.jpg?ts=1574144362',
fit: BoxFit.cover,
),
),
SizedBox(height: 5,),
Container(
color: Colors.green,
child: Text("data",style: TextStyle(
color: Colors.red
),
),
),
],
);
}
}
As you have mentioned SingleChildScrollView should work, but your problem is that you don't have enough items to be scrolled according to your screenshot.
So please use enough items and try again.
And in your _buildPopularItem() widget, decrease the container width, that's why it says pixel overflow.
This overflow is made from _buildPopularTitle() while you set fixed height from _buildPopularSection having height: 200,
if you look closely, inside _buildPopularItem
There is a Container with height: 200, and it is taking full height, so for rest of children SizedBox h:5 and Container Text aren't having any spaces here, and creating overflow.
Solutions
increase the height of _buildPopularSection
reduce the container height inside _buildPopularItem
wrap with FittedBox(child: _buildPopularItem());
From the screenshot I can see that your container is smaller by 21 pixels in height. Please increase it.
_buildPopularSection() {
return Container(
height: 230,
padding: EdgeInsets.only(left: 20),
width: MediaQuery.of(context).size.width,
child: Container(
child: ListView.builder(
itemCount: 6,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context, position) {
return _buildPopularItem();
},
),
),
);
}
_buildPopularItem() {
return Column(
children: [
Container(
height: 225,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.red,
),
margin: EdgeInsets.only(right: 16),
child: Image.network(
'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/movie-poster-template-design-21a1c803fe4ff4b858de24f5c91ec57f_screen.jpg?ts=1574144362',
fit: BoxFit.cover,
),
),
SizedBox(height: 5,),
Container(
color: Colors.green,
child: Text("data",style: TextStyle(
color: Colors.red
),
),
),
],
);
}
Replace the above code and it should work...

ListView Implementation

Please help me or give me an idea on how to implement the above image. I'm planning to use ListView. Below is my sample code but this is on horizontal scroll.
Widget build(BuildContext context) {
return Container(
height: 210,
color: Theme.of(context).primaryColor,
padding: EdgeInsets.symmetric(vertical: 10),
child: ListView.builder(
itemCount: _foodsList.featuredList.length,
itemBuilder: (context, index) {
double _marginLeft = 0;
(index == 0) ? _marginLeft = 20 : _marginLeft = 0;
return FoodsCarouselItemWidget(
heroTag: 'home_food_carousel',
marginLeft: _marginLeft,
food: _foodsList.featuredList.elementAt(index),
);
},
scrollDirection: Axis.horizontal,
));
}
}
You need to build your layout in itemBuilder property of ListView. If you want to place the text on the image the best layout widget is Stack.
See the following in order to get the layout system of flutter: https://flutter.io/tutorials/layout/
and for all the available layout widgets see: https://flutter.io/widgets/layout/
and Stack: https://docs.flutter.io/flutter/widgets/Stack-class.html
Something like this:new Stack(
children: <Widget>[
new Positioned.fill(
child: new FadeInImage(
placeholder: new AssetImage('placeholder.png'),
image: new CachedNetworkImageProvider(photos[int].url),
fit: BoxFit.contain,
alignment: Alignment.center,
fadeInDuration: new Duration(milliseconds: 200),
fadeInCurve: Curves.linear,
),
),
new Positioned(
top: 10.0,
left: 10.0,
child: new Container(
child: new Text(photos[int].title)
),
),
],
)
I have tried my best to achieve the same, but due to time issues, you might find a little different.
Package used: https://pub.dev/packages/cached_network_image#-installing-tab-
Here is the complete code.
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
#override
_Demo createState() => _Demo();
}
class _Demo extends State<Demo> {
Widget foodsCarouselItemWidget() {
return Column(children: <Widget>[
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 15.0,
)
],
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: <
Widget>[
Container(
height: 130,
width: 130,
child: Stack(
children: <Widget>[
CachedNetworkImage(
width: 150,
height: 130,
imageUrl:
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQt46Q_GYs7kSq21kdBlNcD9pZzHVtl8nwNkY5f9GaR3rTbPOMf",
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider, fit: BoxFit.cover),
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
),
),
Positioned(
right: 10,
bottom: 5,
child: Row(children: <Widget>[
Icon(Icons.playlist_add_check,
color: Colors.white, size: 18),
SizedBox(width: 3),
Text("190",
style: TextStyle(color: Colors.white, fontSize: 12))
])),
],
),
),
SizedBox(width: 15),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Food title here",
style: TextStyle(
color: Color(0xff454545),
fontSize: 18,
fontWeight: FontWeight.bold)),
SizedBox(height: 10),
Row(children: <Widget>[
Icon(Icons.location_on, color: Color(0xff777777), size: 18),
SizedBox(width: 5),
Expanded(
child: Text("Add your location here",
style: TextStyle(
fontSize: 13, color: Color(0xff777777))))
]),
SizedBox(height: 5),
Row(children: <Widget>[
Icon(Icons.local_offer, color: Color(0xff777777), size: 18),
SizedBox(width: 5),
Expanded(
child: Text("Spicy, Delicious",
style: TextStyle(
fontSize: 13, color: Color(0xff777777))))
]),
SizedBox(height: 15),
Row(children: <Widget>[
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(
color: Colors.red[900],
borderRadius:
BorderRadius.all(Radius.circular(5.0))),
padding: EdgeInsets.all(10),
child: Text(
"Button Text",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
)),
SizedBox(width: 7),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.red[900]),
borderRadius: BorderRadius.all(Radius.circular(5.0))),
padding: EdgeInsets.all(5),
child:
Icon(Icons.bookmark_border, color: Colors.red[900]),
)
])
]))
])),
SizedBox(height: 10)
]);
}
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xfff3f3f3),
appBar: AppBar(backgroundColor: Colors.red[900], title: Text("DEMO")),
body: ListView.builder(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
itemCount: 4,
itemBuilder: (context, index) {
return foodsCarouselItemWidget();
},
));
}
}
OUTPUT: