How to make a container outside of parent container - flutter

What my try looked like:
What I want to do:
What I have tried is Stack widget. But the half of container became invisible when container moves outside.
Stack(
children: [
Align(
alignment: new Alignment(0.0, 0.0),
child: Container(
height: 150,
width: double.infinity,
decoration: BoxDecoration(color: Colors.red),
),
),
Positioned(
top: -15,
left: 140,
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(color: Colors.black),
)),
],
)
Any info would be welcome.
Answer:
Just adding Padding to main container

try use Transform
return Container(
width: 400,
height: 200,
child: Stack(
children: <Widget>[
Align(alignment: Alignment.topCenter,
child: Transform.translate(
child: Container(
width: 100,
height: 50,
color: Colors.green,
child: Text('some text'),
),
offset: Offset(0, -20),
),
),
],
),
color: Colors.red,
);

Use Stack along with Padding on one container to make it lower.
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
Full code to see the result exactly as you want using dartpad.dev/flutter paste this:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Builder(
builder: (context){
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
}
);
}
}

You were correct in using a Stack widget. The code would look like the following:
Center(
child: Stack(
alignment: Alignment.topCenter,
children: [
Padding(
padding: const EdgeInsets.only(top: 25,),
child: Container(
color: Colors.green,
height: 200,
width: 200
),
),
Container(
color: Colors.red,
height: 50,
width: 100
),
]
),
),
By adding top padding to the green container you are pushing it down making room for the red container to sit on top.
The result looks like this:

Related

multiple draggables are always moving once item is dragged

My goal is to have a dog as my DragTarget and multiple items I can drag to the dog.
Whenever I drag one item the others are switching positions.
Also when I drag the item onto the DragTarget I want to print out a text in a text-box - but nothing is executed even though I'm using onAccept.
This is the code I have:
import 'package:flutter/material.dart';
import 'package:prototype/screens/start-page.dart';
class DragDropItems extends StatelessWidget {
const DragDropItems({super.key});
final Color color = Colors.black;
#override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage('assets/images/Environment.png'))),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(children: <Widget>[
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(top: 200),
child: DragTarget<String>(
onAccept: (data) => Padding(
padding: const EdgeInsets.all(15),
child: Column(
children: const <Widget>[
Padding(
padding: EdgeInsets.all(15),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'asdf',
hintText: 'gang'),
))
],
),
),
builder: (_, __, ___) {
return Container(
width: 300,
height: 300,
alignment: Alignment.center,
child: Image.asset('assets/images/Doggo3.png'),
);
},
))),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 50),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Draggable<String>(
data: 'red',
feedback: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child:
Image.asset('assets/images/cupcake1.png'),
)),
childWhenDragging: Container(),
child: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child: Image.asset('assets/images/cupcake1.png'),
),
),
),
Draggable<String>(
data: 'blue',
feedback: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child: Image.asset('assets/images/banana1.png'),
)),
childWhenDragging: Container(),
child: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child: Image.asset('assets/images/banana1.png'),
),
),
),
Draggable<String>(
data: 'green',
feedback: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child:
Image.asset('assets/images/hamburger1.png'),
)),
childWhenDragging: Container(),
child: SizedBox(
height: 90.0,
width: 90.0,
child: Center(
child:
Image.asset('assets/images/hamburger1.png'),
),
),
),
],
)))
]),
));
}
}
this is the result when executing on the emulator:
enter image description here
I would appreciate any help!
You need to provide the widget passed to the child parameter for childWhenDragging also. It is not required for both to be the same. You can use any widget.
Check this article: https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/

SingleChildScrollView overflows instead of scrolls

Why does this SingleChildScrollView overflow instead of scroll when the screen height is resized too small? I've used this design because the bottom widget needs to be pushed to the bottom.
return NavigationListener(builder: (context, child) {
return SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height,
width: maxSideMenuWidth,
child: Column(
children: [
Container(
height: 155, color: Colors.purple, child: const MenuHeader()),
Container(
color: Colors.red,
height: 327,
child: MainMenu(
primaryDestinations: primaryDestinations,
secondaryDestinations: secondaryDestinations,
onNavigate: onNavigate,
isSelected: isSelected,
)),
Spacer(),
Container(
color: Colors.green,
height: 135,
child: LoginContextWidget(user, userDetails, logOut)),
],
),
));
});
Try This code
return NavigationListener(builder: (context, child) {
return SizedBox(
height: MediaQuery.of(context).size.height,
width: maxSideMenuWidth,
child: SingleChildScrollView(
child: Column(
children: [
Container(
height: 155, color: Colors.purple, child: const MenuHeader()),
Container(
color: Colors.red,
height: 327,
child: MainMenu(
primaryDestinations: primaryDestinations,
secondaryDestinations: secondaryDestinations,
onNavigate: onNavigate,
isSelected: isSelected,
)),
Spacer(),
Container(
color: Colors.green,
height: 135,
child: LoginContextWidget(user, userDetails, logOut)),
],
),
),
);
});
wrap singlechildscrollview in expanded
return NavigationListener(builder: (context, child) {
return Expanded(
child:SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height,
width: maxSideMenuWidth,
child: Column(
children: [
Container(
height: 155, color: Colors.purple, child: const MenuHeader()),
Container(
color: Colors.red,
height: 327,
child: MainMenu(
primaryDestinations: primaryDestinations,
secondaryDestinations: secondaryDestinations,
onNavigate: onNavigate,
isSelected: isSelected,
)),
Spacer(),
Container(
color: Colors.green,
height: 135,
child: LoginContextWidget(user, userDetails, logOut)),
],
),
));
};
});

How to draw SVG image that is bigger than parent widget in Flutter

I'm trying to render SVG image that is bigger than ViewBox. To do this I'm using flutter_svg 0.19.0. For some reasons, the the image scales only to border of parent widget. However, if I specified smaller size than ViewBox, then image scales down correctly.
Creating SVG widget:
void setNewImage(String path) {
setState(() {
svg = SvgPicture.asset(
path,
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 3000.0,
height: 5000.0,
clipBehavior: Clip.none,
);
});
}
Parent Widget:
Scaffold(
resizeToAvoidBottomPadding: false,
//avoid resize when keyboard is visible
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
body: ClipRRect(
child: Container(
color: Colors.transparent,
child: svg
)
)
)
Have you any idea how can I deal with this problem? Using SizedBox, Container with specified width and height also not works.
Edit:
I found the solution by inserting svg Widget into Transform.scale() widget.
Scaffold(
resizeToAvoidBottomPadding: false,
//avoid resize when keyboard is visible
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
body: ClipRRect(
child: Container(
color: Colors.transparent,
child: Transform.scale(
scale: 2,
alignment: Alignment.topLeft,
child: svg)
)
)
)
Maybe you want to use an OverflowBox:
Container(
width: 80,
height: 80,
child: OverflowBox(
alignment: Alignment.centerLeft,
maxWidth: 120,
child: Container(
padding: EdgeInsets.all(12.0),
decoration: BoxDecoration(
color: Colors.amber,
border: Border.all(color: Colors.brown, width: 3.0),
),
child: SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
),
),
),
),
Full source code:
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
);
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: 100,
height: double.infinity,
decoration: BoxDecoration(
color: Colors.amber.shade200,
border: Border(
right: BorderSide(color: Colors.black45, width: 2.0),
),
),
child: Column(
children: [
const SizedBox(height: 24.0),
SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 80.0,
),
const SizedBox(height: 24.0),
SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 80.0,
),
const SizedBox(height: 24.0),
SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 80.0,
),
const SizedBox(height: 24.0),
SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 80.0,
),
const SizedBox(height: 24.0),
Container(
width: 80,
height: 80,
child: OverflowBox(
alignment: Alignment.centerLeft,
maxWidth: 120,
child: Container(
padding: EdgeInsets.all(12.0),
decoration: BoxDecoration(
color: Colors.amber,
border: Border.all(color: Colors.brown, width: 3.0),
),
child: SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
),
),
),
),
const SizedBox(height: 24.0),
SvgPicture.asset(
'images/hello.svg',
alignment: Alignment.center,
allowDrawingOutsideViewBox: true,
width: 80.0,
),
],
)),
);
}
}

Overflow visible in Expanded widget

My layout is a row with three expanded widgets.
I need overflow visible in one of the expanded widgets
This is the code
Row(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red,)
),
Expanded(
flex: 2,
child: Container(color: Colors.green,)
),
Expanded(
flex: 3,
child: Container(color: Colors.orange,)
),
],
),
Now I need to add circles like this, in of the Expanded widgets, that overflow the Expanded widget. I can't wrap the Expanded in an overflow box, so I am lost
Any help is appreciated.
Edit: I tried this with the third expanded, just to see if it will overflow, but it only overflows the SizedOverflowBox, no overflow over the Expanded
Expanded(
flex: 3,
child: SizedOverflowBox(
size: const Size(100.0, 100.0),
alignment: AlignmentDirectional.bottomCenter,
child: Container(height: 50.0, width: 1500.0, color: Colors.blue,),
),
),
It looks like it is not going to be possible
Can easily be achieved with a Stack. Please see the code below :
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.red,
)),
Expanded(
flex: 2,
child: Container(
color: Colors.green,
)),
Expanded(
flex: 3,
child: Container(
color: Colors.orange,
)),
],
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
],
),
),
],
);
}
}
Please see the code for Text Bullet points overflowing.
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(),
body: MyWidget(),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.red.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Text(
'HelloHello 🅐',
style: TextStyle(color: Colors.white),
),
),
),
),
),
Expanded(
flex: 2,
child: Container(
color: Colors.green.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Container(
child: Text(
'\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐',
style: TextStyle(color: Colors.white),
),
),
),
),
),
),
Expanded(
flex: 3,
child: Container(
color: Colors.purple.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Text(
'\n\n\n\n\n\n\n\n\n\n\n\Hello Hello Hello Hello Hello Hello ',
style: TextStyle(color: Colors.white),
),
),
),
),
),
],
);
}
}

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--;
}
}