How to overlay Widget over background widget in flutter? - flutter

I want to overlay a add to cart button on background restaurants list, Overlay card widget on container explains it can be done by Stack but I tried and still not getting how to do it. Kindly help me to know how can I do it?
Expected Design
Result Image
detailpage.dart
#override
Widget build(BuildContext context) {
return Scaffold(
body: Wrap(
children: <Widget>[
Container(...),
Container(...),
Stack(
children: <Widget>[
Center(
child: MaterialButton(
onPressed: () {},
textColor: Colors.white,
padding: const EdgeInsets.all(0.0),
child: Container(
width: 88,
height: 30,
decoration: BoxDecoration(
color: Color(0xff00D99E),
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
blurRadius: 8,
offset: Offset(0, 15),
color: Color(0xff00D99E).withOpacity(.6),
spreadRadius: -9)
]),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/icon-chat-notification.png'),
SizedBox(width: 6),
Text("CART",
style: TextStyle(
fontSize: 10,
color: Colors.white,
letterSpacing: 1,
))
],
),
),
),
)
],
)
],
),
bottomNavigationBar: bottomNavigationBar,
);
}

The problem is that your Stack only composes the Button itself instead of the entire view, you use it to put the Text and the Icon on top of the MaterialButton. Side note: that's actually not necessary, you can just put the text and the icon directly into the MaterialButton and get rid of the Stack.
To solve your problem you have to put your button and your List onto the same Stack (your button can still stay a Stack itself but I don't encourage you to do that).
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand
children: <Widget>[
Container(...),
// Use Positioned around your List to expand it in all dimensions
Positioned(
bottom: 0,
top: 0,
left: 0,
right: 0,
child: Container(...), // This is your container with the list
)
// Position the button at the bottom
Positioned(
bottom: 0,
child: Stack(...), // This is your button
)
],
),
bottomNavigationBar: bottomNavigationBar,
);
}

Related

Flutter 1 positional argument expected, but 0 found

I have created a page with a side drawer which when pressed on the magnifying glass icon opens search requirements for different recipes e.g vegetarian, gluten free etc. I want the recipes to be shown on the page once the side drawer is closed.
I have created a widget called RecipeList() which has all of the code to retrieve the recipes from API and works perfectly on its own page without a side drawer. I wish to call this RecipeList() widget so that when the side drawer is closed they are displayed.
I'm having an error on the 7th line from the bottom, calling the RecipeList() widget it says "1 positional argument(s) expected, but 0 found.Try adding the missing arguments." I feel like I'm missing something that may be pretty obvious to fresh eyes?
Please see this images as references in case I didn't explain it well enough :)
HOW I WISH FOR THE RECIPES TO BE DISPLAYED WHEN THE SIDE BAR IS CLOSED (THIS WORKS ON A PAGE WITHOUT THE SIDE BAR):
CURRENTLY HOW THE SIDE BAR OPENS:
THE PAGE I WISH FOR THE RECIPES TO BE DISPLAYED ON:
//SIDE DRAWER BUILD
Widget build (BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
body: SwipeDrawer(
radius: 20,
key: drawerKey,
hasClone: false,
bodyBackgroundPeekSize: 30,
backgroundColor: Colors.white,
drawer: buildDrawer(),
child: buildBody(),
),
);
}
//WIDGET RESPONSIBLE FOR SHOWING RECIPES FROM API
Widget RecipeList(BuildContext context) {
return Scaffold(
body:
mapResponse==null?Container(): SingleChildScrollView(
child:Column(
children: <Widget> [
ListView.builder(
//SHRINKWRAP ADJUSTS THE BOXES TO FIT THE SCREEN
shrinkWrap: true,
itemBuilder: (context,index){
return Container(
//RECIPE PAGE LAYOUT DESIGN
//EACH RECIPE IS SHOWN WITH ITS TITLE & CALORIES IN A LITTLE RECTANGLE WITH IMAGE
//SETTING UP EACH RECIPE RECTANGLE PLACE HOLDER
margin: EdgeInsets.symmetric(horizontal: 22, vertical: 10),
width: MediaQuery.of(context).size.width,
height: 180,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.6),
offset: Offset(0.0,10.0,),
blurRadius: 10.0,
spreadRadius: -6.0,
),
],
//EACH RECIPES IMAGE GOES HERE
image: DecorationImage(
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.35),
BlendMode.multiply,
),
//IMAGE IS FETCHED FROM API
image: NetworkImage(listOfResults[index]['image']),
fit: BoxFit.cover,
),),
//EACH RECIPES TITLE IS HERE
child: Stack(
children: [
Align(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: Text(
//TITLE IS FETCHED FROM API
listOfResults[index]['title'],
style: TextStyle(
fontSize: 19,
color: Colors.white.withOpacity(1.0)
),
overflow: TextOverflow.ellipsis,
maxLines: 2,
textAlign: TextAlign.center,
),
),
alignment: Alignment.center,
),
//EACH RECIPES CALORIES IS HERE
Align(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
padding: EdgeInsets.all(5),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.4),
borderRadius: BorderRadius.circular(15),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Icon(Icons.star,color: Colors.yellow,size: 18),
SizedBox(width: 7),
Text(
//CALORIES IS FETCHED FROM API
listOfResults[index]['spoonacularScore'].toString(),
style: TextStyle(
color: Colors.white.withOpacity(1.0)),
),
],
),
),
Container(
padding: EdgeInsets.all(5),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.4),
borderRadius: BorderRadius.circular(15),
),
child: Row(
children: [
Icon(
Icons.schedule,
color: Colors.yellow,
size: 18,
),
SizedBox(width: 7),
Text(listOfResults[index]['readyInMinutes'].toString(),
style: TextStyle(
color: Colors.white.withOpacity(1.0)),),
],
),
)
],
),
alignment: Alignment.bottomLeft,
)]
) );
},
//IF THE LIST OF RESULTS RETRIEVED FROM API IS NONE, ITEMCOUNT IS EQUAL TO 0. OTHERWISE ITEMCOUNT IS EQUAL TO LENGTH OF THE LIST OF RESULTS
itemCount: listOfResults==null ? 0 : listOfResults.length)
],
),
));
}
//DISPLAY RECIPES ON PAGE SCREEN FROM API SEARCH
Widget buildBody() {
return Column(
children: [
// build your appBar
AppBar(
title: Text('Recipes'),
leading: InkWell(
onTap: () {
if (drawerKey.currentState.isOpened()) {
drawerKey.currentState.closeDrawer();
} else {
drawerKey.currentState.openDrawer();
}
},
child: Icon(Icons.search)),
),
//MAIN SCREEN
Expanded(
child: Container(
color: Colors.white,
child: RecipeList(),
),
),
],
);
}
}
The error explanation is that you defined RecipeList like this:
Widget RecipeList(BuildContext context)
And you call constructor like this:
child: RecipeList(),
As you pass no parameter you got this error, you need to suppress positionnali parameter in Widget définition or pass parameter when you instanciate Widget.

How to focus on spesific section on keyboar pops up in flutter

I'm bit more thinking a bout UX in this app and wandering how I can achieve below behavior in flutter. so far what I have done is just making sections using Rows, Columns and Expanded widgets. but when the keyboard opens it overlaps content. I have my custom widgets so its simplified as below.
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
flex: 6,
child: Container(
// Image goes here
),
Expanded(
flex: 5,
child: Container(
child: Column(
children: [
Container(
margin: EdgeInsets.only(left: 20, right: 20),
padding: EdgeInsets.only(left: 20, right: 20, top: 5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.1),
spreadRadius: 5,
blurRadius: 8,
offset:
Offset(0, 4), // changes position of shadow
),
],
),
child: Column(
children: [
// Form goes here
],
),
),
Button(
label: "Sign In",
)
],
),
),
),
Expanded(
flex: 2,
child: Button(
label: "Don’t have an account? Sign Up",
type: 'text',
align: 'center'),
),
],
),
));
I did something similar, with FocusScope.of(context).hasFocus to detect if keyboard is showing or not and change my UI accordingly.
Found the solution. Just needs to add reverse: true to SingleChildScrollView then it scroll down the SingleChildScrollView to bottom when focus on TextField
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFF9FDFF),
body: SafeArea(
child: SingleChildScrollView(
reverse: true, // <---- Add this
child: Column(
child: // Your content
),
),
),
);

Custom BottomNavigationBar in Flutter

I tried to implement a custom bottom navigation bar. I use a pageview to navigate between the pages. Now the bottom navigation bar is not absolutely above the page view but cuts off part of the page view. Can anyone help me here?
Here is the associated code:
Scaffold buildHomePage() {
return Scaffold(
body: PageView(
children: <Widget>[
PopularScreen(),
DiscoverScreen(),
ActivityScreen(),
ProfileScreen()
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: buildBottomNavigationBar(),
);
}
Widget buildBottomNavigationBar() {
return BottomAppBar(
child: Container(
height: 65,
margin: EdgeInsets.only(left: 30, bottom: 40, right: 30),
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 5,
)
],
color: Colors.white,
borderRadius: BorderRadius.circular(50),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: items.map((item) {
var itemIndex = items.indexOf(item);
return GestureDetector(
onTap: () => onTap(itemIndex),
child: _buildNavBarItem(item, pageIndex == itemIndex),
);
}).toList(),
),
),
);
}
You can use the Stack Widget to position the your BottomNavigationBar on top of the PageView:
Scaffold(
body: Stack(
children: <Widget>[
PageView(...),
Align(
alignment: Alignment.bottomCenter,
child: buildBottomNavigationBar(pageController),
),
],
),
);
Then you can remove the BottomAppBar from the buildBottomNavigationBar() or set
the elevation property to 0 and the color to Colors.transparent:
BottomAppBar(
color: Colors.transparent,
elevation: 0,
child: ...
);
Result:
Your answer is Stack class, which will help you build your BottomNavigationBar on top of your PageView
Basic layout of the Page
Stack(
children: [
PageView(
....
)
Positioned(
top: ..,
left: ..,
bottom: ..,
right: ..,
child: BottomNavigationBar()
)
]
)
If that make sense to you

Widget which adjusts to the children size?

Is there a widget in Flutter which adjusts itself on the content of it's children which are in Rows and Columns?
I can't seem to find anything related, and I am constantly hit with infinite overflows errors.....
As an example, lets say I would have 3 Widgets in a Row, 1 of which is a text, which I cannot guess how long it will be, but I need to expand the parent as much as the widget needs and at the sametime wrap the text
I would have something like:
Row(
children: [
Expanded(child: Widget1(....)),
Expended(child: Text(.....)),
Expanded(child: Widget2(....))
]
)
Right?
I would want the parent of the row to expand as much as it needs for the children to be shown, I want to align Widget1 and Widget2 in different ways (1-center, 2-bottom).
If I put them in Column I can center Widget1, but I cannot put Widget2 at the bottom because if I put Column to max size it overflows, if I don't it doesn't align...
How would you do it?
I really don't understand the logic behind the inifnite layout thing....
For example, here is where I am stuck at the moment.
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Global.findCategory(event.categId).color.withAlpha(150),
blurRadius: 20,
spreadRadius: -20,
offset: Offset(0, 5)
)
],
color: Colors.white,
borderRadius: BorderRadius.circular(30)
),
child: Row(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: Global.scaleSmallDevice(context) * 64,
height: Global.scaleSmallDevice(context) * 64,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.cover,
image: event.image
)
)
),
)
,
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CustomText(
text: title,
color: Global.findCategory(event.categId).color,
fontSize: 16,
),
Flexible(
child: CustomText(
text: shortInfo,
color: Global.findCategory(event.categId).color,
fontSize: 12,
),
)
],
),
),
Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Align(
alignment: Alignment.bottomCenter,
child: StatefulBuilder(
builder: (context, setState) {
IconData buttonIcon = (event.favorite) ? Icons.favorite : Icons.favorite_border;
return SplashButton(
splashColor: Global.findCategory(event.categId).color,
child: Icon(buttonIcon, color: Global.findCategory(event.categId).color, size: 30),
onTap: () async {
await event.setFavorite(context);
setState(() {
buttonIcon = (event.favorite) ? Icons.favorite : Icons.favorite_border;
});
}
);
},
),
),
],
),
),
],
);
}
In this case I want the StatefulBuilder widget to be on the bottom and the entire container be as small as the column with the CustomText widgets.
The widget with image inside I want it to be centered, while the text to be fully available to the user.
If I don't add a Column as Container, it goes as the height allows it and the StatefulBuilder widget center itself at the bottom, in the curent code it's at the center.....
Sorry if I'm not coherent, it's a bit late and I am tired after coding all day and stuck at this widget for a few hours.

Expand widgets inside the Stack widget

I have a stack widgets with two widgets inside.
One of them draws the background, the other one draws on top of that.
I want both widgets to have the same size.
I want the top widget, which is a column, to expand and fill the stack vertically to the size that is set by the background widget.
I tried setting the MainAxis mainAxisSize: MainAxisSize.max but it didn't work.
How can I make it work?
Use Positioned.fill
Stack(
children: [
Positioned.fill(
child: Column(
children: [
//...
],
),
),
//...
],
);
More info about Positioned in Flutter Widget of the Week
How does it work?
This is all about constraints. Constraints are min/max width/height values that are passed from the parent to its children. In the case of Stack. The constraints it passes to its children state that they can size themselves as small as they want, which in the case of some widgets means they will be their "natural" size. After being sized, Stack will place its children in the top left corner.
Positioned.fill gets the same constraints from Stack but passes different constraints to its child, stating the it (the child) must be of exact size (which meant to fill the Stack).
Positioned.fill() is the same as:
Positioned(
top: 0,
right: 0,
left: 0,
bottom:0,
child: //...
)
For even more examples and info: How to fill a Stack widget and why? and Understanding constraints.
You can try wrapping the Column with a positioned widget. Set top to 0 and bottom to 0. This will make the column fill the stack vertically.
sample code:
Stack(
children: <Widget>[
// Background widget rep
Container(
color: Colors.pink,
),
Positioned(
top: 0,
bottom: 0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('madonna'),
Text('flutter'),
],
),
)
],
)
I think you could wrap you column in a container like this:
Container(
height: double.infinity,
width: double.infinity,
child: Column()
),
The accepted answer did not answer my issue completely. So, I put my code here in case you have a similar issue.
class NoContentView extends StatelessWidget {
const NoContentView({Key? key, required this.message}) : super(key: key);
final String message;
#override
Widget build(BuildContext context) {
return Expanded(
child: Stack(
children: [
Positioned.fill(
child: FittedBox(
child: const Image(image: AssetImage(AppImages.noReceiptsWeb), fit: BoxFit.fitWidth),
),
),
MessageView(message: message),
],
),
);
}
}
In my case, I thought that I needed to expand the container in the light blue color with an expanded widget inside the stack widget.
Problems arose, especially since I was adding the stack widget in the column widget, and the way to make the container double.infinity dimensions I definitely had a problem with the vertical axis.
In the end, I managed to show it like this photo...
Column(
children: [
//filter
Container(
color: orange,
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Text(
'All',
style: getRegular(color: white.withOpacity(.6)),
),
),
Container(
color: white.withOpacity(.6),
width: 1,
height: 50,
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 10, 0),
child: Text(
'Ready',
style: getRegular(color: white.withOpacity(.6)),
),
),
freeh(),
Container(
color: white.withOpacity(.6),
width: 1,
height: 50,
),
],
),
Row(
children: [
Icon(
filter,
color: white.withOpacity(.7),
size: 20,
),
Text(
'FILTER',
style: getLight(
color: white.withOpacity(.7),
),
),
freeh(),
],
),
],
),
),
Expanded(
child: Stack(
alignment: Alignment.topLeft,
children: [
Container(
color: lightBlue, //here to use map>>>>
),
Container(
height: 35,
width: 120,
margin: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: primary,
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(
FontAwesomeIcons.listUl,
color: white.withOpacity(.8),
size: 20,
),
Text(
'List View',
style: getRegular(color: white.withOpacity(.8)),
)
],
),
),
],
),
)
],
),