Fixed number of rows, with horizontal scroll for chips - flutter

I have tried using gridlist, flutter staggered grid view but was still not able to do it.
This is what I would like it to look like.
Intended look
The best i could do is this, but the problem is that if i use a grid it gives each element same width.
GridView.builder(
scrollDirection: Axis.horizontal,
itemCount: widget.profile.interests.length,
gridDelegate:
new SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 50,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
childAspectRatio: 0.3),
itemBuilder: (BuildContext context, int index) {
return CardWidget(context, index);
})
output

You can use ListView instead...
ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Row(
children: <Widget>[
CardWidget('Long text'),
CardWidget('Even longer text'),
CardWidget('Long text'),
CardWidget('Even longer text'),
CardWidget('Text'),
],
),
Row(
children: <Widget>[
CardWidget('Text'),
CardWidget('Short text'),
CardWidget('Text'),
CardWidget('Short text'),
CardWidget('Long text'),
CardWidget('Even longer text'),
],
)
])
],
);
OUTPUT
UPDATE WITH BUILDER
Here is another solution with builder. There is probably better solution for this. It just a first one what cross my mind
List<String> list = ['Long text', 'Even longer text', 'Long text', 'Even longer text', 'Text', 'Text', 'Short text', 'Text', 'Short text', 'Long text', 'Even longer text'];
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 50,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: list.length,
itemBuilder: (context, index) {
return index.isEven ? CardWidget(list[index]) : Container();
},
),
),
SizedBox(
height: 50,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: list.length,
itemBuilder: (context, index) {
return index.isOdd ? CardWidget(list[index]) : Container();
},
),
)
],
),
)

List<int> topRow = [];
List<int> bottomRow = [];
List<int> _addRowElements() {
for (int i = 0; i < numbers.length; i++) {
if (i.isEven) {
topRow.add(numbers[i]);
} else {
bottomRow.add(numbers[i]);
}
}
return topRow;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: MediaQuery.of(context).size.width,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
..._addRowElements()
.map(
(number) => TextAndIconWidget(
label: number.toString(),
),
)
.toList(),
],
),
Row(
children: <Widget>[
...bottomRow
.map(
(number) => TextAndIconWidget(
label: number.toString(),
),
)
.toList(),
],
),
],
),
],
),

Related

How do i avoid overlapping when using column Listview.builder inside column in flutter?

I have this code which causes the bottom to overlap and I cannot figure out what to do. I use a listview.builder which has a column with another listview.builder inside.
image of overlap of text but the listtile continues
the code
body: SafeArea(
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.fromLTRB(
(_deviceWidth! * 0.05), 10, (_deviceWidth! * 0.05), 0),
child: Column(
children: [
_titleText(),
ListView.builder(
itemCount: _exercises.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (BuildContext context, int _index) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
if (_exercises[_index].sets.isNotEmpty)
Text(_exercises[_index].name),
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: _exercises[_index].sets.length,
itemBuilder: (BuildContext _context, int _index2) {
return ListTile(
leading: Text("Sæt " + (_index2 + 1).toString()),
trailing: Text(
_exercises[_index].sets[_index2].reps.toString() +
" reps"),
tileColor: Colors.grey,
onTap: () {
_repsPopUp(_index, _index2);
},
title: Text(_exercises[_index].name),
onLongPress: () {
setState(() {
_exercises[_index].sets.removeAt(_index2);
if (_exercises[_index].sets.isEmpty) {
_exercises.removeAt(_index);
}
});
},
)
}),
],
);
});
],
),
),
),
),

Flutter: Add ReorderableList inside a ListView

The issue I have is adding a ReorderableList inside a ListView, the ReorderableList does not allow for reordering now. I can scroll the list but the long press for reodering is not active anymore. Here is my code:
Widget get _reorder => ReorderableList(
physics: ClampingScrollPhysics(),
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) => _cells[index],
itemCount: _cells?.length ?? 0,
onReorder: _setListOrder,
);
#override
Widget build(BuildContext context) {
return Container(
decoration: context.logoBackground,
child: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_reorder,
Text('End of List Widget')
],
);
}),
)
],
),
),
);
}
I did it with ReorderableListView instead of ReorderableList like this:
void _setListOrder(int oldIndex, int newIndex) {
print('old: $oldIndex, new: $newIndex'); // You need to implement the method!
}
var _cells = List.generate(30, (index) { // Your cells
return ListTile(
key: Key('$index'),
title: Text(
'$index',
),
);
});
Widget get _reorder => ReorderableListView.builder(
physics: ClampingScrollPhysics(),
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) => _cells[index],
itemCount: _cells?.length ?? 0,
onReorder: _setListOrder,
);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: ListView( // I transformed it to simple ListView because you have one children
shrinkWrap: true,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height, // You need this height, or a custom height!
child: _reorder),
Text('End of List Widget')
],
)
],
),
)
],
),
),
),
);
}
If you want to check the documentation, you can do it here!

Scrolling Wrap Widget With 2 lines and Variable Size Objects

I cannot figure out how to create a Scrolling Wrap Widget inside my app that displays a number of items over 2 lines and is scrollable Horizontally to reveal the lines content:
return
SafeArea(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Wrap(
direction: Axis.horizontal,
runAlignment: WrapAlignment.center,
children:[
for (var i=0;i<10;i++) Card(
elevation: 6,
color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(title,),
),
),
]
),
),
);
The result for this is :
I tired to wrap the Wrap with a Container with fixed height and every other combination imaginable and have not found a way to do this correctly, Basically I want to achieve a grid like functionality where the children are not all constrained to a fixed sized cell but are maintaining their variable width .
Expected result as requested would be a scroll-able list that looks like this:
This Answer Will Help You.
List<String> list = ['Long text', 'Even longer text', 'Long text', 'Even longer text', 'Text', 'Text', 'Short text', 'Text', 'Short text', 'Long text', 'Even longer text'];
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 50,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: list.length,
itemBuilder: (context, index) {
return index.isEven ? CardWidget(list[index]) : Container();
},
),
),
SizedBox(
height: 50,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: list.length,
itemBuilder: (context, index) {
return index.isOdd ? CardWidget(list[index]) : Container();
},
),
)
],
),
)
I refer from this solution.
Output like this
I was have similar problem and I fixed with constrained box. And can you try this;
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height * 0.25,
maxWidth: MediaQuery.of(context).size.width * 1.5,
),
child: Wrap(
direction: Axis.horizontal,
crossAxisAlignment: WrapCrossAlignment.end,
alignment: WrapAlignment.start,
spacing: 10,
runSpacing: 2,
children: List.generate(
myList.length,
(index) {
return ChoiceChip(
onSelected: (bool isSelected) {
//when selected it works code
},
label: SizedBox(
height: 20,
child: Text(name),
),
selected: false);
},
).toList(),
),
),
);

Flutter : SingleChildScrollView doesn't work with columns and grid view. Why?

I can't scroll to see my lower grid view. Why is that? What should I change to make this work? Please help!!!
Thank you in advance.
Scaffold(
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
Text('GridView 1'),
GridView.count(
crossAxisCount: 3,
shrinkWrap: true,
children: List.generate(
9,
(index) {
return TouchableImageCard(
imagePath: 'assets/images/view_${index + 1}.jpg',
);
},
),
),
Text('GridView 2'),
GridView.builder(
itemCount: list_item.length,
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return TouchableImageCard(
imagePath: 'assets/images/view_${index + 1}.jpg',
// width: 150,
// height: 150,
);
},
),
],
),
),
),
);
Thank you for helping me in flutter.
It actually does work, just not the way you need it.
The problem is that GridView itself is a scrollable widget too. So when you try to scroll the page, it actually tries to scroll those GridViews and not the SingleChildScrollView.
To disable GridView scrolling ability - you need to add one more parameter.
GridView.count(
physics: NeverScrollableScrollPhysics(),
...
),
Try this:
Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[]
multiple gridview scrollable
Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Text('GridView 1'),
Container(
height: 400,
child: GridView.count(
crossAxisCount: 3,
shrinkWrap: true,
primary: false,
children: List.generate(
20,
(index) {
return TouchableImageCard(
imagePath: 'assets/images/view_${index + 1}.jpg',
);
},
),
),
),
Text('GridView 2'),
Container(
height: 400,
child: GridView.builder(
itemCount: 20,
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return TouchableImageCard(
imagePath: 'assets/images/view_${index + 1}.jpg',
// width: 150,
// height: 150,
);
},
),
),
],
),
),
);

How to implement multiple horizontal lists inside a column

I want to implement multiple horizontal lists that contain cards with some details, and now all i get is just white screens(without errors)
This is my code:
Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 50,
itemBuilder: (BuildContext context, int index) {
Card(
child: Column(
children: <Widget>[
Text('fsfssffs'),
],
),
);
},
),
),
SizedBox(
height: 20.0,
),
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 50,
itemBuilder: (BuildContext context, int index) {
Card(
child: Column(
children: <Widget>[
Text('fsfssffs'),
],
),
);
},
),
),
],
),
);
I want to achieve something like the facebook stories
Use => on List itemBuilder to return the items:
Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 50,
itemBuilder: (BuildContext context, int index) =>
Card(
child: Column(
children: <Widget>[
Text('fsfssffs'),
],
),
);
},
),
),
SizedBox(
height: 20.0,
),
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 50,
itemBuilder: (BuildContext context, int index) =>
Card(
child: Column(
children: <Widget>[
Text('fsfssffs'),
],
),
);
},
),
),
],
),
);
You were not returning any widget from your itemBuilder method which is why you were unable to see anything on screen.
Basically, there are 2 solutions for this, I am giving you idea using a build() method you can do it in your itemBuilder.
(1) Use => (fat arrow notation)
Example:
Widget build(context) => Text("Hi");
(2) Use return.
Widget build(context) {
return Text("Hi");
}