Flutter - How 2 Give different width to different container enclosed in a single column? - flutter

I'm trying to develop a clone of an App idea from dribble, which can be found here.
Can you please tell me is the code I'm trying to write is correct or not??
As I've tried to wrap a Column with two text Widgets Enclosed in Container ( separate ) and when I'm trying to change the width of one container It changes the margin of the other also.
My Code is :-
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 30, left: 20),
child: const Text(
'Find Intrested Events to Join',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0,
wordSpacing: 2,
),
),
width: 200,
),
// -----------------------------------------------------------------
Container(
margin: EdgeInsets.only(left: 20, top: 20),
width: 220,
child: Text(
'We share all events like Charity, workshop, Blood drive, etc.',
style: TextStyle(wordSpacing: 2, height: 1.4),
),
)
],
),
),
);
Can you tell me How to achieve that stage that I can change the Containers' width without affecting the margin of others.

It will be better to use Stack for this case while it contains background image, Also learn more about Stack, Align, Positioned and LayoutBuilder from flutter.dev
Play with this widget ,
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child: LayoutBuilder(
builder: (context, constraints) {
return Stack(
children: [
Container(
color: Colors.amber.withOpacity(.4),
), //background image
Positioned(
left: 20,
top: 30,
child: SizedBox(
width: constraints.maxWidth * .5, // 50% of width
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Find Intrested Events to Join',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0,
wordSpacing: 2,
),
),
SizedBox(
height: 20,
), //spaces between text
Text(
'We share all events like Charity, workshop, Blood drive, etc.',
style: TextStyle(wordSpacing: 2, height: 1.4),
),
],
),
),
),
Align(
// also Postisioned can be use
alignment: Alignment.bottomCenter,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("hereh"),
ElevatedButton(onPressed: () {}, child: Text("Button"))
],
),
)
],
);
},
)),
);
}
}

Related

Container should have the same height

I want that the red container has the same height as the blue container. The problem is, that the height of the blue container changes depending of the amount of lines. It would also help if the red container starts on the same height as blue container. The goal is to have a functional bullet point in the middle of the first line
My code is:
import 'package:flutter/material.dart';
class InhaltGesetz extends StatelessWidget {
final String text;
const InhaltGesetz({
Key? key,
required this.text,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: Container(
height: 70,
color: Colors.red,
child: const Align(
alignment: Alignment(0, -0.99),
child: Text(
'•',
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
),
),
LayoutBuilder(
builder: (context, constraints) {
return Container(
width: 340,
color: Colors.blue,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(4, 8, 8, 8),
child: Text(
text,
style: const TextStyle(
fontSize: 15.5,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
);
},
),
],
);
}
}
I do not know what to do I tried to change the height manually with every container but i ended up with 9000+ lines of code and i will not do it any longer
You can use IntrinsicHeight widget(expensive), Follow this structure.
class InhaltGesetz extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [
IntrinsicHeight(
child: Row(
children: [
Container(
width: 200,
color: Colors.red,
child: const Align(
alignment: Alignment(0, -0.99),
child: Text(
'•',
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
),
Container(
width: 340,
height: 200,
color: Colors.blue,
),
],
),
),
],
);
}
}

ElevatedButton Position Changing method

I have an Elevated Button which is on of the bottom of the Page and I am a beginner sorry for this silly doubts but i can't figure out how to change the position of the button I dont know how to try positioned widget too. Kindly help me
I tried positioned widget but couldn't do well can anyone help me with this. here is my full code.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
],
)
),
);
}
}
class OnBoardContent extends StatelessWidget {
const OnBoardContent({
Key? key,
required this.image,
required this.description,
}) : super(key: key);
final String image, description;
#override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(
height: 160,
),
const Text("Naz-Kearn",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
)),
const Text("A simplify learning App",
style: TextStyle(
fontWeight: FontWeight.normal
),
),
Image.asset(image),
const SizedBox(
height: 50,
),
Text(description,
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.normal),
),
],
);
}
}
Output of the above code
You need your widgets in a stack if you want to use Positioned widget on them :
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack( //wrapped the whole column with a stack so that all the other widgets doesn't get disturbed
children: [
Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
],
),
Positioned(
top: MediaQuery.of(context).size.height*0.7, //change the 0.7 part to any number you like
child: SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
),
],
)
),
);
}
}
class OnBoardContent extends StatelessWidget {
const OnBoardContent({
Key? key,
required this.image,
required this.description,
}) : super(key: key);
final String image, description;
#override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(
height: 160,
),
const Text("Naz-Kearn",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
)),
const Text("A simplify learning App",
style: TextStyle(
fontWeight: FontWeight.normal
),
),
Image.asset(image),
const SizedBox(
height: 50,
),
Text(description,
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.normal),
),
],
);
}
}
try this code, you can use alignment property of the Stack widget to center everything.
SafeArea(
child: Stack(
alignment: Alignment.center, //do this
children: [
You can wrap your button with Padding widget which helps you to add padding as you like
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),),
],
)
),
);
}
Firstly please mention what precisely the issue you are facing. If you have a problem with the get started button, what is the expected place for the get started button in the design?
Based on the code given, I'm hoping that the get started button should be at the bottom of the screen with some space below. You have already placed the button at the bottom, but you are not able to give space below.
There are some possible ways, you can use it with the get started button component.
Instead of this,
SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
Option 1
Use container with margin
Container(
height: 30,
width: 200,
margin: EdgeInsets.only(
bottom: 50,
),
child: ElevatedButton(
onPressed: () {},
child: const Text("Tap to get started"),
),
),
Option 2
Wrap existing SizedBox with padding widget
Padding(
padding: EdgeInsets.only(bottom: 50.0),
child: Sizedbox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: () {},
child: const Text("Tap to get started"),
),
),
),
Even with some more ways to move the button wherever you need, you can try your own with the following widgets Expanded(), Spacer(), SizedBox(), Positioned() and etc.

How to align the child of a column on the bottom while keeping other children's position at the center in Flutter?

I want to align the text "2022" on the bottom of the screen while keeping the image and its neighbor text at the center of the screen.
class SplashScreen extends StatelessWidget {
const SplashScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Globalcolors.mainColor,
body: Container(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Image.asset(
'assets/images/splash_logo.png',
color: Colors.white,
),
),
const Padding(
padding: EdgeInsets.all(50),
child: Text(
'Sample Text',
style: TextStyle(
color: Colors.white,
fontSize: 36,
fontFamily: 'MouseMemoirs'),
),
),
const Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
],
),
),
);
}
}
I tried to use this link to solve the problem. I used Expanded:
const Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
),
)
But, The result is not good:
You can user Stack and column widgets like
Stack(
children: [
Column(), // contains the image and title
Align(
alignment: Alignment.bottomCenter,
child: Text("2022"),
),
]
)
You can also use Positioned widget to align the text 2022
you can use the bottom method that comes with the scaffold
Scaffold(
bottom : Text("2020")
)
this will make the text always on the bottom of the screen

How to break a Text (placed in a Stack widget) into multiple lines in Flutter?

I want to implement the following design in Flutter, specifically the rounded rectangle with the Text placed on it.
I have used the Stack widget to position the Text at the bottom left of the Container, but the problem is that the Text goes in one line beyond the Stack boundary, instead of breaking into the second line. For simplicity sake, I have written a simpler code as following:
#override
Widget build(BuildContext context) {
return Center(
child: Stack(
children: [
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
),
const Positioned(
left: 16,
bottom: 16,
child: Text(
"A very looooooooooooooooong teeeeeeeext",
maxLines: 2,
softWrap: true,
),
),
],
),
);
}
And the result is:
So how can I break the Text into the second line (not by using \n character), in this scenario. Or, if there is another solution other than using Stack, please tell me. Thanks.
You can use align inside the container instead of using stack
#override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
child: const Align(
alignment: Alignment.bottomLeft,
child: Text(
"A very looooooooooooooooong teeeeeeeext",
maxLines: 2,
softWrap: true,
),
),
),
);
}
You can use GridView to implement the following design. Like this:
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 16.0,
crossAxisSpacing: 16.0,
),
children: [
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('title1'),
Text('21222222222222222222222222222')
],
),
),
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('title1'),
Text('21222222222222222222222222222')
],
),
),
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('title1'),
Text('21222222222222222222222222222')
],
),
),
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('title1'),
Text('21222222222222222222222222222')
],
),
),
],
)
Or you can use GridView.builder.
Use Expanded Widget
#override
Widget build(BuildContext context) {
return Center(
child: Stack(
children: [
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
),
const Positioned(
left: 16,
bottom: 16,
child: Expanded(
Text(
"A very looooooooooooooooong teeeeeeeext",
maxLines: 2,
softWrapenter code here: true,
),
),
),
],
),
);
}
Use layout like this for multiple line text on stack.
Stack(
children: [
CustomPaintCurves(),
Positioned(
top: 0.0,
left: 0.0,
right: 0.0,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
AppAssets.icon_button_background,
gaplessPlayback: true,
fit: BoxFit.cover,
height: 80,
width: 80),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
StringManager.appName,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(StringManager.aboutUsDescription),
),
],
),
),
],
),
A sample code snippet
Stack(
children: [
//! other children in this stack
Positioned(
left: 20, //! giving it random position
bottom: 20,
width: MediaQuery.of(context).size.width * 0.80, //! so that the `Text` widget knows where it overflows
child: const Text(
"Some Very Long Text Here .......", //! the long text you want to clip
overflow: TextOverflow.ellipsis, //! type of overflow
softWrap: false,
maxLines: 3, //! max lines before clipping the text
),
),
],
);
Explanation:
Gave random positions to the text widget (bottom left) of its container. Also gave width to the 'Positioned' widget so that the 'Text' widget knows where the text actually overflows and where it needs to apply the overflow properties.
I used 'TextOverflow.ellipsis' so that the overflowed text would go into the next line, but I specified how many lines of text I want to see (i.e. 'maxLines: 3') before the rest of the text is replaced with '...'.
this problem occur because inside stack you not applied any width of Text widget (child widget), so it will not take any width,
so in your case wrap your text widget into SizeBox and applied width , it will work.
watch full code
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: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stack(
children: [
Container(
width: 150,
height: 150,
color: Colors.teal.shade300,
),
const Positioned(
left: 16,
bottom: 16,
child: SizedBox(
width: 150,
child: Text(
"A very looooooooooooooooong teeeeeeeext",
maxLines: 2,
softWrap: true,
),
),
),
],
),
),
);
}
}
see the result

Flutter - overflowed by Infinity pixels on the bottom

I have problem with overflowed by Infinity pixels on the bottom. I'm find many solution but again not working.
This is my details page.
class MyWidgetApiDetails extends StatelessWidget {
final Emp emp;
const MyWidgetApiDetails({Key key, #required this.emp}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DETAILS EMP'),
),
body: Column(
children: [
Hero(
tag: emp.empno,
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image.network(emp.image))
),
Container(
height: 20,
width: 100,
child: PhotoView(
imageProvider: AssetImage("assets/images/map.jpg"),
),
),
PhotoView(
imageProvider: NetworkImage(emp.image),
),
SizedBox(
height: 20,
),
Text(
emp.empno.toString(),
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
),
SizedBox(
height: 20,
),
Text(
emp.ename,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
)
],
),
);
}
}
When put Column in SingleChildScrollView get RenderFlex object was given an infinite size during layout.
You can do by just changing from Column to ListView.
class MyWidgetApiDetails extends StatelessWidget {
final Emp emp;
const MyWidgetApiDetails({Key key, #required this.emp}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DETAILS EMP'),
),
body: ListView(
children: [
Hero(
tag: emp.empno,
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image.network(emp.image))
),
Container(
height: 20,
width: 100,
child: PhotoView(
imageProvider: AssetImage("assets/images/map.jpg"),
),
),
PhotoView(
imageProvider: NetworkImage(emp.image),
),
SizedBox(
height: 20,
),
Text(
emp.empno.toString(),
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
),
SizedBox(
height: 20,
),
Text(
emp.ename,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
)
],
),
);
}
}
Put Column in SingleChildScrollView and wrap PhotoView in Container solve my problem.
That is because the SingleChildScrollView wants to be as large as possible, you need to put it into something that has constraints, like a Column, you could do something like this:
//pseudocode, not sure about the exact syntax.
body: Expanded(
child: SingleChildScrollView(
child: Column(...)
)
]
)
And I believe it should work.
Take a look here and here for more information.