How to handle the flutter widget size compatible to all device? - flutter

Here I'm taking two widgets one is textfield widget and another one is Icon widget both are placed in seperate container. I'm very new to flutter, I dont know how to place two widgets parallely each other with different size. But somehow designed for my mobile but when i run my app on different mobile its widgets are overflowing.Can anybody help me out from this issue. Thanks in advance!!! For More reference see the image given.
Here is my code
Padding(
padding: const EdgeInsets.only(
top: 10.0, bottom: 4.0, right: 20.0, left: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(
child: Material(
color: Colors.white,
elevation: 3.0,
// CONTAINER BACKSIDE SHADOW DEPTH
borderRadius: BorderRadius.circular(5.0),
shadowColor: Color(0xFF90A4AE),
//CONTAINER BACKSIDE SHADOW COLOR
child: Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: IconButton(
icon: Icon(
Icons.search,
color: Colors.pink,
),
onPressed: () {},
),
hintText: "Search for restaurant",
hintStyle: TextStyle(fontSize: 15),
),
onChanged: (input) {
print(input);
},
),
),
),
height: 55.0,
width: MediaQuery
.of(context)
.size
.width - 120,
decoration: BoxDecoration(
border: Border.all(
color: Colors.white,
width: 1.0,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(5.0),
),
),
Container(
child: Material(
color: Colors.white,
elevation: 3.0,
// CONTAINER BACKSIDE SHADOW DEPTH
borderRadius: BorderRadius.circular(5.0),
shadowColor: Color(
0xFF90A4AE),
child: IconButton(
icon: Icon(Icons.tune),
color: Colors.pink,
onPressed: () {
[enter image description here][1]
},
), //CONTAINER BACKSIDE SHADOW COLOR
),
height: 55.0,
width: MediaQuery
.of(context)
.size
.width - 300,
decoration: BoxDecoration(
border: Border.all(
color: Colors.white,
width: 1.0,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(5.0)),
)
],
),
),

From the screenshot and the code, I see there are two widgets in a row and the screen is overflown as it couldn't accommodate and show both of them.
There are two solutions:
To show the row children in the next line automatically if overflown.
To make the row children scrollable row-wise.
NOTE: SMLink is my custom widget. Just used here for demonstration purposes.
SOLUTION-1
The simple workaround would be to use the Wrap widget instead of the Row widget.
The Wrap widget simply takes the children who are prone to screen overflow and places them on the next line. You can set the direction, spacing etc, in addition to the options that Row widget can offer. Try it & let me know.
// By default, it behaves as Row, you can set the direction attribute to Axi.vertical to mimic the Column widget.
Wrap(
spacing: 8.0,
runAlignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
alignment: WrapAlignment.center,
children:
[
SMLink(logo: 'Medium', link: Constants.LINK_MEDIUM),
SMLink(logo: 'linkedin', link: Constants.LINK_LINKEDIN),
SMLink(logo: 'facebook', link: Constants.LINK_FAEBOOK),
SMLink(logo: 'Qwiklabs', link: Constants.LINK_QWIKLABS),
],
)
SOLUTION-2
From the screenshot, the other way to fix would be by making the row scrollable. This makes UI look consistent in your case. We can use ListView widget for this.
ListView(
scrollDirection: Axis.horizontal,
children:
[
SMLink(logo: 'Medium', link: Constants.LINK_MEDIUM),
SMLink(logo: 'linkedin', link: Constants.LINK_LINKEDIN),
SMLink(logo: 'facebook', link: Constants.LINK_FAEBOOK),
SMLink(logo: 'Qwiklabs', link: Constants.LINK_QWIKLABS),
],
)

Related

How can I apply notch curve to Widget?

I have a design as you can see below and the question is how can I apply this notch effect on Arrow ? Is there any easy way to do it just like in Floating Action Button ? Thanks in advance.
I have created Dartpad, please look into this and do let me know if you need any help.
Dartpad Link : https://dartpad.dev/flutter?9bd55396e067e71a839851e18905f478
Code:
Padding(
padding: const EdgeInsets.all(16.0),
child: SizedBox(
height: 70,
child: Stack(alignment: Alignment.centerRight, children: [
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Container(
height: 70,
decoration: BoxDecoration(
color: Colors.cyan, borderRadius: BorderRadius.circular(8)),
),
),
Container(
width: 40,
height: 65,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.white, width: 5)),
child: FloatingActionButton(
onPressed: () {},
backgroundColor: Colors.cyan,
child: const Icon(Icons.chevron_right),
),
)
]),
),
);
Output:
Hey you can achieve it in 2 way
1st one and simple - Create a box decoration widget and a circle shape widget with white border and put these together with Stack and fit as per your requirements.
2nd use Custom Clipper or Custom Paint and draw your shape.

Need Text to stay within the width of the image and wrap around

I have a column inside a listview. The column has image at the top and text in the bottom. I would like the text to stay within the width of the image and wrap. Right now the text does not wrap. Here is how the image and text in the column look like. Any body know how to do that?
Thanks in advance.
Here is the code of the column:
Row(
children: [
Container(
alignment: Alignment.centerRight,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(12),
border: Border.all(width: 1, color: AppColors.pink2),
),
child: Column(
children: [
ConstrainedBox(
constraints:
BoxConstraints(maxHeight: 300, maxWidth: width - 50),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image(
image: MemoryImage(chatMessageModel.rawData!),
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.all(14),
child: Text('I need this text to wrap around'),
),
],
),
),
],
)
You can use Flexible with AutoSizeText Widget to make it possible.
AutoSizeText Widget align your text size by corresponding your screen size
Flexible Widget adjust your text position
Working code :
Flexible(
child: AutoSizeText(
'I need this text to wrap around',
style: const TextStyle(
fontWeight: FontWeight.w400,
color: Color.fromRGBO(0, 0, 0,
1), //rankingNoColors(index)
fontSize: 24.0,
fontStyle: FontStyle.normal,
),
),
);

How to stack two icons one on top of the other?

I have two icons in a stack. I want one to hide the other, but instead it's semi-transparent and the other is shown behind it.
This is the code:
Stack(
children: [
Container(child: Icon(Icons.account_circle_rounded), padding: EdgeInsets.only(left: 10),),
Container(child: Icon(Icons.account_circle_rounded), padding: EdgeInsets.only(left: 20),),
],
)
This is how it looks:
Icons are more like SVG, use vector to draw shape. There are some empty spaces inside the icon to draw the path, and we can see the background though this. Also, it contains padding around it, You can use your assets to draw the UI.
We can wrap with another ColoredBox to hide the background icon, but it will get extra padding from IconData.
This snippet shows basic level structure.
SizedBox(
height: 24,
width: 24 * 1.7,
child: Stack(
alignment: Alignment.center,
children: [
Align(
alignment: Alignment.centerLeft,
child: Container(
child: const Icon(
Icons.account_circle_rounded,
),
decoration: const BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
),
),
Align(
alignment: Alignment.centerRight,
child: Container(
child: const Icon(
Icons.account_circle_sharp,
),
decoration: const BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
),
),
],
),
),
Widget renders bottom to top.

How do i add shadow or bottom margin in dropdownlist in flutter?

I want to add shadow or margin to the bottom of text so user can see distinctly different tiles in it. How do i go about that?
You can add the text as a child to a Container and then set it's border according to your need.
DropdownMenuItem(
child: Container(
child: Row(
children: <Widget>[
Icon(Icons.exit_to_app),
SizedBox(
width: 8,
),
Text('Logout'),
],
),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black87,
width: 1.0,
),
),
),
),
);
Dartpad link. Output:

How to handle overflow of a Row inside a Column inside a Stack in Flutter?

I'm trying to create a simple stack list that for a specific designer reason has the following structure:
Text inside a Padding inside a Row inside a Column inside a Container inside a Positioned inside a Stack inside a Card (although I assume the Card part is irrelevant to the question).
The problem is as follows: once the String inside the Text Widget exceeds a certain amount of characters, it overflows, as shown in the image below:
I've tried wrapping the Text, Row, Column Widgets and so on with a Flexible (or Expanded) Widget, but I get an error message saying "Flexible widgets must be placed inside Flex widgets". I'm pretty sure I lack some knowledge on the Widgets that I'm using, but that knowledge I have not been able to find on the flutter dev website, therefore I have decided to post the question here. This is the code for the Card item in which the Stack is located (I'm not pasting the whole class since other methods and the constructor aren't required to duplicate the error in question and I don't want people to get confused by reading code unrelated to the question):
#override
Widget build(BuildContext context) {
return Card(
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 20.0),
decoration: BoxDecoration(
border: Border.all(width: 3.5, color: Colors.black87),
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: this.image ??
AssetImage(
'images/rolph.jpg',
),
),
),
),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border(
top: BorderSide(width: 3.0, color: Colors.black),
bottom: BorderSide(width: 1.0, color: Colors.black),
right: BorderSide(width: 1.0, color: Colors.black),
left: BorderSide(width: 1.0, color: Colors.black)),
),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 2.0),
child: Text(
this.name,
overflow: TextOverflow.clip,
style: TextStyle(fontSize: 18.0),
),
)
],
),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 2.0, left: 3.0),
child: Text(
this.email,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 16.0),
),
)
],
)
],
),
),
),
_shouldIShowNotificationBubble(),
Positioned(
left: 0.0,
right: 0.0,
top: 0.0,
bottom: 0.0,
child: Container(
color: Colors.transparent,
child: InkWell(
highlightColor: Colors.deepPurple,
onTap: this.onTap ??
() {
debugPrint("Ink is, well, clicked.");
},
),
),
)
],
),
color: Colors.white,
elevation: 4.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
);
}
I hope anyone's got an answer to my problem since I have not been able to find an adequate one on the internet anywhere. Thanks in advance to everyone who participates in answering!
So after a bit of experimenting, the thing that is needed to be done here is to put the Padding widget (the one directly above the problematic widget and directly under the Row (or Column) inside a Flexible (or Expanded) widget. Along with that, what can and should be added to the Text widget is the overflow behavior, such as the following solution:
Row(
children: <Widget>[
Flexible(
child: Padding(
padding: EdgeInsets.only(left: 2.0),
child: Text(
this.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 18.0),
),
),
)
],
Hope this helps others who have stumbled upon the same error. ^_^