I'm making a flutter app where there's a gridview of cards that shows details of different items in a list. In the top right corner of the card I added a banner using the banner widget that shows the current status of the item.
This is what the banner looks like. But I want to move the banner a bit more to the corner as well as make it more wider.
Card(
elevation: 1.0,
child: ClipRect(
child: Banner(
textStyle: TextStyle(color: Colors.white),
message: 'OPEN'
location: BannerLocation.topEnd,
color: const Color(0xFF0db3ae),
child: InkWell(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('......')
),
onTap: () {},
),
),
),
);
This is the basic structure of the code I used to make the cards and to make sure the banner wraps around the card. But i can't find a way to increase it's width or move it a bit more to the corner.
Related
I am currently working on my first mobile app, and need some help with some of the code pertaining to my navbar. I have watched some videos and read some docs on using the tag BottomNavigationBar to better sort and make my navigation bar but I am struggling to make it look how I want. Right now I have the default navbar it gives you with a different background color but I added padding to give it space from the edges of the screen. I now want to round the corners but it seems wherever I try to apply a border-radius it just doesn't work. Any idea how I can go about rounding these corners to give a good-looking floating navbar look? Thanks!
Current Navbar Look
Widget build(BuildContext context) {
return Scaffold(
// ignore: prefer_const_literals_to_create_immutables
bottomNavigationBar: Container(
padding: const EdgeInsets.all(8.0),
child: Material(
borderRadius: BorderRadius.circular(10),
color: Colors.black,
child: BottomNavigationBar(
fixedColor: Colors.white,
unselectedItemColor: Colors.white,
backgroundColor: Color.fromARGB(255, 27, 27, 27),
currentIndex: _selectedIndex,
// ignore: prefer_const_literals_to_create_immutables
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.add), label: 'Add Friend'),
BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search Friends'),
],
),
),
),
);
}
The Problem
I have a card that contains a row of two elements, the second one being a column of two texts. One of the texts can be long, so the column-element itself can overflow.
Coming from CSS, a "width: 100%" would usually be enough here.
What I already tried
I learned you could wrap the column with something like Expanded or Flexible, but it leads the text to become invisible (and the overflow still existing).
I am not sure where exactly I have to put the Flexible/Expanded.
I looked into similar questions and there was always an "Expanded", but I could not apply any of said solutions to my layout so far.
Question
What is the cleanest way to get the outlined box to be only as wide as the padding should allow?
Code
#override
Widget build(BuildContext context) {
return (Card(
child: InkWell(
onTap: () {
print("tab" + name);
},
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(25),
child: SizedBox(
width: 50,
height: 50,
child: Image(
image: NetworkImage(imageUrl),
),
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Container(
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.purpleAccent),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
),
Text(
description,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left,
),
],
),
),
),
],
),
),
),
));
}
You can surround the Padding widget inside your Row with an Expanded widget - The child of Expanded will then size itself to the space available.
For some additional context, Rows and Columns are both Flex widgets. Flex widgets layout their children along an axis, and the size of this axis is unbounded by default. Even though the widget "knows" how much space it has available on the display, it doesn't pass that information along to its children, so the children are free to take whatever size they want, which means they can potentially overflow the container.
An Expanded widget can only be placed as a direct child of a Flex widget, and it automatically takes up a given proportion (given by the flex property) of the space available to it (by default that will simply be all of the space which is not taken up by other widgets in the children list).
So essentially, Expanded will take up as much space as the parent widget has available, and then will constrain it's child to be no larger than that (along whichever axis pertains).
The link above to the Flex documentation has more info.
return Stack(
children: <Widget>[
Image.asset(
'assets/logo/greenframe.png',
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.fill,
),
...
I'm using this code for using a background image on my app. It is a rectangle frame. It works okay with my Redmi Note 8 screen but on another phone with a different size of the screen, it's not looking good. Not fitting the screen. Is there a way to fix this?
How can I set the size based on screen size?
You can use MediaQuery.of(context).size to get a responsive design for all screen sizes. For example, defining the size of the icon button will look like this:
IconButton(
iconSize: MediaQuery.of(context).size.height * 0.03, // Here!
icon: Icon(
Icons.arrow_back,
color: Colors.black,
),
onPressed: () => Navigator.pop(context),
),
Read more from here.
This is from London App Brewery completed Flutter project "BMI Calculator": https://github.com/londonappbrewery/BMI-Calculator-Flutter-Completed.git
I'm not sure if caused by my outdated android phone or the android screen is too small, but I'm getting Bottom Overflow Pixels For example, the top two cards have an error message of Bottom Overflow by 19 Pixels, the center card has an error of Bottom Overflowed by 60 pixels, and bottom two cards have an error of Bottom Overflowed by 56 pixels.
Additionally, if I rotate my phone to 90 degrees, the card image size decreases dramatically, as you can see from the second image.
Please help me fix this.
Thank you
class _InputPageState extends State<InputPage {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Column(
children: <Widget[
Expanded(
child: Row(
children: <Widget[
Expanded(
child: ReusableCard(
colour: colorCode,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget[
Icon(
FontAwesomeIcons.mars,
size: 20.0,
),
SizedBox(
height: 5.0,
),
Text(
'MALE',
style: TextStyle(
fontSize: 10.0,
color: Color(0xFF8D8E98),
),
)
],
),
),
)]),),
]));
}
}
This is happening because your UI takes up too much space for your phone! You have two options to solve this.
Either you wrap your overflowing widgets in a scrolling widget, like SingleChildScrollView, this will let you scroll the Widget if it is too large.
Or you calculate the size of the widget based on device size. You can do that by using the MediaQuery class:
MediaQuery.of(context).size.height Can be used to get device height for instance.
Have a look at this article if you want to go down that path. https://medium.com/tagmalogic/widgets-sizes-relative-to-screen-size-in-flutter-using-mediaquery-3f283afc64d6
Of course you could also just reduce the size of your Widgets so it fits on your device. Thats fine for Tutorial purposes, but if you actually want to publish an App, go with the MediaQueryapproach.
Good luck!
I am looking to create an indicator widget for my pageview app, that means that It will be inside my widget tree.
What I have remarked is that When I use
PageViewIndicator class, it's automatically placed either in bottom or centre but when I place it in the center, for example, it hides my fields thing that I don't want, so what do you think?
I have used This
PageIndicatorContainer(
pageView: PageView(
children: customWidgetLists,
controller: controller,
),
align: IndicatorAlign.Center,
length: 4,
indicatorSpace: 20.0,
padding: const EdgeInsets.all(10),
indicatorColor: Colors.white,
indicatorSelectorColor: Colors.blue,
shape: IndicatorShape.circle(size: 12),
// shape: IndicatorShape.roundRectangleShape(size: Size.square(12),cornerSize: Size.square(3)),
// shape: IndicatorShape.oval(size: Size(12, 8)),
}
Everything works fine, just I want the dots to be in the centre of the screen and not cover the other widgets because it did.