Flutter: How to attach a FloatingActionButton to the AppBar? - flutter

The Scaffold-Widget only allows to place a FloatingActionButton at the bottom right or the bottom center. How can I place it between AppBar and body like here?
[

It is now possible using:
Scaffold(
appBar: AppBar(title: Text('Title'),),
body: ...,
floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
floatingActionButton: FloatingActionButton(...),
),

Siva Kumar gave nice example, but the second have of the FAB was not clickable. So I made some changes in the code and it works great now!
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double appBarHeight = 200.0;
#override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
new Scaffold(
appBar: new PreferredSize(
preferredSize: new Size(MediaQuery.of(context).size.width, appBarHeight),
child: new Container(
color: Colors.blue,
child: new Container(
margin:const EdgeInsets.only(top: 30.0),
child: new Column(children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Row(
children: <Widget>[
new IconButton(
icon: new Icon(
Icons.arrow_back,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context, false);
}
),
new Text(
"Controller Name",
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white
),
),
],
),
],
),
]
)
)
)
),
body: new Center(
child: new Text('Hello!'),
),
),
new Positioned(
child: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
print('FAB tapped!');
},
backgroundColor: Colors.blueGrey,
),
right: 10.0,
top: appBarHeight - 5.0,
)
],
);
}
}

we can Achieve it by using stack
and code snippet look like this
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new PreferredSize(
preferredSize: new Size(MediaQuery.of(context).size.width, 200.0),
child:
new Stack(
alignment: const FractionalOffset(0.98, 1.12),
children: <Widget>[new Container(
color: Colors.blue,
child: new Column(
children: <Widget>[
new Container(
margin: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
child: new Column(children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Row(
children: <Widget>[
new IconButton(
icon: new Icon(
Icons.arrow_back,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context, false);
}),
new Text(
"Controller Name",
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white),
),
],
),
],
),
]))
],
)
),new FloatingActionButton(onPressed: (){print("floating button Tapped");},child: new Icon(Icons.add),)],)
),
body: new Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child:
new Container(),
),
);
}
[![output][1]][1]}

for me the solution was, set floatingActionButtonLocation to FloatingActionButtonLocation.endTop
Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.endTop)

Related

Message on any screen

I want to show the snack bar or Dialog on any screen in the Flutter app, anyone knows the way for that ?
For example.. let's say, I receive a notification when the user is in-app, on any screen in-app. How can I display the snack bar message in that situation regardless of which screen the user is currently on?
You can do something like that :
pip_flutter: ^0.0.3
Example:
import 'package:flutter/material.dart';
import 'package:pip_flutter/pipflutter_player.dart';
import 'package:pip_flutter/pipflutter_player_configuration.dart';
import 'package:pip_flutter/pipflutter_player_controller.dart';
import 'package:pip_flutter/pipflutter_player_data_source.dart';
import 'package:pip_flutter/pipflutter_player_data_source_type.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Picture in Picture Mode'),
),
body: Center(
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => PictureInPicturePage()));
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Center(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Text(
'Picture in Picture Mode',
style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),
),
),
),
),
),
),
);
}
}
class PictureInPicturePage extends StatefulWidget {
#override
_PictureInPicturePageState createState() => _PictureInPicturePageState();
}
class _PictureInPicturePageState extends State<PictureInPicturePage> {
late PipFlutterPlayerController pipFlutterPlayerController;
final GlobalKey pipFlutterPlayerKey = GlobalKey();
#override
void initState() {
PipFlutterPlayerConfiguration pipFlutterPlayerConfiguration =
const PipFlutterPlayerConfiguration(
aspectRatio: 16 / 9,
fit: BoxFit.contain,
);
PipFlutterPlayerDataSource dataSource = PipFlutterPlayerDataSource(
PipFlutterPlayerDataSourceType.network,
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
);
pipFlutterPlayerController =
PipFlutterPlayerController(pipFlutterPlayerConfiguration);
pipFlutterPlayerController.setupDataSource(dataSource);
pipFlutterPlayerController
.setPipFlutterPlayerGlobalKey(pipFlutterPlayerKey);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Picture in Picture player"),
leading: IconButton(onPressed: (){
Navigator.of(context).pop();
}, icon: const Icon(Icons.arrow_back_ios,color: Colors.white,)),
),
body: Column(
children: [
const SizedBox(height: 20),
Flexible(
flex: 1,
fit: FlexFit.loose,
child: AspectRatio(
aspectRatio: 16 / 9,
child: PipFlutterPlayer(
controller: pipFlutterPlayerController,
key: pipFlutterPlayerKey,
),
),
),
Container(
margin: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Center(child: Text("Show PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () {
pipFlutterPlayerController
.enablePictureInPicture(pipFlutterPlayerKey);
},
),
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: Center(child: const Text("Disable PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () async {
pipFlutterPlayerController.disablePictureInPicture();
},
),
],
),
),
],
),
);
}
}

Prevent Row from Overflowing Container in Flutter

I am attempting fit a row of widgets to a container, but it seems to overflow when i set the property of the mainaxisalignment to MainAxisAlignment.spaceBetween. This causes the row (font awesome icon) to overflow the width of the container. I'm not sure how to fit the row to the width of the container. I am getting render errors when trying to wrap the row with an expanded widget.
Not sure where i am going wrong.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
//clipBehavior: Clip.none,
color: Colors.blue,
child:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Hello'
),
Icon(FontAwesomeIcons.solidMoneyBillAlt)
],
),
),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Current result:
Better answer use FaIcon widget Instead of Icon widget
Row(
// mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Hello'),
FaIcon(
FontAwesomeIcons.solidMoneyBillAlt,
size: 50,
)
],
)
Try like this
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text('Hello'),
),
Container(
width: 50,
child: Icon(
FontAwesomeIcons.solidMoneyBillAlt,
// size: 30,
),
)
],
)
SAmpleCode
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
//clipBehavior: Clip.none,
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text('Hello'),
),
Container(
width: 50,
child: Icon(
FontAwesomeIcons.solidMoneyBillAlt,
// size: 30,
),
)
],
),
),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Try below code hope its help to you. Add your icon with padding.
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Hello'),
Padding(
padding: EdgeInsets.only(right: 12.0),
child: Icon(Icons.money),
),
],
),
),
),
Result Screen->

flutter bottomsheet with overlay button on top

Can someone give guidance on how to create a bottomsheet with a button on top like below?
I think typically we should be able to achieve it using a stack widget. But not sure how to do this with a bottomsheet. Thanks for your help.
Here is code:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test Bottom Sheet app',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo '),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: Text("Body"),),
bottomSheet:getBottomSheet() , //Main Center
); //Scaffold
}
Widget getBottomSheet(){
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Stack(
children: [
Padding(
padding: const EdgeInsets.only(top: 30, left: 8.0, right: 8.0),
child: Card(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.only(top:20.0),
child: SizedBox(height: 50, child: Text("Postpone Start")),
),
SizedBox(height: 50, child: Text("Postpone Start")),
SizedBox(height: 50, child: Text("Postpone Start")),
],),
),
),
),
Row(
children: [
Card(
color: Colors.orange,
child: FlatButton(onPressed: (){}, child: Text("Header"))),
],
),
],
),
],
);
}
}

How can I change the Size of the standard hamburger menu icon of a drawer in Flutter via theme

While creating the theme:
appBarTheme: new AppBarTheme(
iconTheme: base.appBarTheme.iconTheme.copyWith(size: 100, color: Colors.red),
//Color is working but size having no effect*
),
The easiest way to change your hamburger menu is by changing your leading widget from the AppBar
final _scaffoldKey = new GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.menu, size: 40), // change this size and style
onPressed: () => _scaffoldKey.currentState.openDrawer(),
),
title: Text('Home Screen'),
),
body: Container(),
drawer: Container(
width: 300,
color: Colors.white,
),
);
}
This change will create this huge hamburger menu:
import 'dart:math';
import 'package:app_with_bloc/util/MyScreenUtil.dart';
import 'package:flutter/material.dart';
class AppBar extends StatefulWidget implements PreferredSizeWidget {
GlobalKey scaffoldKey;
// AppBar(this.scaffoldKey, {Key key}) : preferredSize = Size.fromHeight(kToolbarHeight), super(key: key);
AppBar(this.scaffoldKey, {Key key})
: preferredSize = Size.fromHeight(max(MyScreenUtil().getScaledSize(kToolbarHeight), kToolbarHeight)),
super(key: key);
#override
final Size preferredSize; // default is 56.0
#override
_AppBarState createState() => _AppBarState();
}
class _AppBarState extends State {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: SafeArea(
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [
Padding(
padding: EdgeInsets.only(bottom: MyScreenUtil().getScaledSize(4.0), left: MyScreenUtil().getScaledSize(4.0), ),
child: IconButton(
icon: Icon(Icons.menu, size: MyScreenUtil().getScaledSize(26), color: Colors.white), // change this size and style
onPressed: () => widget.scaffoldKey.currentState.openDrawer(),
),
),
Padding(
padding: EdgeInsets.all(MyScreenUtil().getScaledSize(8.0)),
child: Text('Start', style: Theme.of(context).textTheme.title.apply(fontSizeFactor: 1.4, color: Colors.white)),
),
Padding(
padding: EdgeInsets.only(bottom: MyScreenUtil().getScaledSize(4.0), right: MyScreenUtil().getScaledSize(4.0), ),
child: IconButton(
icon: Icon(Icons.menu, size: MyScreenUtil().getScaledSize(26), color: Colors.white),
),
),
]
),
),
);
}
}

Creating a Custom widget in Flutter

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
int _weight =60;
class RoundIconData extends StatefulWidget {
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: Icon(icon),
onPressed: (){
setState(() {
if(icon == FontAwesomeIcons.minus){
_weight--;
}
else{
_weight++
}
});
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
i am getting error while creating this.
What i Want
Custom widget with RawmaterialButton through which i can add icons.
if i add icon.minus then my given private weight wants to be decremented
else
given private weights to be incremented
You can copy paste run full code below
You have to move the following code to RoundIconData
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
and pass callback for refresh
working demo
full code
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
int weight = 60;
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
refresh() {
setState(() {});
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RoundIconData(
icon: Icon(FontAwesomeIcons.minus),
notifyParent: refresh,
),
RoundIconData(
icon: Icon(Icons.add),
notifyParent: refresh,
),
Text(
'${weight}',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class RoundIconData extends StatefulWidget {
final Icon icon;
final int pressme;
final Function() notifyParent;
RoundIconData(
{#required this.icon,
#required this.pressme,
#required this.notifyParent});
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: widget.icon,
onPressed: () {
print(widget.icon.toString());
print(Icon(FontAwesomeIcons.minus).toString());
if (widget.icon.toString() == Icon(FontAwesomeIcons.minus).toString()) {
weight--;
widget.notifyParent();
print(weight);
} else {
weight++;
widget.notifyParent();
print(weight);
}
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
Creating a Custom widget in Flutter
import 'package:flutter/material.dart';
class WelcomePage extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Auth',
theme: ThemeData(
primaryColor: Colors.purple,
scaffoldBackgroundColor: Colors.white,
),
home:Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"WELCOME TO XYZ",
style: TextStyle(fontWeight: FontWeight.bold,color: Colors.purple,fontSize: 25),
),
Padding(
padding: const EdgeInsets.only(right: 40),
child: Image.asset(
"assets/images/food_order.png",
height: 200,
),
),
SizedBox(height: 10 ),
loginMethod(),
signUpMethod(),
],
),
),
),
),
);
}
// Login Button Method Widget
Widget loginMethod(){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
),
),
);
}
// Signup button method widget
Widget signUpMethod (){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Sign up',
style: TextStyle(color: Colors.white),
),
),
),
);
}
}