Image.network overriding the border radius property flutter - flutter

And after image is loaded, it is taking full container size by overriding the border radius.
This is the out put i was getting Screenshot1
this is the output which i was looking for
ScreenShot2
code:
StaggeredGridView.countBuilder(
padding: EdgeInsets.all(8.0),
crossAxisCount: 4,
itemCount: wallpapersList.length,
itemBuilder: (context, index) {
String imgPath = wallpapersList[index].data['url'];
return Container(
decoration: BoxDecoration(
color: kSecondaryColor,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
child: Image.network(imgPath, fit: BoxFit.fitHeight,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: SpinKitDoubleBounce(
color: Colors.white,
),
);
}));

Images don't follow the border radius of the Container.
So what you'll have to do is use ClipRRect()
Use it like this
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.network(),
),
),
Just add the ClipRRect widget and that should work.

Related

FutureBuilder inside GridView.builder while retrieving an Image from memory freezes UI completely

I'm trying to make an app that loads multiple images from metadata and puts them in a gridview.builder, on Windows. Having thousands of items, I tried using FutureBuilder to retrieve the image and put it there when it's loaded.
Container(
height: 725,
child: GridView.builder(
cacheExtent: 250,
padding: EdgeInsets.only(right: 20),
controller: _scrollController,
itemCount: allmetadata.length,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
childAspectRatio: 0.8 ,
maxCrossAxisExtent: 275,
crossAxisSpacing: 25,
mainAxisSpacing: 25),
itemBuilder: (BuildContext context, int sindex) {
return
Container(
child:
MouseRegion(
cursor: SystemMouseCursors.click,
onEnter: (event){
//something
},
onExit: _incrementExit3,
child: GestureDetector(
onTap: (){
//something
},
child: Column(
children: [
Container(
height: 250,
width: 250,
child:
allmetadata[sindex].imageloaded ?
Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.cover,
image: Image.memory(allmetadata[sindex].image).image,
)
),
): FutureBuilder(
future: imageretriever(allmetadata[sindex].path),
builder: (ctx, snapshot) {
if (snapshot.hasData) {
return
Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.cover,
image: Image.memory(snapshot.data!).image,
)
),
);
}
else if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18),
),
);
} else{
return Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.cover,
image: Image.memory(File("assets\\bg.png").readAsBytesSync()).image,
)
),
);
}
},
),
Container(
height: 10,
),
Text(
//something here
),
],
),
),
),
);
},
)
Any solution to this? How can I make this unfreeze or be more efficient?
I tried solving it by:
Loading every single image in the init, but the app takes way too long to load
Putting the FutureBuilder so every image loads when it's on the screen, but it freezes the UI when scrolling.
I couldn't find a better solution. Tried using Isolates but they didn't seem to work

Error when listviewBuilder scrollDirection is horizontal

I got this very simple code, but somehow I get an error when I set scrollDirection to Axis.horizontal
I invoke this widget in Page that has MaterialApp and Scaffold widgets.
What I tried is wrapping ListView.builder with expanded, I removed all widgets besides Container and ListView.builder
Here is code:
class CalendarPage extends StatelessWidget {
List<String> hoursList = ['00:00', '00:30', '01:00'];
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25), topRight: Radius.circular(25))),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
TextLabel('Choose preferred date of meeting'),
ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: hoursList.length,
itemBuilder: (context, i) {
return Text(hoursList[i]);
})
],
),
),
);
}
}
thank you in advance
Wrap your ListView inside Expanded or Flexible widget. refer below code:
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25),
topRight: Radius.circular(25),
),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
TextLabel('Choose preferred date of meeting'),
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: hoursList.length,
itemBuilder: (context, i) {
return Text(
hoursList[i],
);
}),
)
],
),
),
),
Result screen->
You must wrap with a container your Listview and define height value.
class CalendarPage extends StatelessWidget {
List<String> hoursList = ['00:00', '00:30', '01:00'];
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25), topRight: Radius.circular(25))),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
TextLabel('Choose preferred date of meeting'),
Container(
height: 300,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: hoursList.length,
itemBuilder: (context, i) {
return Text(hoursList[i]);
}),
)
],
),
),
);
}
}

Flutter "NoSuchMethodError: The getter 'position' was called on null" when sticky header is used to wrap around GirdView.builder widget

Trying to use sticky header to wrap around a GridView.builder widget, because I want to have a header follow when the user scrolls down enough the screen, but getting "NoSuchMethodError: The getter 'position' was called on null. Receiver: null Tried calling: position See also: https://flutter.dev/docs/testing/errors". I use this same widget to wrap around a Column widget. I tried wrapping the GridView with Container, Column and other widgets. I would just get other errors. What does this error mean?
return _stickyHeader.render(
headerChild: Text(''),
content: GridView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
),
itemCount: state.avatarUrlList.length,
itemBuilder: (BuildContext context, int index) {
if (index == state.currentSelectedAvatarIndex) {
return Center(
child: InkWell(
onTap: () => _onAvatarImageClicked(
state.avatarUrlList[index], index),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.red,
width: 4.0,
),
),
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.transparent,
backgroundImage:
NetworkImage(state.avatarUrlList[index]),
),
),
),
);
} else {
return Center(
child: InkWell(
onTap: () => _onAvatarImageClicked(
state.avatarUrlList[index], index),
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.transparent,
backgroundImage:
NetworkImage(state.avatarUrlList[index]),
),
),
);
}
},
),
);
}
Widget render({Widget headerChild, Widget content}) {
return StickyHeader(
header: Container(
decoration: BoxDecoration(
color: ThemeConstants.ceruleanCrayola,
borderRadius: BorderRadius.circular(15),
),
height: 50.0,
padding: EdgeInsets.symmetric(horizontal: 16.0),
margin: EdgeInsets.symmetric(horizontal: 20.0),
alignment: Alignment.centerLeft,
child: headerChild,
),
content: content,
);
}
Based on https://pub.dev/packages/sticky_headers
You can place a StickyHeader or StickyHeaderBuilder inside any scrollable content, such as: ListView, GridView, CustomScrollView, SingleChildScrollView or similar.
You may need to try put your StickyHeader to SingleChildScrollView for basic.

Replace the first element of a grid with other widget

I need help replacing the first element of the StaggeredGridView with other Widget, say, Container. I have been trying to change it for hours but couldn't find a way.
Padding(
padding: const EdgeInsets.all(5.0),
child: new StaggeredGridView.countBuilder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
crossAxisCount: 4,
mainAxisSpacing: 3,
crossAxisSpacing: 3,
itemCount: posts.length,
itemBuilder: (BuildContext context, int index) {
Post post = posts[index];
return Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(15))),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(7)),
child: Image(
image: AssetImage(post.imageUrl),
fit: BoxFit.cover,
),
),
);
},
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 3 : 2),
),
),
Above is the code for the grid I am using.
Thank you.
itemBuilder will generate a widget with the function provided, thus you could just add an if statement to build one or other widget:
itemCount: posts.length + 1, // added one item on the beginning
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return Container(
child: // your widget
);
} else {
Post post = posts[index - 1]; // remember that you have +1 on your index
return Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(15))),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(7)),
child: Image(
image: AssetImage(post.imageUrl),
fit: BoxFit.cover,
),
),
);
}
},

How to use stack widget inside a StaggeredGridView in flutter?

om/cFMse.png
why images get weird size when i use stack widget inside StaggeredGridView.. When i remove the stack from the grid view the layout seems perfectly fine . am i using the layout in a wrong way?
new StaggeredGridView.countBuilder(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
itemCount:model.hit.length,
itemBuilder: (context, index) {
return
Stack(
children: [
Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(35))),
child:
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(35)),
child:FadeInImage.memoryNetwork(
placeholder: kTransparentImage, image: model.hit[index]['largeImageURL'],fit: BoxFit.cover,)
),
)
],
);
},
staggeredTileBuilder: (index) {
return new StaggeredTile.count(1, index.isEven ? 1.2 : 1.8);
})
try passing a StackFit.expand argument to the Stack
return
Stack(
fit: StackFit.expand, /// <-- expand to the size of the parent constraint
children: [
Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(35))),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(35)),
child:FadeInImage.memoryNetwork(
placeholder: kTransparentImage, image: model.hit[index]['largeImageURL'],fit: BoxFit.cover,)
),
)
],
);