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.
Related
I am using flutter 3.0.0
In my application I am displaying a native video using platform view.
The video is displaying but it is always displaying on upper left corner and it covers other
widgets even they are in a stack.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Stack(
children: [
Center(
child: ConstrainedBox(
constraints:
const BoxConstraints.expand(height: 200, width: 200),
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child:
const AndroidView(viewType: 'remote-video'),
),
),
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
MethodChannels.coreMethodChannel
.invokeMethod("load");
},
child: const Text('Invoke'),
),
),
],
),
],
),
);
}
This is how it looks when I run the code
As you can see it is displaying over everything.
Can you provide some advice on how to fix this?
As of 31/07/2022 this seems to be a bug in flutter >= 3.0.0. Following the solution in this question Flutter AndroidView Widget I downgraded to 2.10.5 and then it worked as expected. Hopefully the flutter team will resolve it shortly.
I want to reuse mobile app code for flutter web. I already coded with AppBar() and body widgets in scaffold in all screens. Now i am taking 400 width and center for web, it is good except appbar.
Scaffold(
appBar: this.getAppBarWidget(),
body: Center(
child: Container(
width: kIsWeb ? 400.0 : MediaQuery.of(context).size.width,
child: this.getBodyWidget(),
),
))
Above code is perfectly working for all screens of mobile and web except appbar in web.
How do i change width of appbar to fit width 400 ?
If i use Size.fromWidth(400) getting error.
Below code is working for mobile and web.
Scaffold(
body: Center(
child: Container(
width: kIsWeb ? 400.0 : MediaQuery.of(context).size.width,
child: Column(
children: [
this.getCustomAppbarWidget(),
this.getBodyWidget(),
],
),
),
))
Please suggest me.
The size this widget would prefer if it were otherwise unconstrained.
In many cases it's only necessary to define one preferred dimension. For example the [Scaffold] only depends on its app bar's preferred height. In that case implementations of this method can just return new Size.fromHeight(myAppBarHeight).
But we can provide customAppBar like
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
const MyAppBar({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.centerLeft,
child: Container(
alignment: Alignment.center,
color: Colors.pink,
// we can set width here with conditions
width: 200,
height: kToolbarHeight,
child: Text("MY AppBar"),
),
);
}
///width doesnt matter
#override
Size get preferredSize => Size(200, kToolbarHeight);
}
and use
Scaffold(
extendBodyBehindAppBar: true,
appBar: MyAppBar(),
body: ......
if it cover the 1st item of body, and in this case use SizedBox(height: kToolbarHeight) to handle the situation if needed.
Result
As i know, width did not allow in AppBar. Only height is allowed in AppBar
toolbarHeight: 60,
But if you want to apply manually width in your AppBar you can wrap your AppBar in Padding component
return Scaffold(
body: Column(
children: [
Container(
width: kIsWeb? 400 : double.maxFinite,
child: AppBar(
title: Text('hello'),
),
),
Expanded(child: HomePageOne()), // this expanded is you page body
],
),
);
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'
I display a custom-made bottom app bar in a Stack because of keyboard padding reasons. The custom widget is fully opaque as it should be until it's a child of a Stack in which case, the content behind it starts to be visible since the color's opacity somehow changes.
As you can see, it's only the "main" color that's transparent. Icons remain opaque.
This is the build method of my custom BottomBar widget which is then just regularly put into a Stack. I have tried using a Material and even a simple Container in place of the BottomAppBar widget but the results are the same.
#override
Widget build(BuildContext context) {
return BottomAppBar(
color: Colors.blue.withOpacity(1),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(MdiIcons.plusBoxOutline),
onPressed: () {},
),
Text('Edited 11:57'),
IconButton(
icon: Icon(MdiIcons.dotsVertical),
onPressed: () {},
),
],
),
);
}
Can you interact with the BottomAppBar ? It looks like an order problem. Try to put the BottomAppBar as last in the Stack children.
Note that BottomAppBar doesn't have a constant size, if you did not add it to Scaffold bottomNavigationBar named parameter has a size if this is not null. Below is peace of code in Scaffold dart file:
double bottomNavigationBarTop;
if (hasChild(_ScaffoldSlot.bottomNavigationBar)) {
final double bottomNavigationBarHeight = layoutChild(_ScaffoldSlot.bottomNavigationBar, fullWidthConstraints).height;
bottomWidgetsHeight += bottomNavigationBarHeight;
bottomNavigationBarTop = math.max(0.0, bottom - bottomWidgetsHeight);
positionChild(_ScaffoldSlot.bottomNavigationBar, Offset(0.0, bottomNavigationBarTop));
}
You can even develop your own Widget without BottomAppBar but if you want things like centerDocked and things like circular notched, you will have to do more stuff (anyway you have flexibility to custom design the way you want).
Here is a simple example to do that(one way to do that):
import 'package:flutter/material.dart';
class CustomBottomBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 50),
color: Colors.greenAccent, // if you want this color under bottom bar add the margin to list view
child: ListView.builder(
itemCount: 100,
itemBuilder: (_, int index) => Text("Text $index"),
),
),
Positioned(
bottom: 0,
child: Container(
color: Colors.amber.withOpacity(.5),
width: MediaQuery.of(context).size.width,
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(4, (int index) => Text("Text $index")), // you can make these clickable by wrapping with InkWell or any gesture widget
),
),
),
],
),
);
}
}
I want to get the width of a FlatButton in an horizontal ListView but i only can get Width: Infinity.
I can get width in an isolated FlatButton succesfully, but when the FlatButton is inside of a ListView i cannot, but i don't know why. Could you help me to get the width of a FlatButton when it is inside a ListView?
Thanks.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Container(
height: 70.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
_flatButtonStoreWidth(),
_flatButtonStoreWidth(),
_flatButtonStoreWidth(),
_flatButtonStoreWidth(),
_flatButtonStoreWidth(),
],
),
),
);
}
Widget _flatButtonStoreWidth (){
return FlatButton (
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double _width = constraints.maxWidth;
print('Width: $_width');
return Text('This is a FlatButton');
},
),
onPressed: (){},
);
}
To get width of the object in ListView you are supposed to:
Give widget a GlobalKey.
When build is complete, access the context through the GlobalKey.
Using context you can get size by context.size.
You can get width from Size object.
Following is the working code for your reference:
Widget _flatButtonStoreWidth() {
GlobalKey globalKey = GlobalKey();
WidgetsBinding.instance.addPostFrameCallback((_) {
double _width = globalKey.currentContext.size.width;
print('Width: $_width');
});
return FlatButton(
key: globalKey,
child: Text('This is a FlatButton'),
onPressed: () {},
);
}
I hope this helps, in case of any doubt please comment. In case this this answer helps you please accept and up-vote it.
That's because the ListView with a horizontal axis has an infinite width. You'll have to provide a width for each FlatButton.
FlatButton gets its size from the parent. In this case the parent provides an infinite width. Therefore you should size the button yourself