ListView.builder is not overlapping in Stack in Flutter - flutter

I have been using ListView.builder in Stack. But the rounded containers are not overlapping each other. How can I overlap them? I have attached the code and screenshot of output as well.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Emniii extends StatelessWidget {
const Emniii({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 30,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.only(top: 10),
child: Center(
child: Stack(
children: [
ListView.builder(
itemCount: 13,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (_, index) {
return Container(
height: 30,
width: 30,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(15),
),
);
}),
],
),
),
),
);
}
}
Current Output
what I am expecting

ListView is the most commonly used scrolling widget. It displays its children one after another in the scroll direction. In the cross axis, the children are required to fill the ListView.
In your case you just want to generate 13 Containers, use loop or List.generate and use Positioned widget to align them.
I am using left: index * 15, it is shifting right by container's half width.
child: Stack(
children: [
...List.generate(
13,
(index) => Positioned(
left: index * 15,
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
color: index.isEven ? Colors.black : Colors.grey,
borderRadius: BorderRadius.circular(15),
),
),
),
),
],
),
More about ListView and Stack

Related

Flutter : When I rotate a horizontal ListView by Transform.rotate, the left and right edges are cut off

When I use Transform.rotate to make a horizontal ListView diagonal as shown below, it becomes diagonal, but the left and right edges are cut off.
Is there a way to display without clipping the left and right edges, or is there a widget that can be used for that?
I came up with a way to use the Stack widget to overlay the left and right edges with strips of gradation and make them invisible.
I've actually tried it and it's fine, but I thought I'd ask if there is another way.
I think this is happening because the screen width is passed from the parent as a constraint, but is there any way to disable the constraint?
Thank you.
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Animated Icons',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.greenAccent,
body: Column(
children: [
Expanded(
child: Center(
child: SizedBox(
width: double.infinity,
height: 200.0,
child: Transform.rotate(
angle: -math.pi / 20,
child: Container(
color: Colors.white,
height: 200.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 6,
itemBuilder: (context, innerIndex) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
//padding: EdgeInsets.all(4.0),
height: 50.0,
width: 200.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color:
Colors.red.withOpacity(1.0 - 0.1 * innerIndex),
),
child:Center(child:Text(innerIndex.toString()),),
),
),
),
),
),
),
),
),
Container(
height:100.0,
color: Colors.white,
child:Center(
child:Text('title'),
),
)
],
),
);
}
}
With the following code using OverflowBox, the left and right corners are no longer cut off.
Expanded(
child: Center(
child: OverflowBox(
maxWidth: MediaQuery.of(context).size.width*1.2,
child: Transform.rotate(
angle: -math.pi / 20,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.white,
),
height: 200.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 6,
itemBuilder: (context, innerIndex) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
//padding: EdgeInsets.all(4.0),
height: 50.0,
width: 200.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color:
Colors.red.withOpacity(1.0 - 0.1 * innerIndex),
),
child:Center(child:Text(innerIndex.toString()),),
),
),
),
),
),
),
),
),

ListTile inside ListView is causing an error

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/weather_provider.dart';
class BottomListView extends StatelessWidget {
const BottomListView({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final weatherData = Provider.of<WeatherProvider>(context).weatherData;
final isLandscape =
MediaQuery.of(context).orientation == Orientation.landscape;
final height = (MediaQuery.of(context).size.height -
50 -
MediaQuery.of(context).padding.top);
return Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Colors.white),
),
color: Color.fromRGBO(255, 255, 255, 0.2),
),
height: isLandscape ? height * 0.35 : null,
child: Row(
children: [
Expanded(
child: SizedBox(
width: 200,
child: ListView(
scrollDirection: Axis.horizontal,
children: [ListTile(title: Text('Hello'))]),
),
),
],
));
}
}
I want to have horizontal scrollable listView with ListTiles. But without ListTile it works fine . With ListTile it is causing an error.How to solve it? I tried giving it the width but didn't work.
Error:
BoxConstraints forces an infinite width.
These invalid constraints were provided to RenderParagraph's layout() function by the following function, which probably computed the invalid constraints in question:
ListTiles needs to be defined with width params explicitly using SizedBox or Container.
If you dont define width, it will throw infinite width error.
So Wrap your ListTile inside a SizedBox or Container.
Since, ListView's scrollDirection is set to Horizontal, you dont need to place it inside of a row i.e. it will show children horizontally.
If scrollDirection is set to vertical, it will show children in a Column i.e. Vertically
Also you can't do both, Use expanded and give width to a child of a row.
Using Expanded means the child will take maximum size available to it w.r.t to its parent.
Try the below code snippet
Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Colors.white),
),
color: Color.fromRGBO(255, 255, 255, 0.2),
),
// height: isLandscape ? height * 0.35 : null,
height: 100,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
SizedBox(
width: 200,
child: ListTile(
title: Text('Hello'),
),
),
SizedBox(
width: 200,
child: ListTile(
title: Text('Hello'),
),
),
SizedBox(
width: 200,
child: ListTile(
title: Text('Hello'),
),
),
SizedBox(
width: 200,
child: ListTile(
title: Text('Hello'),
),
),
],
),
),

How can I provide a minimum height to ListView.builder's children which can expand automatically if the children require more space?

I am using ListView.builder. In this, I need to set heights for the individual children. As of now, I'm achieving this using a SizedBox, but this doesn't give me the effect I want.
This is important because some of the children are column widgets like this:
return Column(
children: List.generate( //using List.generate for building the UI
data.length, //I don't know this length beforehand
(int i) => Padding(
padding: EdgeInsets.only(
left: 30.0, right: 30.0, bottom: height * 0.05),
child: EducationMobileCard(
//contents here
),
),
),
);
I want to wrap all widgets like this in a parent ListView.builder. Such that, when the user scrolls, each widget takes up the space that is required to fit its contents. Wrapping the widgets in SizedBox doesn't handle overflows. I've tried ConstrainedBox too. But it doesn't let the other parts of the UI access the vacant screen in case a widget hasn't taken up the entire maxWidth. It just simply keeps the space vacant.
This is the ListView.builder widget I'm currently using:
ListView.builder(
itemCount: 7,
itemBuilder: (context, index) {
return SizedBox(height: height, child: widgetList[index]); //height is the screen height here
}
),
I simply want something that specifies the minimum space to occupy but lets the widget handle the maximum space to take within the ListView.builder.
Edit: This is the EducationMobileCard widget:
import 'package:flutter/material.dart';
import '../custom/text_style.dart';
import '../theme/config.dart';
class EducationMobileCard extends StatefulWidget {
const EducationMobileCard(
{Key? key,
//the required parameters
)
: super(key: key);
final double height, width;
final String insttution, location, years, grades, desc, image;
#override
_EducationMobileCardState createState() => _EducationMobileCardState();
}
class _EducationMobileCardState extends State<EducationMobileCard> {
bool isHover = false;
#override
Widget build(BuildContext context) {
return AnimatedContainer(
decoration: BoxDecoration(
boxShadow: [
//contents
],
),
duration: const Duration(milliseconds: 200),
padding: EdgeInsets.only(
top: isHover ? widget.height * 0.005 : widget.height * 0.01,
bottom: !isHover ? widget.height * 0.005 : widget.height * 0.01),
child: InkWell(
onTap: () {},
onHover: (bool value) {
setState(() {
isHover = value;
});
},
child: Container(
alignment: Alignment.topCenter,
padding: EdgeInsets.only(
top: widget.height * 0.04,
left: widget.width * 0.015,
right: widget.width * 0.015,
bottom: widget.height * 0.04),
width: widget.width,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: isHover ? Colors.black12 : Colors.black45,
blurRadius: 10.0,
offset: const Offset(8, 12),
)
],
color: currentTheme.currentTheme == ThemeMode.dark
? Theme.of(context).cardColor
: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(
5.0,
),
),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 5.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset(
'assets/education/${widget.image}',
scale: 1.2,
)),
),
FittedBox(
fit: BoxFit.cover,
child: text(widget.insttution, 25, Colors.white)),
Padding(
padding: const EdgeInsets.only(bottom: 5.0),
child: FittedBox(
fit: BoxFit.cover,
child: text(widget.location, 10, Colors.white)),
),
FittedBox(
fit: BoxFit.cover,
child: Padding(
padding: const EdgeInsets.only(bottom: 11.0),
child: text(
widget.years != ''
? 'Years of study: ${widget.years}'
: '',
12,
Colors.white),
)),
FittedBox(
fit: BoxFit.cover,
child: text(widget.desc, 15, Colors.white)),
FittedBox(
fit: BoxFit.cover,
child: text(
widget.grades != ''
? 'Grades Achieved: ${widget.grades}'
: '',
12,
Colors.white)),
],
),
),
),
),
);
}
}
You can use ConstrainedBox to set the maxmium and minimum hight.
Refer the example below
ConstrainedBox(
constraints: new BoxConstraints(
minHeight: 35.0,
maxHeight: 160.0,
),
child: new ListView(
shrinkWrap: true,
children: <Widget>[
new ListItem(),
new ListItem(),
],
),
)
You can then use the shrinkWrap proper to wrap the unused space like this:-
shrinkWrap: true,
Thank you

Carousel Slider is repeated almost infinite number of times vertically, instead of once, with no error showing

I'm doing a carousel slider, but it's repeating itself almost an infinite number of times, instead of showing there once in my home page, and there is no error message that I can work with, I can't seem to find why it's doing all that, any help?
Carousel Slider code:
class CarouselSliderPage extends StatefulWidget {
const CarouselSliderPage({Key? key}) : super(key: key);
#override
_CarouselSliderPageState createState() => _CarouselSliderPageState();
}
class _CarouselSliderPageState extends State<CarouselSliderPage> {
int activeIndex = 0;
setActiveDot(index) {
setState(() {
activeIndex = index;
});
}
List imageList = [
"assets/images/mobiles/4.png",
"assets/images/laptops/1.jpg",
"assets/images/mobiles/3.png",
"assets/images/laptops/7.jpg",
"assets/images/mobiles/6.png",
];
#override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
SizedBox(
height: 10,
),
Container(
width: MediaQuery.of(context).size.width,
child: CarouselSlider(
options: CarouselOptions(
autoPlayInterval: Duration(seconds: 4),
autoPlayCurve: Curves.fastLinearToSlowEaseIn,
autoPlayAnimationDuration: Duration(seconds: 2),
viewportFraction: 1.0,
onPageChanged: (index, reason) {
setActiveDot(index);
},
),
items: imageList
.map(
(item) => Center(
child: Image.asset(
item,
fit: BoxFit.cover,
),
),
)
.toList(),
),
),
Positioned(
left: 0,
right: 0,
bottom: 10,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: List.generate(imageList.length, (idx) {
return activeIndex == idx ? ActiveDot() : InactiveDot();
})),
)
],
);
}
}
class ActiveDot extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Container(
width: 25,
height: 8,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
),
),
);
}
}
class InactiveDot extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Container(
width: 8,
height: 8,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(5),
),
),
);
}
}
The calling of Carousel Slider class:
Container(
height: MediaQuery.of(context).size.height,
child: ListView.builder(
itemBuilder: (context, index) =>
CarouselSliderPage(),
),
),
ListView.builder is a builder that can and will multiple of 'x' widgets it works like a loop unless told otherwise.
You can try adding a parameter into listview.builder that's called itemCount: 1 or in your specific case add 'imageList.length' and it will cap on the amount of images you have on your list.
or simply remove the listview.builder entirely and just call
CarouselSliderPage(),
Example of code below:
Container(
height: MediaQuery.of(context).size.height,
child: ListView.builder(
itemCount: imageList.length,
itemBuilder: (context, index) =>
CarouselSliderPage(),
),
),
But I would use this one below:
Container(
height: MediaQuery.of(context).size.height,
child: CarouselSliderPage(),
),
Just remove the ListView.builder
Container(
height: MediaQuery.of(context).size.height,
child: CarouselSliderPage(),
),

how to creare Horizontal list in flutter

I am trying to develop a horizontal list view in flutter.
when ever I type scrollDirection: Axis.horizontal, and re run the program I have issues saying.
I tried rapping the whole with listview but also its not working
body: Container(
child:ListView.builder(
itemCount: _list.length,
itemExtent: 200.0,
itemBuilder: (context, i) {
final b = _list[i];
return new ListTile(
title: new Card(
elevation: 1.0,
child: new Container(
height: 293,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
boxShadow: [
BoxShadow(
blurRadius: 6,
color: Colors.black,
offset: Offset(4, 4))
]),
padding: EdgeInsets.all(1.0),
child:
Column(
children: <Widget>[
SizedBox(height: 4),
Padding(
child: Image.network(b.bikeimage),
padding: EdgeInsets.only(bottom: 0.0),
),
Padding(
child: Text(
b.name,
textAlign: TextAlign.right,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.black), ),
padding: EdgeInsets.only(left: 10),
),
],
),
),
),
onTap: () {
});
},
),
)
You got the error because your Container wasn't given a specified width and height.
The code below would work perfectly, Check it out,
body: Container(
// give your container any desired height
height: 500,
// give your container any desired width
width: double.infinity,
child: ListView.builder(
// set the scroll direction to horizontal for a horizontal list
scrollDirection: Axis.horizontal,
...
// the rest of your listview.builder code here
...
),
);
Hope this helps.
It looks like your container has no height or width constraints so it can't be drawn.
You havent mentioned scrollaxisdirection in your code add that and try. For further refer
https://pusher.com/tutorials/flutter-listviews
you can use below class for building listview in horizontal or vertical mode:
import 'package:flutter/material.dart';
class HorizontalListView extends StatefulWidget {
HorizontalListView({this.items, this.horizontal, this.width, this.height});
final List<Widget> items;
final bool horizontal;
final double width;
final double height;
#override
_HorizontalListState createState() {
// TODO: implement createState
return _HorizontalListState();
}
}
class _HorizontalListState extends State<HorizontalListView> {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: widget.height > 0 ? widget.height : 200.0,
child: ListView(
scrollDirection: widget.horizontal ? Axis.horizontal : Axis.vertical,
children: widget.items,
));
}
#override
void initState() {
super.initState();
}
}