I have the following very simple Flutter code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
)
);
}
}
I expect to see an app with a green background, and 2 square boxes in a line.
However, I only seem to see the green background and no boxes.
I have also experimented with mainAxisAlignment and crossAxisAlignment.
What do I seem to be doing wrong?
Docs:
https://api.flutter.dev/flutter/widgets/Row-class.html
You need to wrap your container in a WidgetsApp or the more commonly used MaterialApp or CupertinoApp that builds upon WidgetsApp. These widgets provide the default configuration required for a flutter application. Like navigation behaviour and such, see this link for more info: https://api.flutter.dev/flutter/widgets/WidgetsApp/WidgetsApp.html
An working examples:
import 'package:flutter/material.dart';
//import 'package:flutter/cupertino.dart'; // If using CupertinoApp
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return WidgetsApp( // Alternatively replace with MaterialApp or CupertinoApp
color: Colors.white,
builder: (context, widget) {
return Container(
color: Colors.green,
child: Row(
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
)
);
},
);
}
}
Hope this helps :-)
I ran your code and it raised the exception
Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined.
I added textDirection to Row and it ran as expected
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
width: double.infinity,
height: double.infinity,
color: Colors.green,
child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
),
Container(
width: 100,
height: 100,
color: Colors.blue,
),
],
),
),
),
),
);
}
}
Related
I have a Column: child: Column( children: [ Container( height: 200, width: 300, color: Colors.red, ), Align( alignment: Alignment.bottomCenter, child: Text("This Product is Built By Imtiaz")), ], ),
I want to Expand Column to print Text At Bottom Of Screen
You could put a Spacer() between your Text widget and your Container widget
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
children: [
Container(
height: 200,
width: 300,
color: Colors.red,
),
Spacer(),
Text("This Product is Built By Imtiaz"),
],
),
),
),
);
}
}
I'm trying to center both texts, and for the T text to take as much height as possible from the parent so the only space remaining will be the container's paddings.
The code:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(TestApp());
}
class TestApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
// Remove status bar
SystemChrome.setEnabledSystemUIOverlays([]);
return MaterialApp(
home: Scaffold(
body: _body(),
),
);
}
Widget _body() => Container(
width: 200,
height: 200,
padding: EdgeInsets.all(16),
color: Colors.lightBlueAccent,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Container(
color: Colors.lightGreen,
child: FittedBox(
fit: BoxFit.contain,
child: Text("T"),
),
),
),
Flexible(
child: Text("subtitle"),
)
],
),
);
}
As you can see in the screenshot there are undesired paddings in the T text:
The following flutter example results in the green box and text actually exceeding the constraints of the parent container. Why does this happen in this case? Basically the column is exceeding its incoming parent constrained height of 400, and seems to be using the top parent container height when making decisions on where to layout its children. Also the black border is not visible?
I have also tried using sizedbox, constrainedbox and the result is the same.
This is purely to understand the reason why this happens in more detail.
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) {
return Container(
width: double.infinity,
height: double.infinity,
color: Colors.red,
child: Container(
height: 400,
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
constraints: BoxConstraints(maxHeight: 400),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 50,
width: 50,
child: Container(
color: Colors.green,
)
),
Text("Test")
]
)
)
);
}
}
I know it's over a year since this issue was raised, but I recently ran into a very similar issue, and (by experimenting) I found a solution
My problem can be better demonstrated by having the Column contain multiple Text entries that are multiple lines long, and the overflow marker appears, but the text continues on the screen
The actual solution seems quite simple but not immediately obvious... wrap the Text entries in a Flexible!
SizedBox(
height: 131,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text1,
),
Flexible(
child: Text(
text2,
style: const TextStyle(
fontWeight: FontWeight.w400,
fontSize: 10,
),
),
),
],
),
);
I have one problem.
My app is
SingleChildScrollView -> column -> children: A,B(variable height),C
I expcted
If A,B,C not overflow.
C position is bottom of screen.
If A,B,C overflow.
C position is tail of content.
but it's not work.
I can't use flexible, spacer..etc.. because SingleChildScrollView
How can do that?
as an option
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(child: SomeWidget()),
),
);
}
}
class SomeWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: Container(
height: 340,
color: Colors.red[200],
),
),
SliverToBoxAdapter(
child: Container(
height: 620,
color: Colors.orange[200],
),
),
SliverFillRemaining(
hasScrollBody: false,
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 80,
color: Colors.green[200],
),
),
),
],
);
}
}
Hi guys i'm try to make this on flutter: img
i'm try with that code, but work
Container(
child: Positioned(top:0, left:0)
)```
as an option you can use Stack as container for card widget + image
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Stack(
children: <Widget>[
Card(
color: Colors.blueAccent,
child: Container(
width: 300,
height: 80,
),
),
FractionalTranslation(
translation: Offset(0, -0.5),
child: Icon(Icons.android, size: 48),
),
],
),
),
),
);
}
}