In the following code
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) {
final text = Text(
'When large fortunes fall into the hands of some people, they become strong in the light of it, and in the shadow of strength and wealth they dream that they can live out of their homeland and be happy and proud, they will soon realize that they have made mistakes, and the greatness of every nation On the ruins of his homeland and cease!',
style: Theme.of(context).textTheme.headline4);
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: [
Column(
children: [
text,
],
),
],
)
],
);
}
}
I have a Text widget in a Column in Row in another Column , As text is in Row it is single row and overflowed, I want to make it multiline to I wrap it with Flexible
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: [
Column(
children: [
Flexible(child: text),
],
),
],
)
],
);
What is wrong here? Why I have RenderFlex children have non-zero flex but incoming height constraints are unbounded. ?
You must wrap column with flexible, not text itself.
Please try this. It's working for me.
Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: [
Flexible(child: Column(
children: [
text
],
),)
],
)
],
)
Related
Is there any way to achieve text baseline with VericalDivider() where the Rows() under the Column() and required IntrinsicHeight() to make the VerticalDiver() render properly.
Meanwhile, look like there is limitation around CrossAxisAlignment.baseline and IntrinsicHeight https://github.com/flutter/flutter/issues/71990#issuecomment-747141796
From the picture below, I can't use CrossAxisAlignment.baseline with IntrinsicHeight Widget so the text that has different size will look something like this. But if I remove IntrinsicHeight the VericalDivider Widget will not render. (Height = 0)
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("hey"),
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(child: Text("aaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")),
VerticalDivider(
width: 50,
thickness: 50,
),
Expanded(child: Text("bbbbbb", style: TextStyle(fontSize: 30),))
],
),
),
],
),
);
}
}
So i tried to make my TextWidget aligned to left but when i put TextWidget above ( before ) TextFieldWidget it's automatically become centered following with the TextFieldWidget, here my code
import 'package:flutter/material.dart';
void main() {
runApp(new MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(
child: Text("SQLite Application"),
),
),
body: Column(
children: <Widget>[
SizedBox(
height: 20,
),
Text("Name of Things"),
TextField()
],
),
),
);
}
}
and here is the result :
Code Output
Is there a solve to make the TextWidget to aligned to the left ? I've tried to use TextAlign.left on TextWidget but it wont change.
Use
crossAxisAlignment: CrossAxisAlignment.start,
on the Column
You need to set the mainAxisAlignment to Start
Column(
crossAxisAlignment: CrossAxisAlignment.start
In case you want to place the contents at the Right Side, you can use
Column(
crossAxisAlignment: CrossAxisAlignment.end
just add
Column(
crossAxisAlignment: CrossAxisAlignment.start,
the result
You put your text inside Column widget. Column by default crossAxisAlignment is centre. That's why your text is always on centre.
for example
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:const [
SizedBox(
height: 20,
),
Text("your text....!"),
TextField()
],
),
And if you wanna align text without Column, use Align with with alignment.topleft. It will move on left side
Align(
alignment: Alignment.topLeft,
child: Text("Name of Things", textAlign: TextAlign.left,),
),
suppose we have:
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Wrap(
children: [
Row(
children: [
Text('a'),
],
),
Row(
children: [
Text('b'),
],
),
],
);
}
}
the output of this would be:
notice how each of the Row's is taking a whole line
how can I fix this and make them side by side?
you should wrap your Row's with FittedBox, which will make them only consume the space they need, so your code should be like this:
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Wrap(
children: [
FittedBox(
child: Row(
children: [
Text('a'),
],
),
),
FittedBox(
child: Row(
children: [
Text('b'),
],
),
),
],
);
}
}
and your output will be like this:
Here is my flutter code:
class LandingScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
LandingPageForm(),
],
),
);
}
}
class LandingPageForm extends StatefulWidget {
#override
LandingPageFormState createState() {
return LandingPageFormState();
}
}
final headerStyle = TextStyle();
class LandingPageFormState extends State<LandingPageForm> {
final _formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Container(
color: Colors.grey,
padding: EdgeInsets.all(10),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: Colors.amber,
child: FittedBox(
child: Text(
'Hey there!',
style: headerStyle,
),
fit: BoxFit.fitWidth,
),
),
],
),
));
}
}
If I remove the headerStyle from the fitted textbox, the padding above disappears which is how I want it. As soon as I add a style though this whitespace appears and I have no idea what is causing it.
What is going on?
Closing this - it seems there is a persistent caching issue with styles on my device. Closing and reopening the app fixed the issue.
"I'm a beginner in flutter, I want to display two text in row .where "hello" is the first text widget and "world" is another text widget
I tried doing this with my basic knowledge but I came across these errors
Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined.
'package:flutter/src/rendering/flex.dart':
Failed assertion: line 439 pos 18: 'textDirection != null'
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Row(
children: <Widget>[
Text("hello",textDirection: TextDirection.rtl,),
Text("world",textDirection: TextDirection.rtl,)
],
);
}
}
Maybe you want to use RichText instead?
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: 'hello', style: TextStyle(color: Colors.red)),
TextSpan(text: ' world', style: TextStyle(color: Colors.blue)),
],
),
)
Please read this link about Row in Flutter:
https://medium.com/jlouage/flutter-row-column-cheat-sheet-78c38d242041
In a nutshell, you Declare a Row and the items that will be inside that row are the children (Which must be widgets).
I picked an example from here
https://flutter.dev/docs/development/ui/layout
something like this
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Image.asset('images/pic1.jpg'),
Image.asset('images/pic2.jpg'),
Image.asset('images/pic3.jpg'),
],
);
In Your case, you just have to erase the image.asset and change it for texts...
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Hello'),
Text('World'),
],
);
or whatever you want.
Please try below code:-
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Hello",style: TextStyle(color: Colors.blue)),
Text("World",style: TextStyle(color: Colors.blue))
],
);
}
**Try This Simplest Way**
import 'package:flutter/material.dart';
class Example extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Row(
children: <Widget>[
Text("Hello"),
Text("World"),
],
),
),
);
}
}