How to overlay widget on widget in flutter [duplicate] - flutter

This question already has answers here:
How can I put a widget above another widget in Flutter?
(4 answers)
Closed 6 months ago.
I'd need to build a content widget with the red circle over the text content in Flutter, how can I do?

you can do it using stack widget
return Scaffold(
body: Center(
child: Container(
width: 450,
height: 200,
color: Colors.white,
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
width: 400,
height: 150,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius:BorderRadius.circular(20)
),
),
),
Align(
alignment: Alignment.topRight,
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle
),
),
),
],
),
),
),
);

Related

Is there something like mainaxisaligment for a stack?

Is there a way to align the blue chart bars to the bottom instead of the top?
Here's the code, the blue bar is the FractionallySizedBox:
#override
Widget build(BuildContext context) {
return Column(
children: [
Container(
height: 20,
child: FittedBox(
child: Text('\$${getShortForm(spendingAmount)}'),
),
),
SizedBox(
height: 4,
),
Container(
height: 64,
width: 10,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
color: Color.fromRGBO(220, 220, 220, 1),
borderRadius: BorderRadius.circular(10),
),
),
FractionallySizedBox(
heightFactor: spendingPctOfTotal,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
SizedBox(
height: 4,
),
Text(label)
],
);
}
}
I tried working with the Stack's alignment attribute but that didn't seem to work. It seems I can only work with the order in which the children are stacked, not position. I tried wrapping it with a column but that broke the entire thing.
Mostly we need to wrap stack children with positioned widget like Aling/Positioned. For your case
child: Stack(
alignment: Alignment.bottomCenter,//default is topStart
children: [
Or
Align(
alignment: Alignment.bottomCenter,
child: FractionallySizedBox(
heightFactor: .3,
child: Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
),

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,
),
],
)),
);
}
}

How can I correctly place a red oval in Flutter?

Where should the red oval be
Hello, my question is how can I get the red oval in the picture above to the position marked below? Does anyone know? I thank you in advance.
This is my code:
return Scaffold(
backgroundColor: const Color(0xffffffff),
body: Stack(
children: <Widget>[
Container(
child: Align(
alignment: Alignment.bottomCenter,
),
width: 436.0,
height: 207.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29.0),
image: DecorationImage(
image: const AssetImage('assets/images/vorlage.png'),
fit: BoxFit.cover,
),
),
),
Container(
child: Align(
alignment: Alignment.bottomCenter,
),
width: 265.0,
height: 20.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14.0),
color: const Color(0xffff0000),
),
),
You might need to look into painting a Canvas. It's fairly well developed, and quite a bit of tutorial information on it (even how to animate things!). Everything is a widget, but some of those widgets are RenderObjects. :)
If you want to red oval to always be fixed at a position in it's parent widget, you can use Positioned widget to fix the distance from top, then use Center to place it in the middle. You will have to tweak the top value (I put 300 arbitrarily).
return Scaffold(
backgroundColor: const Color(0xffffffff),
body: Stack(
children: <Widget>[
Container(
child: Align(
alignment: Alignment.bottomCenter,
),
width: 436.0,
height: 207.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29.0),
image: DecorationImage(
image: const AssetImage('assets/images/vorlage.png'),
fit: BoxFit.cover,
),
),
),
Positioned(
top: 300 // <----tweak this till it's where you want it (distance from top)
child: Center(
child: Container(
child: Align(
alignment: Alignment.bottomCenter,
),
width: 265.0,
height: 20.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14.0),
color: const Color(0xffff0000),
),
),
),
),

Scaled Container Overlapping Parent-widget flutter [duplicate]

This question already has answers here:
Flutter mask a circle into a container
(2 answers)
Closed 2 years ago.
How do I make this blue-circle not overlap the green-box, but insted be confined to the inner bounderyes of the green-box?
Code:
Container(
width: 300,
height: 100,
color: Colors.green,
child: Transform.scale(
scale: 2,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue
),
),
),
),
Pictures tells the rest:
Thx already
- Tobias
I was able to do this using ClipRect widget with hardEdge on clipBehaviour property.
Center(
child: Container(
width: 300,
height: 100,
color: Colors.green,
child: ClipRect(
clipBehavior: Clip.hardEdge,
child: Transform.scale(
scale: 2,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue,
),
),
),
),
),
),

How to remove the inner border of a Widget in a Stack in Flutter?

I wan't to get the outline border of two containers in a Stack, one positioned like the image suggest. My goal is to remove the "red" part.
How can I accomplish this in Flutter?
Widget build(BuildContext context) {
return Stack(
overflow: Overflow.visible,
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
border: Border.all(),
),
child: const Center(child: Text("BLABLABLA")),
),
Positioned(
top: -10,
left: 10,
child: Container(
width: 150,
height: 25,
decoration: BoxDecoration(
border: Border.all(),
),
child: const Center(child: Text("BLABLABL2")),
),
),
],
);
}
If I have understood your question correctly, then this would work for you!
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stack Example'),
),
body: Center(
child: Stack(
overflow: Overflow.visible,
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.2),
border: Border.all(),
),
child: const Center(child: Text("BLABLABLA")),
),
Positioned(
top: -10,
left: 10,
child: Container(
width: 150,
height: 25,
decoration: BoxDecoration(
color: Colors.white,
),
child: Stack(children: [
Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.black),
left: BorderSide(color: Colors.black),
right: BorderSide(color: Colors.black),
),
),
height: 11),
Center(
child: Text("BLABLABL2"),
),
],
),
),
),
],
),
),
);
}
The output will be like,