I'm trying to display a number of horizontal lines on the screen.
What would be the best approach to achieve this?
I attempted the following though the screen height returned seems to be bigger than the actual screen height so the divisions end up off the screen. I end up with the image shown at https://imgur.com/a/Dx1wH1Y.jpg
final int _numLines = widget._verticalLines;
return ListView.builder(
itemCount: _numLines,
itemBuilder: (BuildContext ctxt, int index) {
return Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height / (_numLines + 1),
),
child: Container(
height: 1.0,
width: MediaQuery.of(context).size.width,
color: Colors.red,
),
);
});
One way you can do this is like this.
Widget build(BuildContext context) {
final int _numLines = widget._verticalLines;
return Column(
children: getChildren(_numLines),
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
);
}
List<Widget> getChildren(int number) {
List<Widget> list = [];
for (int i = 0; i < number; i++) {
list.add(
Container(
height: 1.0,
width: MediaQuery.of(context).size.width,
color: Colors.red,
),
);
}
return list;
}
if i understand your question correctly,try this if it is not let me know
ListView.builder(
itemCount: 5,
shrinkWrap:true,
itemBuilder: (context, index) {
return Container(
height:120,
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.red,width: 2,)),
),
);
},
)
Use MediaQuery and divide the height of the screen by the given number
Example: MediaQuery.of(context).size.height / 5. - This will be dividing the screen into five equal parts, this will work across all devices.
Code:
ListView.builder(
shrinkWrap:true,
itemCount: number,
itemBuilder: (BuildContext ctxt, int index) {
return Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height / number,
),
child: Container(
height: 2.0,
width: MediaQuery.of(context).size.width,
color: Colors.red,
),
);
})
Related
I've been trying to make the first item look like this. And make them all scrollable.
But only the items which are located in two groups are scrolling. The image at the top stays where it is. I want it scrolls too. Anyone can help?
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomBar(),
body: Column(
children: [
Container(
width: 550,
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
// color: Colors.amber,
image: const DecorationImage(
fit: BoxFit.cover,
// I want his part to be scrolled.
image: AssetImage("assetss/fortuneteller.jpg"))),
),
SizedBox(
height: 30,
),
Padding(
padding: const EdgeInsets.only(
left: 15,
right: 15,
),
This is the part where items that are able to scroll are located.
child: Container(
width: screenWidth / 1,
height: screenlHeight / 1.6,
child: GridView.builder(
itemBuilder: (BuildContext context, int index) {
return CardForFortuneTellers();
},
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
mainAxisExtent: 500,
mainAxisSpacing: 0, //screenlHeight / 20,
crossAxisCount: 2,
),
itemCount: 10,
),
),
)
],
),
);
}
}
Listview works perfectly fine on mobile and it tablets when there are less than 8 items ( the 2nd row in the picture ) but when I generate more items it doesn't work ( 1st row with the red thing in the picture).I tried giving the container width too but it didnt work as well.
Container(
width: pageWidth - 40,
height: 50,
alignment: Alignment.center,
margin: EdgeInsets.only(
top: pageHeight * 0.02,
),
decoration: boederDailyQHomePage,
child: const Text("سوال روزانه"),
), // box for daily question
const SizedBox(
height: 10,
),
//test categories list
TitleWidget(
text: "تست ها",
onPressed: () {
pushNewScreen(context,
screen: TestsPage(
initialIndex: 0,
),
withNavBar: true);
}),
Container(
height: 180,
padding: const EdgeInsets.only(right: 7),
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext ctx, int index) {
return TestCategoryHomePage(
testName: Provider.of<TestCategoriesData>(context,
listen: false)
.testCategoryData[index],
testImage: testCategoriesImageData[index],
initialIndex: index,
);
},
itemCount: Provider.of<TestCategoriesData>(context,
listen: false)
.testCategoryData
.length
),
),
const SizedBox(
height: 5,
),
//educations list
TitleWidget(
text: "مقاله ها",
onPressed: () {
pushNewScreen(context,
screen: const EducationsPage(), withNavBar: true);
},
),
Container(
height: 180,
padding: const EdgeInsets.only(right: 7),
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext ctx, int index) {
return EducationWidgetHomePage(
education: Provider.of<EducationData>(context,
listen: false)
.educationData[index]);
},
itemCount:
Provider.of<EducationData>(context, listen: false)
.educationData
.length),
),
This is the picture
I am trying to run the Xcode Accessibility Inspector over my Flutter app and have issues when I have a vertical list that has a nested horizontal list. The screen reader gets stuck on the horizontal list and either never progresses or drops focus. I expected the reader to read off each square but observed the reader getting stuck on horizontal list. I have tried to create an example app to demo the issue.
Here is the build function of my stateless widget:
Widget _getVertList(double width) {
return ListView.builder(
shrinkWrap: true,
itemBuilder: (_, i) {
if (i != 0 && i % 3 == 0) {
return _getHorzList(width); // show horizontal list
}
return Semantics(
label: 'RED BOX $i',
child: Column(
children: [
Container(
color: Colors.red,
height: 100,
width: 100,
),
const SizedBox(
width: 10,
height: 10,
)
],
),
);
},
itemCount: 20,
);
}
Widget _getHorzList(double width) {
return SizedBox(
height: 101,
width: width,
child: Column(
children: [
Expanded(
child: ListView.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemBuilder: (_, i) {
return Semantics(
label: 'Blue BOX $i',
child: Row(
children: [
Container(
color: Colors.blue,
height: 100,
width: 100,
),
const SizedBox(
width: 10,
height: 10,
)
],
),
);
},
itemCount: 10,
),
),
],
),
);
}
#override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
return Scaffold(
body: _getVertList(width, height));
}
How to do auto spacing in listview.builder().For example i need something like MainAxisAlignment.spaceEvenly in column, but i need it in list view.builder().I will have two or three not scrollable horizontal items.So i need spacing between items for different sceen sizes.Could you help me.
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 3,//list.length expected two or three
itemBuilder: (BuildContext context, int index) {
return Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
color: Colors.amber,
border: Border.fromBorderSide(
BorderSide(
color: Color(0xFF8D4AE9),
width: 1.0,
),
),
),
);
},
),
For example, picture
enter image description here
add margin and padding as much you want.
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 7,//list.length expected two or three
itemBuilder: (BuildContext context, int index) {
return Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10),// add margin
padding: EdgeInsets.all(10),// add padding
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
color: Colors.amber,
border: Border.fromBorderSide(
BorderSide(
color: Color(0xFF8D4AE9),
width: 1.0,
),
),
),
);
},
)
You can try adding a SizedBox with fixed height: or width: or even both between the content you want. Another solution is adding Padding with equal values.
First you have to wrap your list view with the container or card or sized box if you want the horizontal Scroll of the list. Next how you just wrap your return width with the padding to the padding to get the padding.
Try this code:
Container(
height: 100,width: MediaQuery.of(context).size.width,
child:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 13,
itemBuilder: (context, i) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 100,
decoration: BoxDecoration(
color: Colors.black,
),
),
);
},
),
),
How can I show an horizontal Listview inside a Stack in Flutter?
Listview is part of a card inside another (vertical) Listview.
I can show it correctly if I give it a fixed width, but I should not, and I want it to adapt to the Card width.
I have the following code:
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
final PlatformModel platform = snapshot.data[index];
final EdgeInsets padding = index == 0
? const EdgeInsets.only(
left: 16.0,
right: 8.0,
top: 8.0,
bottom: 8.0,
)
: const EdgeInsets.only(
left: 8.0,
right: 8.0,
top: 8.0,
bottom: 8.0,
);
bloc.fetchPlatformLogo(platform.platformLogo);
String fetcher = "platforms='${platform.id}'";
bloc.fetchGames(fetcher);
return Padding(
padding: padding,
child: Card(
child: Container(
height: 250.0,
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(NETWORK_URL),
_listGames(fetcher),
],
),
),
),
);
},
);
and
Widget _listGames(String fetcher) {
final bloc = BlocProvider.of<GamesBloc>(context);
return Positioned(
bottom: 5.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
height: 200,
child: StreamBuilder(
stream: bloc.games,
builder: (context,
AsyncSnapshot<Map<String, Future<List<GameModel>>>> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return FutureBuilder(
future: snapshot.data[fetcher],
builder:
(context, AsyncSnapshot<List<GameModel>> snapshotGames) {
if (!snapshot.hasData || !snapshotGames.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshotGames.data.length == 0) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.warning,
size: 100.0,
),
Text("Nessun gioco presente"),
],
),
);
}
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshotGames.data.length,
itemBuilder: (context, index) {
final GameModel game = snapshotGames.data[index];
final lastGame = index == snapshotGames.data.length;
bloc.fetchGameCover(game.cover);
return Padding(
padding:
lastGame ? 0.0 : const EdgeInsets.only(right: 8.0),
child: GameCard(game: game),
);
},
);
},
);
},
),
),
),
);
}
But I keep getting "Horizontal viewport was given unbounded width".
If I put, for example
child: SizedBox(
height: 200,
width: 300,
child: StreamBuilder(
It shows well, but of course it is no good.
What can I do?
Can you try with this..
ListView.builder(
shrinkWrap: true,
...