How to wrap text in Positioned child? - flutter

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.

Related

Neither Positioned's width or SizedBox's width works inside a stack widget - Flutter

#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Title Text'),
),
body: SafeArea(
child: Stack(children: [
Column(
children: const [
Text('short content'),
],
),
const Positioned(
top: 100,
width: 320,
child: SizedBox(
width: 320,
height: 50,
child: ColoredBox(color: Colors.red),
))
])),
);
I need the red box taking almost the entire screen with, but it flows the text width. How should I make this work?
Wrap your Stack with SizedBox:
SizedBox(
width: double.infinity,
child: Stack(
clipBehavior: Clip.none,
children: [
SizedBox(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text('short content'),
],
),
),
Positioned(
top: 100,
right: 0,
left: 0,
child: Container(
height: 50,
color: Colors.red,
))
],
),
),
You can wrap all children of the stack with either Align or Positioned so that the Stack can properly lay them out.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Title Text'),
),
body: SafeArea(
child: Stack(
children: [
/// The column is now positioned.
Positioned(
top: .0,
left: .0,
child: Column(
children: const [
ColoredBox(color: Colors.amber, child: Text('short content'))
],
),
),
const Positioned(
top: 100.0,
width: 320.0,
height: 50.0,
child: ColoredBox(color: Colors.red),
)
],
),
),
);
}
Output:

Flutter - Why Material elevation not working?

Material elevation in Column not working.
On this code elevation not working.
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
height: 50,
color: Colors.yellowAccent,
),
),
Container(
height: 50,
color: Colors.white,
)
],
),
);
But just remove Container is working. why?
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
height: 50,
color: Colors.yellowAccent,
),
),
],
),
);
has next Container.
remove next Container.
Include SingleChildScrollView and some content message.
It looks like a little shadow appears in the background, but not in the content.
update simple code:
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
elevation: 0,
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
alignment: Alignment.center,
height: 50,
color: Colors.yellowAccent,
child: const Text('Some title message'),
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
for (var i = 0; i < 20; i++)
Container(
alignment: Alignment.center,
height: 100,
color: Colors.white,
// Divider
margin: const EdgeInsets.only(bottom: 8),
child: const Text('Some content'),
),
],
),
))
],
),
)
I think you can use Stack instead of Column if you want the shadow of Material was visible even if you scroll the list. And add transparent Container on the top of the list.
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
elevation: 0,
),
// Use stack instead of column
body: Stack(
children: [
SingleChildScrollView(
child: Column(
children: [
// Add transparent container
Container(
alignment: Alignment.center,
height: 60,
color: Colors.transparent,
),
for (var i = 0; i < 20; i++)
Container(
alignment: Alignment.center,
height: 100,
color: Colors.white,
// Divider
margin: const EdgeInsets.only(bottom: 8),
child: const Text('Some content'),
),
],
),
),
Material(
elevation: 8,
child: Container(
alignment: Alignment.center,
height: 50,
color: Colors.yellowAccent,
child: const Text('Some title message'),
),
)
],
),
);

SingleChildScrollView not working in a nested Column. My second column is not scrolling using SingleChildScrollView

SingleChildScrollView not working in a nested Column. My second column is not scrolling using SingleChildScrollView
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: [
Container(
height: 200,
),
Container(
child: SingleChildScrollView(
child: Column(
children: [
Container(
color: Colors.purple,
height: 200,
),
Container(
color: Colors.orange,
height: 200,
),
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
],
),
),
),
],
),
),
);
}
In the second column is not scrolling when i using SingleChildScrollView
Try to wrap Container into Expanded Widget. If it doesn't work try to wrap either SingleChildScrollView or second Column
You should use Expanded as a parent of SingleChildScrollView:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: [
Container(
height: 200,
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Container(
color: Colors.purple,
height: 200,
),
Container(
color: Colors.orange,
height: 200,
),
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
],
),
),
),
],
),
),
);
}

Align stack elements flutter

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

The 'body' part is not visible after using ' bottomNavigationBar' in Flutter

Whenever i use the bottomNavigationBar: it dose not show the body: part on screen but when i remove the bottomNavigationBar: then it shows the body:
Here is the code
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home', textAlign: TextAlign.center),
actions: <Widget>[],
backgroundColor: Color(0xffd81b60),
),
bottomNavigationBar: _getNavBar(context),
body: ListView(children: <Widget>[
SizedBox(height: 300.0),
Container(
height: 50,
width: 10,
child: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => mealwisePage()));
},
color: Colors.pink,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Meal Wise',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, color: Colors.white),
),), ), ), ), ]),);}
_getNavBar(context) {
return Stack(
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.pink,
Colors.pink.shade800,
])), ),),),],);}
No error is showing just body is invisible on the screen
Any Solution Please?
I fount that it is because of using Stack it overlaps with body so i changed it
from:
return Stack(
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(),),)],)
To
return Container(
height: 90,
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(),),),],),)
bottom navigation is part of scaffold constructor Widget bottomNavigationBar and not the body hence refactor your code as shown below
Scaffold(
appBar: AppBar(
title: const Text('Home', textAlign: TextAlign.center),
backgroundColor: Color(0xffd81b60),
),
resizeToAvoidBottomPadding: false,
body: ListView(children: <Widget>[
Container(
child:Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15.0, 40.0, 40.0, 25.0),
child: Center(
child: Text(
'Home',
textAlign: TextAlign.center,
),),), ], ),),
SizedBox(height: 200.0),
]),//listview
bottomNavigationBar: _getNavBar(context),
);//scaffold
update edit
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
you must give a value to this width: MediaQuery.of(context).size.width,
example width: MediaQuery.of(context).size.width * .98,