Unable to resize widgets inside appbar in flutter - flutter

The size of my container is not changing when I am using it inside App bar. I tried wrapping it inside a sizedBox even then it's not working as expected. And not only container , I am unable to resize any of the buttons inside appbar.

You can simply apply Transform or Icon Widget in Flutter to fix this issue.
Wrapping as a child widget (Container, IconButton...) inside Transform.scale().
Scale of the Icon widget can be easily adjusted by using its size property.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Transform.scale(
scale: 0.5, // effect to size of child widget
child: Container(
width: 5,
height: 5,
color: Colors.amber,
),
),
title: Transform.scale(
scale: 1.7, // effect to size of child widget
child: IconButton(
onPressed: () {},
icon: SvgPicture.asset("assets/icons/back_button.svg"),
iconSize: 20,
),
),
centerTitle: true,
actions: [
Icon(
Icons.person_off_outlined,
size: 32, // effect to size of Icon widget
),
]),
body: Container(),
);
}
Result Mobile view of 'Appbar'

Related

ListView containing a multiline Textfield followed by ListTiles in flutter

The goal is to have a ListView that contains a multiline texfield (with an arbitrary number of lines, maxLines=null) that is followed by several ListTiles. When lines are added to the TextField, it grows and the ListTiles should move accordingly. However, there is an unexpected behaviour with the following code:
#override
Widget build(BuildContext context) {
ListView l = new ListView(children: [
TextField(maxLines: null),
SizedBox(height: 50, child: ColoredBox(color: Colors.yellow,child: Text("Tile1"),) ),
ListTile(tileColor: Colors.blueGrey,title: Text("Tile1"),),
ListTile(tileColor: Colors.blueGrey,title: Icon(Icons.looks_two_rounded),)
]);
return Scaffold(backgroundColor: Color(0xdcdcffff), body: Center(child: l));
}
https://gfycat.com/fr/obviousshorttermchihuahua
The green colored box moves down at expected but the ListTiles do not (although their children do), until I scroll, then they move where they should've been.
Is there any way to solve this ?
I don't know what exactly caused the bug, but I found a workaround that could perhaps be useful to someone trying to do the same: I put the ListTile in transparent inside a Colored box:
#override
Widget build(BuildContext context) {
ListView l = new ListView(children: [
TextField(maxLines: null),
SizedBox(
height: 50,
child: ColoredBox(
color: Colors.green,
child: Text("Tile1"),
)),
ColoredBox(
color: Color(0x77ff0000),
child: ListTile(
tileColor: Colors.transparent,
title: Text("Tile1"),
),
),
ColoredBox(
color: Color(0x77ff0000),
child: ListTile(
tileColor: Colors.transparent,
title: Icon(Icons.looks_two_rounded),
)),
]);
return Scaffold(body: Center(child: l));
}
https://gfycat.com/fr/idioticquarrelsomegossamerwingedbutterfly

Fixed height in Container is not working in Flutter

Container height is set to fixed 40 but once I'm using that Widget in AppBar() it takes all the possible height. Here is the code for my custom widget which has Fixed height of Container,
class LPBorderButtonWithIcon extends StatelessWidget {
final GestureTapCallback onPressed;
final String text;
final String iconAsset;
final Color textColor;
LPBorderButtonWithIcon(
{#required this.onPressed,
#required this.text,
#required this.textColor,
#required this.iconAsset});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed,
child: Container(
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
border: Border.all(color: Color(0XFFd8dce1))),
child: Row(
children: [
WidthSizedBox(15),
Image.asset(
iconAsset,
height: 14,
width: 14,
),
WidthSizedBox(5),
Text(text,
style: TextStyle(
color: textColor,
fontSize: 12,
fontFamily: "GilroyMedium")),
WidthSizedBox(15),
],
),
));
}
}
and here I'm using LPBorderButtonWithIcon() in this screen,
class CreateRulesScreen extends StatefulWidget {
#override
_CreateRulesScreenState createState() => _CreateRulesScreenState();
}
class _CreateRulesScreenState extends State<CreateRulesScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
brightness: Brightness.light,
backgroundColor: Colors.white,
elevation: 1,
centerTitle: false,
titleSpacing: 0.0,
leading: BackButton(
color: LPColor.primary,
onPressed: () {
Navigator.of(context).pop();
},
),
title: Text(
"Create Rule",
style: LPStyle.titleStyle,
),
actions: [
Container(
margin: EdgeInsets.only(top: 12, bottom: 12, right: 16),
child: LPBorderButtonWithIcon(
onPressed: null,
text: "Create",
textColor: Color(0XFF508ff4),
iconAsset: "images/ic_publish.png",
),
)
],
),
);
}
}
and below is the result where that custom container takes all the possible height. Please let me know how can I set fixed height to my custom widget.
Place your Container inside an Align, Aling will force the container to occupy only the space it needs.
Align(
child: Container(
height: 20,
width: 30,
color: Colors.white,
),
)
The parent widget takes the entire space available to draw the widget, Here Container is the parent widget, and it's taking whatever space is available, so to give height to the Container, that needed to be placed inside any widget which assigns x,y position of widgets to get it to draw.
Container(
height: 40, // Its not going to apply height as it's parent widget
)
So to work out the above code you have to align Container to any other widget like Center, Align, etc.
For Eg:
Scaffold(
body: Container(
height: 600,
color: Colors.red,
child: Container(
height: 200,
color: Colors.yellow,
),
),
);
The above example child container will not draw yellow color in 200 height, it will take the entire 600 height space.
Output:
To Solve this we have assigned some widgets to the child Container so that it will get the x, y position to start drawing the child widget. Here Center widget is used.
Eg:
Scaffold(
body: Container(
height: 600,
color: Colors.red,
child: Center(
child: Container(
height: 200,
color: Colors.yellow,
),
),
),
);
Output:
Some Limitation:
A widget can decide its own size only within the constraints given to
it by its parent. This means a widget usually can’t have any size it
wants.
A widget can’t know and doesn’t decide its own position in the
screen, since it’s the widget’s parent who decides the position of
the widget.
Since the parent’s size and position, in its turn, also depends on
its own parent, it’s impossible to precisely define the size and
position of any widget without taking into consideration the tree as
a whole.
If a child wants a different size from its parent and the parent
doesn’t have enough information to align it, then the child’s size
might be ignored. Be specific when defining alignment.
Reference link: https://flutter.dev/docs/development/ui/layout/constraints

Flutter web vertical tab navigation

How to create vertical tab navigation for dashboard in flutter web and whats it the best way?
You can you a NavigationRail to get this look. It was added to flutter this year. It works almost like the bottom tab bar.
I believe you want something similiar to what is shown in the screenshot right?
If so, I would recommend you to use the Scaffold Widget and making use of the attributes appBar and drawer.
For further information about the Scaffold Widget please check this link.
Here a simple example:
In your main Widget modify the build function like this.
#override
Widget build(BuildContext context) {
GlobalKey<ScaffoldState> key = GlobalKey();
return Scaffold(
key: key,
drawer: Padding(
padding: const EdgeInsets.fromLTRB(0, 70, 0, 0),
child: Container(
color: Colors.red,
width: 300,
child: Column(children: [Text("1"), Text("2"), Text("3")])),
),
appBar: AppBar(
toolbarHeight: 70,
elevation: 5,
centerTitle: true,
backgroundColor: Colors.black,
leading: RawMaterialButton(
child: Icon(Icons.menu),
onPressed: () => key.currentState.openDrawer(),
),
title: Container(child: Text("Title Widget")),
),
body: Container(
child: Text("Main Widget"),
));
}
The result would look like this:

How do I make the AppBar slide from top and overlay the screen content, just like inshorts app bar

I am trying to make the AppBar slide from top and show it on screen tap and then hide after 10 seconds.
I am currently doing it by showing the Appbar based on a boolean variable value and it is working also, but when the AppBar comes, the screen is resized to fit the AppBar. I want that my screen should remain as is and the AppBar just overlay on the screen, Just like a banner.
_showAppBar(bool val) {
setState(() {
if (val) {
_barVisible = true;
print("show");
}
else {
_barVisible = false;
print("hide");
}
});
}
Widget build(BuildContext context) {
return Theme(
data: getCurrentTheme(),
child: Scaffold(
key: _scaffoldKey,
appBar: _barVisible ? buildAppBar():null,
body: buildBody()
));
}
buildAppBar() {
Color accentColor = currentSelectedTheme == MyThemes.defaultLight ? Colors.white : getAccentColor();
return AppBar(
leading: Text(appBarTitle, style: TextStyle(color: accentColor)),
actions: <Widget>[
IconButton(
icon: Icon(
_nightMode ? Icons.lightbulb_outline : Icons.brightness_2
),
padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0),
iconSize: 32.0,
onPressed: () => print("hi")
],
);
}
Scaffold won't help you to achieve what you're looking for. Use the AppBar in a Stack instead.
Try something like this:
#override
Widget build(BuildContext context) {
return Theme(
data: getCurrentTheme(),,
child: Scaffold(
key: _scaffoldKey,
body: Stack(
children: <Widget>[
buildBody(),
/*Below is the new AppBar code. Without Positioned widget AppBar will fill entire screen*/
Positioned(
top: 0.0,
left: 0.0,
right: 0.0,
child: _barVisible ? buildAppBar() : Container(width: 0, height: 0,),
/*You can't put null in the above line since Stack won't allow that*/
)
],
)
),
);
}
Rest of the code will remain same. Let me know if it helps!

flutter move floatingActionButton up 50 pixels

Is it possible to move the floatingActionButton up by 50 pixels?
I have a floatingActionButton in an App that uses firebase_admob and the Ads Banner is overlapping on top of the floatingActionButton.
How does one set the floatingActionButton to be 50 pixels from the bottom of the screen?
From the documentation of floatingActionButton I can not seem to pick out how to position the button.
Wrap your FloatingActionButton inside a Padding and add the size you want:
floatingActionButton: Padding(
padding: const EdgeInsets.only(bottom: 50.0),
child: FloatingActionButton(
child: Icon(Icons.remove),
onPressed: () => null,
),
),
It's simple.
class Test extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(),
floatingActionButton: Align(
child: FloatingActionButton(onPressed: null),
alignment: Alignment(1, 0.7)),
);
}
}
Use Alignment, as everything is a Widget in Flutter.