Flutter On Press Problem with Negative Y offset Positioned in a Stack - flutter

I have two widgets in a stack. Which is demonstrated below.
The second widget is a button and it is located in a positioned widget which has a negative Y axis.
The problem is overflowed are is not clickable. I there any way to fix this issue?
Stack(
fit: StackFit.expand,
overflow: Overflow.visible,
clipBehavior: Clip.none,
alignment: AlignmentDirectional.topCenter,
children: [
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(29)),
child: ClipPath(
clipper: NavbarClipper(),
child: Container(
color: Colors.white,
),
),
),
Positioned(
top: -30,
child: Container(
width: context.dynamicHeight(0.16),
height: context.dynamicWidth(0.16),
child: FittedBox(
child: FloatingActionButton(
onPressed: () {},
backgroundColor: Colors.orange,
child: Icon(Icons.ac_unit),
),
),
),
)
],
)
Thanks.

You can wrap rect with padding instead of negative margin to button
class MyHomePage extends StatefulWidget {
MyHomePage();
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Color color = Colors.orange;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 200,
padding: EdgeInsets.all(32),
child: Stack(
fit: StackFit.expand,
clipBehavior: Clip.none,
alignment: AlignmentDirectional.topCenter,
children: [
Padding(
padding: EdgeInsets.only(top: 30),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: ClipRRect(
child: Container(
color: Colors.blue,
)
),
),
),
Positioned(
top: 0,
child: Container(
width: 50,
height: 50,
child: FittedBox(
child: FloatingActionButton(
onPressed: () {
setState((){
color = (color == Colors.red) ? Colors.orange : Colors.red;
});
},
backgroundColor: color,
child: Icon(Icons.ac_unit),
),
),
),
)
],
),
),
);
}
}

Related

how to make child widget looks like its bigger then its parent widget in flutter

I want to make a custom navigationbar widget in Flutter. The blue circle widget size is bigger than its parent widget. But I don't know how to make it without any plugins. Please see the image for clarification.
class CustomNavBar extends StatelessWidget {
const CustomNavBar({final super.key});
#override
Widget build(final BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(16)),
),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
width: 150,
child: Row(
children: [
GestureDetector(
child: const Icon(
Icons.phone,
color: Color(0xFF24A3FF),
),
onTap: () {
// On phone tapped
},
),
const Spacer(),
GestureDetector(
child: const Icon(
Icons.contacts,
color: Color(0xFF24A3FF),
),
onTap: () {
// On contacts tapped
},
),
],
),
),
GestureDetector(
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
border: Border.all(color: Color(0xFF24A3FF), width: 6),
),
child: Icon(UniconsLine.plus, color: Color(0xFF24A3FF)),
),
onTap: () {
// On plus tapped
},
),
],
);
}
}
You can use a Stack to achieve this effect
SizedBox(
height: 70,
width: MediaQuery.of(context).size.width *0.7,
child :Stack(
children: [
Center(
child : Container(
height : 40,
width : MediaQuery.of(context).size.width * 0.7,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color : Colors.red,
)
child: Row(
children : [
Icon(Icons.add),
Container(),
Icon(Icons.remove),
]
)
),
),
Center(
child: CircleAvatar(
radius : 70,
)
)
]
)
)

Flutter - Problem about container with Network Image aligned to right and text aligned to left

I'm trying to create a simple Container includes;
A text aligned to center left (that text can include 50 characters)
A network image which is aligned to bottom right, rotated a bit, and rounded.
I've written a code like this:
import 'package:flutter/material.dart';
class MyScreen extends StatefulWidget {
static const String idScreen = "myScreen";
#override
_MyScreenState createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("My Screen")),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 200,
height: 100,
alignment: Alignment.bottomRight,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadiusDirectional.circular(16.0),
color: Colors.yellow),
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Flexible(
child: Text(
"Baby",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
),
Transform(
child: Image.network(
"https://target.scene7.com/is/image/Target/Baby-210913-1631564062108?wid=315&hei=315&qlt=60&fmt=webp",
fit: BoxFit.contain,
),
transform: Matrix4.rotationZ(-0.2),
alignment: FractionalOffset.centerRight,
),
],
),
),
),
);
}
}
and the output is;
My output has some problems.
I want the container to be wide left-right and has 8 padding from the right and left.
I want the network picture to be bottom right with only its rounded picture, not as white square.
-- EDIT --
I've changed my code like;
import 'package:flutter/material.dart';
class MyScreen extends StatefulWidget {
static const String idScreen = "myScreen";
#override
_MyScreenState createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("My Screen")),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: MediaQuery.of(context).size.width,
height: 100,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadiusDirectional.circular(16.0),
color: Colors.yellow),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Baby",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
Container(
width: 50,
height: 50,
decoration: const BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.contain,
image: NetworkImage(
"https://target.scene7.com/is/image/Target/Baby-210913-1631564062108?wid=315&hei=315&qlt=60&fmt=png"
)
)
),
),
],
),
),
),
);
}
}
But now there is no rotation on picture, and it is not aligned to bottom right. Output:
To get the container wide you should change its width from 200 to width of your screen using Mediaquery.
eg : MediaQuery.of(context).size.width
That image has a white background also its square. You should use an image with transparent background.
Add this to your row : mainAxisAlignment: MainAxisAlignment.spaceBetween this will move the image to right end and text to left.
Hope that is what you meant and this helps.
Edit
I made some changes and now your code looks like this :
Container(
color: Colors.yellow,
height: 100,
width: Constants(context).scrnWidth,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Baby",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
Container(
height: 100,
child: Align(
alignment: Alignment.bottomCenter,
child: Transform.rotate(
angle: -0.3,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 50,
height: 50,
decoration: const BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.contain,
image: NetworkImage(
"https://target.scene7.com/is/image/Target/Baby-210913-1631564062108?wid=315&hei=315&qlt=60&fmt=png"))),
),
),
),
),
),
],
),
),

How can I change the color of a container with multiple textbuttons in flutter?

I have this exercise where I have a container above and four textbuttons below. when I press on one of the 4 textbuttons (each one having another color), the top container should change color. Now I know this has to happen with a custom function that should either take the color of the text button or take the original color of the top container. Can I do this through assigning a number to each color as a variable and inputing that as a parameter into the colorwidget above?
I'm a bit stuck with it.
thx.
check this example
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Color color = Colors.amber;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Container color")),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Container(
decoration: BoxDecoration(
color: color,
borderRadius: const BorderRadius.all(Radius.circular(10))),
height: 100,
width: 100,
),
),
const SizedBox(
height: 20,
),
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Center(
child: InkWell(
onTap: () {
setState(() {
color = Colors.amber;
});
},
child: Container(
decoration: const BoxDecoration(
color: Colors.amber,
borderRadius:
BorderRadius.all(Radius.circular(10))),
height: 100,
width: 100,
),
),
),
Center(
child: InkWell(
onTap: () {
setState(() {
color = Colors.blue;
});
},
child: Container(
decoration: const BoxDecoration(
color: Colors.blue,
borderRadius:
BorderRadius.all(Radius.circular(10))),
height: 100,
width: 100,
),
),
)
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Center(
child: InkWell(
onTap: () {
setState(() {
color = Colors.red;
});
},
child: Container(
decoration: const BoxDecoration(
color: Colors.red,
borderRadius:
BorderRadius.all(Radius.circular(10))),
height: 100,
width: 100,
),
),
),
Center(
child: InkWell(
onTap: () {
setState(() {
color = Colors.green;
});
},
child: Container(
decoration: const BoxDecoration(
color: Colors.green,
borderRadius:
BorderRadius.all(Radius.circular(10))),
height: 100,
width: 100,
),
),
)
],
),
)
],
)
],
),
);
}
}

Container is not center aligned in stack

I have simple application, and I try to center a circular image in the screen with the following:
main.dart:
import 'package:flutter/material.dart';
import './widgets/mashoff.dart';
void main() => runApp(_MyApp());
class _MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 0,
backgroundColor: Colors.black, // status bar color
),
body: WMashoff(),
),
);
}
}
WMashoff widget:
import 'package:flutter/material.dart';
import './mashoff-item.dart';
class WMashoff extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkMKry-Vcp7oU4erjWxG4pXhfva0weO4n4_g&usqp=CAU',
Alignment.topLeft,
),
WMashoffItem(
'https://hotforsecurity.bitdefender.com/wp-content/uploads/2019/12/pexels-photo-1090393-1024x682.jpg',
Alignment.topRight,
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://images.indianexpress.com/2019/07/tiktok.jpg',
Alignment.bottomLeft,
),
WMashoffItem(
'https://www.gannett-cdn.com/-mm-/b4f5f67b6112c6935b66ff0cbffc204774fdc87c/c=0-0-3022-1700/local/-/media/2020/08/18/PalmBeachPost/ghows-LK-200819292-73c48e6d.jpg?width=660&height=372&fit=crop&format=pjpg&auto=webp',
Alignment.bottomRight,
),
],
),
),
],
),
Container(
height: 70,
width: 70,
alignment: Alignment.center,
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
const BoxShadow(
color: Colors.black87,
spreadRadius: 5,
blurRadius: 3,
offset: const Offset(0, 3), // changes position of shadow
),
],
image: const DecorationImage(
image: const NetworkImage('http://i.imgur.com/QSev0hg.jpg'),
fit: BoxFit.cover,
),
borderRadius: const BorderRadius.all(const Radius.circular(50)),
border: Border.all(
color: Colors.white,
width: 4,
),
),
),
],
),
);
}
}
WMashoffItem widget:
import 'package:flutter/material.dart';
class WMashoffItem extends StatelessWidget {
final Alignment _favoriteIconAlignment;
final String _img;
WMashoffItem(
this._img,
this._favoriteIconAlignment,
);
#override
Widget build(BuildContext context) {
return Expanded(
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(
this._img,
fit: BoxFit.cover,
),
Container(
alignment: this._favoriteIconAlignment,
child: const IconButton(
iconSize: 50,
icon: Icon(
Icons.favorite_border,
color: Colors.red,
),
onPressed: null,
),
),
],
),
);
}
}
Please focus the 2nd stack child in the WMashoff widget code (The Container element).
Once I give this container height and width, it is no more centered.
This is the result:
As you can see the image is in the top left. But I want it in the center. Where am I wrong?
When you do:
Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
You create a 70x70 Container and align its child in the center.
Now, what you want is to align your container at the center of the Stack:
Try this:
Stack(
children: <Widget>[
Column([...]),
Align(
alignment: Alignment.center,
child: Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
),
],
),

How to change the text in first screen when there is any change in third screen using provider package in flutter?

I have a firstscreen called including stateful widget, PlantFeatureScreen1 where I have to show the change when the update occurs in thirdscreen which also includes a stateful widget, CartDetais3.
How can i update the first screen when changes happens in third screen and addQuantity and subtractQuantity functions will be added in third screen? Please suggest using provider package.
firstScreen code Here I have to show the change in very last center widget of this widget tree.
class PlantFeatureScreen1 extends StatefulWidget {
#override
_PlantFeatureScreen1State createState() => _PlantFeatureScreen1State();
}
class _PlantFeatureScreen1State extends State<PlantFeatureScreen1> {
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TopAppBar(),
Expanded(
flex: 1,
child: Align(
alignment: Alignment(-1, 0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Text(
"Plants",
style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700),
),
),
),
),
Expanded(
flex: 5,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.blue,
),
child: DefaultTabController(
length: 5,
child: Column(
children: [
Container(
height: 50,
width: double.infinity,
child: TabBar(
isScrollable: true,
tabs: ourAllLists.tabMaker(),
),
),
Container(
height: 317,
width: double.infinity,
decoration: BoxDecoration(color: Colors.white),
child: TabBarView(
children: ourAllLists.tabViewerMaker(context),),),
],
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
child: Container(
alignment: Alignment.bottomRight,
height: 120,
width: double.infinity,
child: Stack(
overflow: Overflow.visible,
children: [
Container(
height: 70,
width: 105,
decoration: BoxDecoration(
color: Color(0xFF96CA2D),
borderRadius: BorderRadiusDirectional.horizontal(
end: Radius.circular(32),
start: Radius.circular(32))),
child: Icon(FontAwesomeIcons.shoppingBag,color:Colors.white,size:30),
),
Positioned(
// top: 0,
bottom: 50,
right: 0,
child: Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Color(0xFF96CA2D),width: 4)
),
child: Center(child: Text(ourAllLists.totalquantity().toString(),style:TextStyle(fontSize: 20,color: Color(0xFF96CA2D)))),
),
)
],
),
),
)
],
);
}
}
thirdScreen code Here the update happens when the + or - icons get clicked.
class CartDetais3 extends StatefulWidget {
#override
_CartDetais3State createState() => _CartDetais3State();
}
class _CartDetais3State extends State<CartDetais3> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.fromLTRB(8, 20, 8, 15),
child: Column(
children: [
TopAppBar(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Cart",
style: kPlantNameStyle,
),
Text(
"\$" + "284",
style: kItemPrice,
),
],
),
Expanded(
child: ListView.builder(
itemCount: OurAllLists.cartPlantList3.length,
itemBuilder: (context, index) {
return Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 110,
width: 100,
child: Image(image: AssetImage("assets/tulip.png")),
),
Container(
height: 80,
width: 120,
child: Column(
children: [
Text(OurAllLists.cartPlantList3[index].pN),
Text(OurAllLists.cartPlantList3[index].ca
.toUpperCase()),
Text("\$ " +
OurAllLists.cartPlantList3[index].pr
.toString()),
],
),
),
Container(
height: 120,
child: Column(
children: [
FlatButton(
onPressed: () {
setState(() {
*here addQuantity function must go*
});
},
child: Icon(
FontAwesomeIcons.plus,
size: 20,
),
),
Text(OurAllLists.cartPlantList3[index].qu
.toString()),
FlatButton(
onPressed: () {
*here subtractQuantity function must go*
},
child: Icon(
FontAwesomeIcons.minus,
size: 20,
),
),
],
))
],
),
);
}),
)
],
),
),
)));
}
}
I have a also a seperate class called ViewTotallItemProvider
class ViewTotalItemProvider extends ChangeNotifier{
addQuantity(index){
OurAllLists.cartPlantList3[index].qu++;
notifyListeners();
}
subtrachQuantity(index){
OurAllLists.cartPlantList3[index].qu--;
}
}