I have started using flutter a couple of days ago & working on an application in which trying to place a title at the center of the screen & the image at the right corner of the screen. I have the following code. But it doesn't seem to work.
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(
"Launching",
textAlign: TextAlign.center,
style: GoogleFonts.poppins(
fontWeight: FontWeight.w700, fontSize: 18),
),
),
),
Container(
child: CircleAvatar(
backgroundImage: NetworkImage(
"https://cdn.pixabay.com/photo/2015/12/01/20/28/fall-1072821__340.jpg"),
),
),
],
)
],
),
),
),
Can somebody tell me how can I achieve this?
I think this is what you are trying to achieve:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 30),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(
"Launching",
textAlign: TextAlign.center,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
child: CircleAvatar(
backgroundImage: NetworkImage(
"https://cdn.pixabay.com/photo/2015/12/01/20/28/fall-1072821__340.jpg"),
),
),
],
)
],
),
),
),
),
);
}
}
which produces the following outcome:
When working with Stack you should only use Positioned, Align & Center widgets to position your widgets.
Example app:
void main() {
runApp(
MaterialApp(
title: 'Flutter To Do',
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Text(
"center",
),
),
Align(
alignment: AlignmentDirectional.centerStart,
child: Text('start'),
),
Positioned(
left: 16,
top: 16,
width: 64,
height: 64,
child: CircleAvatar(
backgroundImage: NetworkImage(
"https://cdn.pixabay.com/photo/2015/12/01/20/28/fall-1072821__340.jpg"),
),
),
],
),
),
),
);
}
Result:
Just use alignment property of the container.
I have changed the code for you, hope it helps:
Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 30),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(
"Launching",
textAlign: TextAlign.center,
),
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 30),
alignment: Alignment.topRight,
child: CircleAvatar(
backgroundImage: NetworkImage(
"https://cdn.pixabay.com/photo/2015/12/01/20/28/fall-1072821__340.jpg"),
),
),
],
),
),
));
Related
I have a flutter app that I want to add "stories" to, but I am unable to do so cause I can't place 3 buttons over an image.
That's my desired look...
And this is what I can do so far...
And this is my code:-
child: GestureDetector(
child: Center(
child: Stack(
alignment: Alignment.bottomRight,
children: [
Image.network(
widget.url,
),
Container(
child: Column(children: [
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
),
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
),
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
)
]),
)
],
),
),
onTap: () => controller.next(),
),
How can I achieve this effect?
Am I doing something with the Stack widget?
Using Positioned inside Stack can solve your problem, Below code may work for you, You can position the Container where you want it to be.
Widget build(BuildContext context) {
var ScreenHeight = MediaQuery.of(context).size.height;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Expenses App"),
backgroundColor: Colors.pink[900],
actions: [
IconButton(
icon: Icon(Icons.add),
// onPressed: () => startAddNewTransaction(context),
),
],
),
body: GestureDetector(
child: Center(
child: Stack(
// alignment: Alignment.bottomRight,
children: [
Image.network(
'https://picsum.photos/250?image=9',
),
Positioned(
top: ScreenHeight * 0.17,
child: Container(
child: Column(children: [
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
),
SizedBox(height: ScreenHeight * 0.01),
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
),
SizedBox(height: ScreenHeight * 0.01),
Container(
color: Colors.grey,
child:
Column(children: [Icon(Icons.share), Text('مشاركة')]),
)
]),
),
)
],
),
),
// onTap: () => controller.next(),
),
),
);
}
The default mainAxisSize of Column is max, If you set MainAxisSize.min for all Column you will get Stack's alignment.
child: Column(
mainAxisSize: MainAxisSize.min,
As for the alignment of your desire UI, I prefer using Align widget to wrap the parent Column.
Align(
alignment: Alignment(-1, .5), //play with it
child: Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
More about Align.
GestureDetector(
child: Center(
child: Stack(
children: [
Container(
height: double.infinity,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"https://st2.depositphotos.com/3759967/5593/i/600/depositphotos_55936567-stock-photo-swirling-frosty-multi-colored-fractal.jpg",
),
),
),
),
Positioned(
bottom: MediaQuery.of(context).size.height * 0.1,
child: Column(
children: [
Container(
margin: const EdgeInsets.only(bottom: 10.0),
color: Colors.grey,
child: Column(
children: const [Icon(Icons.share), Text('مشاركة')]),
),
Container(
margin: const EdgeInsets.only(bottom: 10.0),
color: Colors.grey,
child: Column(
children: const [Icon(Icons.share), Text('مشاركة')]),
),
Container(
margin: const EdgeInsets.only(bottom: 10.0),
color: Colors.grey,
child: Column(
children: const [Icon(Icons.share), Text('مشاركة')]),
)
]),
),
],
),
),
),
You can do like this, place image as background of a container, and your column in a positioned widget.
example
I'm trying to make a list that when expanded hides under button (stack will do it) and place the button at the bottom of the screen. The problem is whenever the list comes to the bottom edge, it throws an overflow error. The second problem is that when I wrap list and button in stack, they are stacked on each other. Without stack wrapping the Column the layout is fine but the list still overflows. With stack, the displacement is wrong and app throws vertical overflow error.
I'm aiming towards a layout shown on this picture
Layout without usage of stack
\
Layout with usage of stack
Here is task_list_screen.dart:
class TaskListScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Container(
color: Colors.blueAccent,
child: Text(
'Your tasks chief',
style: GoogleFonts.notoSans(
textStyle: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w500,
color: Colors.black)),
textAlign: TextAlign.center,
),
alignment: Alignment.topCenter,
),
),
Container(
height: 500,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Stack(
children: [
_taskList(),
Align(
child: _AddTaskButton(),
alignment: Alignment.bottomCenter,
),
],
),
],
),
),
],
),
),
),
),
);
}
}
A simple way to archive your layout using floatingActionButton
Result
Demo widget
class TaskListScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Container(
color: Colors.blueAccent,
child: Text(
'Your tasks chief',
// style: GoogleFonts.notoSans(
// textStyle: TextStyle(
// fontSize: 30,
// fontWeight: FontWeight.w500,
// color: Colors.black)),
textAlign: TextAlign.center,
),
alignment: Alignment.topCenter,
),
),
Expanded(
child: ListView(
shrinkWrap: true,
children: [
...List.generate(
20,
(index) => Container(
height: 100,
color: index.isEven ? Colors.red : Colors.pink,
),
),
],
),
),
],
),
),
),
floatingActionButton: Align(
/// alignment: Alignment(0, .95), more controll on dy
alignment: Alignment.bottomCenter,
child: ElevatedButton(
onPressed: () {},
child: Text("asda"),
),
),
),
);
}
}
I am still new to flutter, and I want to position the box in the screenshot below, not in the centre of the screen, but at a certain distance away from the left side with text inside the box.
I have tried the positioning widget but still no luck.
Any advice on what widget I would use, and how to do so properly?
#override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/startbackground.jpg"),
fit: BoxFit.cover),
),
child: Center(
child: Container(
height: 500.0,
width: 120.0,
color: Colors.blue[50],
child: Align(
alignment: Alignment.topRight,
child: FlutterLogo(
size: 60,
),
),
),
));
}
}
Try out that code. I mainly used containers, paddings, alignment property as well as the axis alignment for the column. With that I achieved what you can see on the screenshot below. In order to use an image behind all that, I would just simply recommend to use a stack and then this whole code with some adaptations. The colors are just illustrative. You could set all of them transparent.
Here is the screenshot:
And here is the code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
title: Text("EXAMPLE"),
),
body: Container(
color: Colors.orangeAccent,
height: double.infinity,
width: double.infinity,
alignment: Alignment.bottomLeft,
child: UnconstrainedBox(
child: Padding(
padding: const EdgeInsets.only(left: 50, bottom: 50),
child: Container(
height: 400,
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
child: Text("Text", style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700),textAlign: TextAlign.left,),
color: Colors.purpleAccent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text("Some Text", style: TextStyle(fontSize: 60, fontWeight: FontWeight.w700),),
color: Colors.purpleAccent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text("Some Text", style: TextStyle(fontSize: 60, fontWeight: FontWeight.w700),),
color: Colors.teal,
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.all(15),
child: FlatButton(
minWidth: 200,
onPressed: (){},
child: Text(
"HI",
style: TextStyle(
color: Color(0xff7638c9),
fontSize: 11),
),
color: Colors.transparent,
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.purple),
borderRadius: BorderRadius.circular(18.0),
),
),
),
),
],
),
),
),
),
),
);
}
}
I am trying to show an image and there will be a text just next to it but my text is so long that it can't fit the screen. Here is my code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
backgroundColor: Colors.grey,
title: new Align(
alignment: Alignment.center,
child: Text("Demo"),
),
),
body: Stack(
children: [
Positioned(
top: 50,
left: 25,
child: Image(
image: NetworkImage(
"https://reimg-teknosa-cloud-prod.mncdn.com/mnresize/200/200/productimage/125036758/125036758_0_MC/48543732.jpg"),
height: 300,
width: 200,
fit: BoxFit.fitWidth,
),
),
Container(
child: Positioned(
top: 70,
left: 260,
child: Text(
"Durum: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaB",
style: GoogleFonts.sourceSerifPro(
fontSize: 20,
),
),
),
)
],
),
);
}
}
I tried overflow: TextOverflow.clip, did not work and I searched about Flexible and Expended but I could not place them in my code. I tried to place them instead of Container but that did not work. How can I make my text continue to down line if it won't fit? Thanks in advance.
Change your code like this:
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
backgroundColor: Colors.grey,
title: new Align(
alignment: Alignment.center,
child: Text("Demo"),
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Center(
child: Text(
"Durum: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaB",
style: GoogleFonts.sourceSerifPro(
fontSize: 20,
),
),
),
),
Image(
image: NetworkImage(
"https://reimg-teknosa-cloud-prod.mncdn.com/mnresize/200/200/productimage/125036758/125036758_0_MC/48543732.jpg"),
height: 300,
width: 200,
fit: BoxFit.fitWidth,
),
],
),
);
}
}
You can add this in your Text widget softWrap: true.
Anybody how to do the layout similar to the image? I am having difficulty on the CardView and it position is lies between the body. I am still consider quite new to Flutter, having some difficulty in UI part. Full code is as below:
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: PreferredSize(
preferredSize: Size.fromHeight(45.0),
child: AppBar(
automaticallyImplyLeading: false,
elevation: 0.0,
centerTitle: true,
backgroundColor: Color(0xFFED2849),
leading: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
margin: const EdgeInsets.only(left: 8.0),
child: GestureDetector(
onTap: (){
Navigator.of(context).pop(null);
},
child: Image.asset('assets/ic_user_title_back.png', width: 12.0, height: 12.0,),
),
),
],
),
title: Center(
child: Container(
child: Text(
'账号中心',
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 14),
),
),
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: GestureDetector(
onTap: (){
//logout
},
child: Text(
'退出登陆',
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 11),
),
),
),
],
),
)
],
)
),
body: SafeArea(
top: false,
child: Material(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
color: Color(0xFFED2849),
),
),
Expanded(
flex: 7,
child: Container(
color: Color(0xFFF1F1F1),
),
)
],
),
),
)
);
}
You can do this using Stack
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
// fit: StackFit.expand,
children: [
Column(
children: [
Expanded(
flex: 3,
child : Container(color: Colors.red)
),
Expanded(
flex: 7,
child: Container(
color : Colors.white)
)
]
),
Positioned(
top: MediaQuery.of(context).size.height * 0.2,
left: 20,
right: 20,
child:Card(
child: Padding(
padding : const EdgeInsets.all(16),
child: Text("Your Text here", ),
),)
)
],
)
Try using Stack() it will allow you to overlap several children in a simple way.