handling application layout in flutter app - flutter

I have Entry page to my app like below, I am getting bottom overflow , image size issue , text font issue, when I run on the phone with less than 5 inches,
If I run the same app on over 5 inches I am getting like below
Can anyone who have worked on developing flutter apps help me on how I can adjust according to the screen?
also How to adjust text size, image size every other things as per the screen size?
below is my code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Color colorTheme;
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
TextEditingController controllerEmail = TextEditingController();
TextEditingController controllerPassword = TextEditingController();
String username, password;
Widget loginButtonChild = const Text(
"Log in",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white, fontFamily: "OpenSans-Regular", fontSize: 16),
);
Widget loginButtonWithCircle = Row(
children: <Widget>[
const Text(
"Log in",
style: TextStyle(
color: Colors.white,
fontFamily: "OpenSans-Regular",
),
),
CircularProgressIndicator(),
],
);
#override
Widget build(context) {
double maxHeight = MediaQuery.of(context).size.height;
// ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
return Scaffold(
resizeToAvoidBottomPadding: true,
body: SingleChildScrollView(
child: LimitedBox(
maxHeight: maxHeight * 1,
child: Stack(
//fit: StackFit.expand,
children: <Widget>[
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.05,
left: MediaQuery.of(context).size.width * 0.05),
child: Image.asset('assets/Heat Map.png',
width: 100, height: 20, fit: BoxFit.fill),
)
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(15))),
padding: EdgeInsets.all(
// MediaQuery.of(context).size.width * 0.09,
MediaQuery.of(context).size.width * 0.1,
),
child: Image.asset(
'assets/layer_1_3.png',
// color: Color(0xFFe31735),
// width: 300,
width: MediaQuery.of(context).size.width * 0.72,
// height: 300,
height: MediaQuery.of(context).size.height * 0.5,
fit: BoxFit.contain,
),
),
Positioned(
left: 78.0,
bottom: 10.0,
child: RichText(
text: TextSpan(
text: 'APP',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
children: <TextSpan>[
TextSpan(
text: '\nDevelopment',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
),
),
TextSpan(
text: '\nFlutter',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
),
),
],
),
maxLines: 3,
overflow: TextOverflow.ellipsis,
softWrap: true,
),
),
],
),
SingleChildScrollView(
child: new Container(
padding: const EdgeInsets.fromLTRB(20.0, 0.0, 40.0, 40.0),
child: new Form(
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Icon(Icons.person_outline,
size: 38, color: Colors.black),
),
SizedBox(width: 10.0),
new Expanded(
child: new TextFormField(
style: TextStyle(color: Colors.black),
controller: controllerEmail,
//cursorColor: , make it yellow later TODO
decoration: new InputDecoration(
labelText: "Username",
enabledBorder: new UnderlineInputBorder(
borderSide: new BorderSide(
color: Colors.red,
))),
keyboardType: TextInputType.text,
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Icon(
Icons.lock_outline,
size: 38,
color: Colors.black,
),
),
SizedBox(width: 10.0),
new Expanded(
child: new TextFormField(
controller: controllerPassword,
style: TextStyle(color: Colors.red),
decoration: new InputDecoration(
labelText: "Password",
enabledBorder: new UnderlineInputBorder(
borderSide: new BorderSide(
color: Colors.red,
))),
obscureText: true,
keyboardType: TextInputType.text,
),
),
],
),
Padding(
padding: EdgeInsets.only(
top: 0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
},
child: Text(
"Forgot password?",
style: TextStyle(
color: Colors.grey,
fontFamily: "OpenSans-Regular",
),
),
)
],
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.04,
),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(25.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 48),
child: loginButtonChild,
),
const SizedBox(
width: 45.0,
height: 45.0,
),
Icon(Icons.arrow_forward,
color: Colors.white),
],
),
onPressed: () {
}),
],
),
),
),
),
],
),
],
),
),
),
);
}
}

You can use SingleChildScrollView to avoid overflow error just wrap your widget with SingleChildScrollView
Example:-
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child:YourWidget();
)
)

Related

flutter : how to make transparent

How to make it transparent which is I circled.
Scaffold(
appBar: AppBar(
title: Text("Add New Address",
style: TextStyle(
fontSize: t1Size,
fontWeight: FontWeight.w700,
color: Colors.white)),
),
bottomNavigationBar: saveButton(context),
backgroundColor: Colors.white.withOpacity(0.9),
body: Container(
child: Column(
// alignment: Alignment.topCenter,
children: [
Expanded(
child: MapPicker(
// pass icon widget
iconWidget: Icon(
Icons.location_on,
size: 40,
color: appGreenColor,
),
//add map picker controller
mapPickerController: mapPickerController,
child: GoogleMap(
myLocationEnabled: true,
zoomControlsEnabled: false,
// hide location button
myLocationButtonEnabled: true,
mapType: MapType.normal,
indoorViewEnabled: false,
// camera position
initialCameraPosition: cameraPosition,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
onCameraMoveStarted: () {
// notify map is moving
mapPickerController.mapMoving!();
textController.text = "checking ...";
},
onCameraMove: (cameraPosition) {
this.cameraPosition = cameraPosition;
},
onCameraIdle: () async {
// notify map stopped moving
mapPickerController.mapFinishedMoving!();
//get address name from camera position
List<Placemark> placemarks = await placemarkFromCoordinates(
cameraPosition.target.latitude,
cameraPosition.target.longitude,
);
fLocationName = placemarks.first.name;
setState(() {
fLocationName;
});
// update the ui with the address
textController.text =
'${placemarks.first.name} ,${placemarks.first.subLocality}, ${placemarks.first.locality}, ${placemarks.first.administrativeArea}, ${placemarks.first.country}';
},
),
),
),
Container(
// height: MediaQuery.of(context).size.height - 400,
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30))),
child: SingleChildScrollView(
child: Column(children: [
SizedBox(
height: 25,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Select Your Location",
style: TextStyle(
fontSize: t2Size,
fontWeight: FontWeight.w700,
),
),
],
),
),
SizedBox(
height: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
children: [
Icon(
Icons.location_on_outlined,
color: appYellowColor,
size: 20,
),
SizedBox(
width: 6,
),
Text(
fLocationName == null ? "" : '$fLocationName',
style: TextStyle(
color: appYellowColor,
fontSize: t3Size,
fontWeight: FontWeight.w500,
),
),
],
),
),
SizedBox(
height: 5,
),
Container(
padding: const EdgeInsets.only(left: 46.0, right: 20),
child: Text(
'${textController.text}',
style: TextStyle(
color: extraLightColor,
fontSize: t4Size,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.start,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
Column(
children: [
SizedBox(
height: 15,
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Save This Address As",
style: TextStyle(
fontSize: t2Size,
fontWeight: FontWeight.w700,
),
),
],
),
),
SizedBox(
height: 10,
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 15.0),
child: Row(
children: [
Container(
width: MediaQuery.of(context).size.width - 30,
decoration: BoxDecoration(
color: extraLightColor.withOpacity(0.1),
borderRadius: BorderRadius.circular(4),
//color: Colors.white,
),
padding: const EdgeInsets.only(left: 12.0),
child: Center(
child: TextFormField(
controller: _saveAddressAsController,
autocorrect: false,
style: TextStyle(
color: apptitleColor,
fontWeight: FontWeight.w400,
fontSize: t4Size),
decoration: InputDecoration(
border: UnderlineInputBorder(
borderSide: BorderSide.none),
hintText: "Home/ Work/ Other",
prefixIconConstraints: BoxConstraints(
minWidth: 16,
minHeight: 16,
),
//isDense: true,
),
//controller: _controller1,
maxLines: 1,
// onChanged: searchSalon,
),
),
)
],
),
),
SizedBox(
height: 10,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Search Location",
style: TextStyle(
fontSize: t2Size,
fontWeight: FontWeight.w700,
),
),
],
),
),
SizedBox(
height: 15.0,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: searchBar(context),
),
],
)
],
),
]),
),
)
],
),
),
);
wrap your container with ClipRRect
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30)),
child:Container()
)
That color is coming from Scaffold's backgroundColor. For this kind of layout, Stack is more suitable than Column. Also in your snippet you can do a trick using Transform. But for click-event you might find some trouble. For this, use Container's transform for second Container that is holding second part.
Container(
transform: Matrix4.translationValues(0, -30, 0),//-30=y based on your need
you can try this suggestions
*first;
You can use>> Stack widget runs like a layer (maybe you know Photoshop etc..)
Stack( children: <Widget>[
//Container(), this show bottom
// Column(),
// google map
//TextField(), this show top
// any widget..
]),
*secondly;
Scaffold(
backgroundColor: Colors.transparent, //maybe it work
);

Different behaviours using `SingleChildScrollView` Flutter

I'm trying to make the whole OrderScreen slide up when the keyboard appears as I do in UserProfileScreen by wrapping the scaffold's container in a SingleChildScrollView, but doing so on this screen it throws various RenderBox was not laid out: _RenderScrollSemantics#abc84 relayoutBoundary=up4 NEEDS-PAINT errors, which are not thrown without SingleChildScrollView. I set up the two screen just the same apart from UserProfileScreen having a Column as a child of another Column. All column's children are wrapped in and Expanded widget with different flex values for layout purposes. Can you se why I do get these errors?
UserProfileScreen ( working ) :
child: Scaffold(
resizeToAvoidBottomInset: true,
body: SingleChildScrollView(
child: Container(
color: Colors.black54,
padding:
EdgeInsets.only(left: 100, right: 100, top: 30, bottom: 70),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 100,
),
Stack(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey[200],
width: 2,
),
borderRadius: BorderRadius.circular(100),
),
child: CircleAvatar(
radius: 50,
backgroundImage:
NetworkImage('${widget.user.shopLogoUrl}'),
),
),
],
),
SizedBox(
height: 35,
width: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Text(
AppLocalizations.instance.text('Shop name'),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white, fontSize: fontSize),
),
),
// SizedBox(width: 50),
Expanded(
flex: 2,
child: TextField(
controller: _nameController,
style: TextStyle(
color: Colors.black87,
fontSize: fontSize,
fontWeight: FontWeight.w500),
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.all(5),
enabled: false,
hintText: AppLocalizations
.instance
.text(
'Shop name placeholder'),
//'nome',
hintStyle: TextStyle(
fontSize: fontSize,
color: Colors.grey[350]),
border: OutlineInputBorder(),
// focusColor: Colors.lightGreenAccent,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1,
),
),
fillColor: Colors.white,
filled: true),
cursorColor: Colors.black,
),
),
],
),
...
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
// TODO cancel button
RaisedButton(
color: Colors.redAccent,
child: Text(
AppLocalizations.instance
.text('Cancel'),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: fontSize),
),
onPressed: () {
BlocProvider.of<UserBloc>(context)
.add(LoadUser());
},
),
SizedBox(width: 20),
// TODO save button
RaisedButton(
color: Colors.green.shade400,
child: Text(
AppLocalizations.instance.text('Save'),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: fontSize),
),
onPressed: () {
BlocProvider.of<UserBloc>(context)
.add(SaveUser(
user: FixitUser(
shopName: widget.user.shopName,
email: widget.user.email,
phoneNumber:
_phoneNumberController.text,
uid: widget.user.uid,
shopLogoUrl:
widget.user.shopLogoUrl,
address: _addressController.text,
zipCode: _zipCodeController.text,
city: widget.cityUser,
region: widget.regionUser,
country: widget.countryUser,
vatNumber: _vatController.text,
homeLat: widget
.userLocation.latitude
.toString(),
homeLong: widget
.userLocation.longitude
.toString(),
lastLogin: DateTime.now()
.millisecondsSinceEpoch,
signUpDate: signUpDate ??
DateTime.now()
.millisecondsSinceEpoch),
));
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
child: Container(
padding: EdgeInsets.all(50),
child: new Column(
mainAxisAlignment:
MainAxisAlignment
.spaceEvenly,
mainAxisSize:
MainAxisSize.min,
children: [
new CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<
Color>(
Colors
.redAccent),
),
SizedBox(
height: 50,
),
new Text(
AppLocalizations
.instance
.text(
'Saving user profile'),
style: TextStyle(
fontSize: fontSize,
color:
Colors.black54),
),
],
),
),
);
});
}),
...
],
),
),
],
),
),
),
),
OrderScreen ( trowing error ) :
child: Scaffold(
resizeToAvoidBottomInset : true,
body: SingleChildScrollView(
child: Container(
color: Colors.black54,
padding:
const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 10,),
Expanded(
flex: 1,
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(5),
border: Border.all(
color: Colors.redAccent, width: 2),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
flex: 9,
child: TextField(
controller: _customerNameController,
style: TextStyle(
color: Colors.black87,
fontSize: fontSize,
fontWeight: FontWeight.w500),
decoration: InputDecoration(
isDense: true,
enabled: true,
hintText: AppLocalizations.instance
.text('Customer name placeholder'), //'nome',
hintStyle: TextStyle(
fontSize: fontSize, color: Colors.grey[400]),
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1,
),
),
fillColor: Colors.white,
filled: true),
cursorColor: Colors.black,
onChanged: (value){
BlocProvider.of<OrderBloc>(context).add(LoadOrdersForCustomer(_customerNameController.text));
},
),
),
SizedBox(width: 20,),
Expanded(
flex: 2,
child: RaisedButton(
color: Colors.redAccent,
child: Text(
AppLocalizations.instance.text('Cancel'),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white, fontSize: fontSize),
),
onPressed: () {
setState(() {
_customerNameController.text = '';
});
BlocProvider.of<OrderBloc>(context).add(LoadOrders(user: widget.user));
},
),
)
],
),
),
),
...
],
),
),
),
),
Progress
Adding height and width to the container got rid of the error, but the view doesn't slide up as UserProfileScreen instead does..with or without resizeToAvoidBottomInset: true, set in Scaffold.. the keyboard just covers up the screen..
child: Scaffold(
resizeToAvoidBottomInset: true,
body: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height-50,
width: MediaQuery.of(context).size.width,
color: Colors.black54,
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
child: Column(
So I have two different behaviours.
UserProfileScreen doesn't need Container's heigh and width and it slides up on keyboard appearing.
OrderScreen does need Container's height and width but doesn't slide up on keyboard appearing.

TextOverflow not working in Text Widget Flutter

i am trying to clip the description of the foodItem i have in a flutter app i tried adding 'overflow: TextOverflow.clip' to my Text() Widget, but that didn't really work... i'll share the specific Widget and then the full file..
Text('This is a very long description.....................',
overflow: TextOverflow.clip,
softWrap: true,
style: TextStyle(
fontSize: 9.0,
color: Colors.black38,
),
),
the full file..
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/material.dart';
import '../model/food.dart';
import '../style/theme.dart' as Style;
class FoodList extends StatelessWidget {
final foodItems = <Food>[
Food(
title: "Jimmy's Steak",
price: "34.00",
img: 'assets/icons/foods/food5.jpg',
rating: "4.2"
),
Food(
title: "Butter Steak",
price: "45.00",
img: 'assets/icons/foods/food6.jpg',
rating: "4.2"
),
Food(
title: "Sushi",
price: "10.00",
img: 'assets/icons/foods/food7.jpg',
rating: "4.7"
)
];
#override
Widget build(BuildContext context) {
return ListView(
scrollDirection: Axis.horizontal,
children: foodItems.map<Widget>((Food food) {
return GestureDetector(
onTap: () {
},
child: Padding(
padding: EdgeInsets.only(left: 20.0, top: 10.0, bottom: 20.0),
child: Container(
width: 170,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey[300], width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
height: 180.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(topLeft:Radius.circular(10), topRight:Radius.circular(10)),
image: DecorationImage(
image: AssetImage(food.img),
fit: BoxFit.cover
)
),
),
SizedBox(
height: 10.0,
),
Padding(
padding: const EdgeInsets.only(left: 10.0),
child: Text(
food.title,
style: TextStyle(
color: Colors.black,
fontSize: 12.0),
),
),
SizedBox(
height: 5.0,
),
Padding(
padding: const EdgeInsets.only(left:10.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('This is a very long description.....................',
overflow: TextOverflow.clip,
softWrap: true,
style: TextStyle(
fontSize: 9.0,
color: Colors.black38,
),
),
SizedBox(
width: 5.0,
),
/* Icon(EvaIcons.star, color: Style.Colors.mainColor, size: 8.0,),
Icon(EvaIcons.star, color: Style.Colors.mainColor, size: 8.0,),
Icon(EvaIcons.star, color: Style.Colors.mainColor, size: 8.0,),
Icon(EvaIcons.star, color: Style.Colors.mainColor, size: 8.0,),
Icon(EvaIcons.star, color: Colors.black38, size: 8.0,), */
SizedBox(
width: 5.0,
),
/* Text("(200)", style: TextStyle(
fontSize: 9.0,
color: Colors.black38
),),
*/ ],
),
Text( "\$" + food.price, style: TextStyle(
fontSize: 10.0,
color: Colors.black,
fontWeight: FontWeight.bold
),)
],
),
)
],
),
),
)
);
}).toList());
}
}
I tried using the ClipRect Widget as well, by wrapping my Text Widget into it but that still didn't work.. I am Kindo Of stuck here, any help would be much appreciated..
You can set a limit of String for text like below.
String text =
"This is a very long description.........his is a very long description.........his is a very long description.........his is a very long description.....................";
Text(
text.length > 30 ? '${text.substring(0, 30)}......' : text,
style: TextStyle(
fontSize: 9.0,
color: Colors.black38,
),
)
You could also check the below code.
Center(
child: Container(
padding: EdgeInsets.all(4.0),
color: Colors.lime,
width: 200.0,
child: Row(
children: <Widget>[
Flexible(
child: RichText(
overflow: TextOverflow.ellipsis,
strutStyle: StrutStyle(fontSize: 12.0),
text: TextSpan(
style: TextStyle(color: Colors.black),
text: 'A very long text :)'),
),
),
Container(
width: 100.0,
height: 100.0,
color: Colors.orangeAccent,
)
],
),
)),

MainAxisAlignment under SingleChildScrollView is not working

I developed a login screen in which first i get the render flex error when i open the keyboard so for that i wrap my widgets in SingleChildScrollView, but after that mainAxisAlignment of Column is not working but when i removed SingleChildScrollView then everything working fine except render flex error. i don't know what to do kindly help please.
Following is the code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class LoginScreenOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: [
Container(
height: double.infinity,
width: double.infinity,
color: Colors.blue,
),
SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(top: 70, left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.keyboard_arrow_up,
size: 30,
color: Colors.white,
),
Text(
"Login Screen",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold),
),
],
),
),
),
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * 0.6,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(top: 50, left: 20),
child: Text(
"Welcome",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
decoration: InputDecoration(
icon: Icon(Icons.email),
hintText: "Enter User Name"),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
obscureText: true,
decoration: InputDecoration(
icon: Icon(Icons.vpn_key),
hintText: "Enter Password"),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: Container(
width: double.infinity,
height: 50,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
side: BorderSide(width: 2, color: Colors.blue)),
textColor: Colors.blue,
child: Text(
"Sign In",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
onPressed: () {},
),
),
),
Center(
child: Text(
"Forgot Password",
style: TextStyle(color: Colors.grey),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: Container(
width: double.infinity,
height: 50,
child: FlatButton(
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
child: Text("Create Account"),
color: Colors.blue,
onPressed: () {},
),
),
)
],
),
)
],
),
),
],
),
));
}
}
You can try this code blocks
CustomScrollView(
scrollDirection: Axis.vertical,
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('test'),
for (int i = 0; i < 10; i++) const FlutterLogo(size: 80)
],
),
),
],
),
After wrapping Column with SingleChildScrollView wrap it with Center widget.
Set the alignment of the outer container, then wrap the column with singleChildScrollView.
Container(
alignment: Alignment.center, //Set container alignment then wrap the column with singleChildScrollView
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: []
),
);
Yeah MainAxisAlignment' property of a 'Column' does not work when you wrap the 'Column' in 'SingleChildScrollView'. I dont know the reason, but thats how it is.
I use 'SizedBox(height: xx)' to give space between widgets inside the 'Column' when i absolutely need a scroll view, otherwise i tend not to use the 'SingleChildScrollView'.
you can Column wrap with a Container...
full code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class LoginScreenOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: [
Container(
height: double.infinity,
width: double.infinity,
color: Colors.blue,
),
SingleChildScrollView(
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(top: 70, left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.keyboard_arrow_up,
size: 30,
color: Colors.white,
),
Text(
"Login Screen",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold),
),
],
),
),
),
),
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * 0.6,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(top: 50, left: 20),
child: Text(
"Welcome",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
decoration: InputDecoration(
icon: Icon(Icons.email),
hintText: "Enter User Name"),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: TextField(
obscureText: true,
decoration: InputDecoration(
icon: Icon(Icons.vpn_key),
hintText: "Enter Password"),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: Container(
width: double.infinity,
height: 50,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
side: BorderSide(width: 2, color: Colors.blue)),
textColor: Colors.blue,
child: Text(
"Sign In",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
onPressed: () {},
),
),
),
Center(
child: Text(
"Forgot Password",
style: TextStyle(color: Colors.grey),
),
),
Padding(
padding: const EdgeInsets.all(20),
child: Container(
width: double.infinity,
height: 50,
child: FlatButton(
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)
),
child: Text("Create Account"),
color: Colors.blue,
onPressed: () {},
),
),
)
],
),
)
],
),
),
],
),
));
}
}
This will work...
Set the alignment of the outer container as alignment.bottomCenter, then wrap the column with singleChildScrollView. It will definitely works. Thankyou.

How to create Column in Center of App with Left/Right margins?

im new to Flutter. I have started learning a few days ago and am trying to grasp the concept of Rows and Columns.
I made a simple Page like this.
To explain my code I first make a Column to put everything in.
Then i use a Row for the TopBar, and then another Row to put the things into the body, so that i can put a Column in the center of the Page, with a bit of space on both sides of it. I then pack Text and Button in a Column and insert it into the Column in the Middle of the Page.
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: MainPage(),
));
class MainPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
Color Color1 = const Color.fromRGBO(204, 126, 185, 100);
Color Color2 = const Color.fromRGBO(140, 235, 203, 100);
Color Color3 = const Color.fromRGBO(227, 225, 204, 100);
Color Color4 = const Color.fromRGBO(89, 130, 145, 100);
return Scaffold(
body: Container(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(20.0, 50.0, 0, 0),
child: SizedBox(
child: Image.asset('assets/MenuIcon.png'),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 50.0, 20.0, 0),
child: SizedBox(
child: Image.asset('assets/SearchIcon.png'),
),
),
],
),
Divider(height: 50,),
Expanded(
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(),
),
Expanded(
flex: 5,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Erwachsen werden",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: Color1,
),
),
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Glückliches Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: Color2,
),
),
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Ab in das Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: Color3,
),
)
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Alleine Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: Color4,
),
),
),
],
),
],
),
),
Expanded(
flex:1,
child: Container(),
),
],
),
),
],
),
),
);
}
}
I feel like there is a lot of unnecessary Coding, but i can't seem to be able to improve it, with it working properly.
Can anybody Help improve my code?
Simply what i want to achieve is a Column in the middle of the body with margin to the left and right of the screen, without a million lines of code.
Scaffold by default has an parameter for AppBar() use that for your app bar
and as per your layout I will suggest to use ListView() instead of Column()
using Listview will automatically scroll your page if length of your page extends
and also has an parameter as padding using which you can add space on your left and right side
refer below mentioned code structure
Scaffold(
appbar: AppBar(),
body: ListView(
padding: EdgeInsets.only(left:12.0,right:12.0),
children: <Widget>[
//your list of widgets here
],
)
)
Try this:
Code example
Center(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 10),
height: 400,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Material(
color: Colors.transparent,
child: Image.asset(
"assets/images/logo.png",
height: 100,
width: 200,
),
),
//email
TextFormField(
style: TextStyle(
fontFamily: "Light",
color: Theme.of(context).primaryColor,
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
cursorColor: Theme.of(context).primaryColor,
decoration: InputDecoration(
labelText: 'Username',
filled: true,
fillColor: Colors.white,
),
),
TextFormField(
style: TextStyle(
fontFamily: "Light",
color: Theme.of(context).primaryColor,
),
keyboardType: TextInputType.visiblePassword,
textInputAction: TextInputAction.done,
cursorColor: Theme.of(context).primaryColor,
obscureText: passwordVisible,
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
filled: true,
fillColor: Colors.white,
onPressed: () {},
),
),
),
Container(
child: RaisedButton(
onPressed: () {},
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontFamily: "Light",
),
),
),
),
],
)),
)
I removed unnecessary code ! It may help you !
Color color1 = const Color.fromRGBO(204, 126, 185, 100);
Color color2 = const Color.fromRGBO(140, 235, 203, 100);
Color color3 = const Color.fromRGBO(227, 225, 204, 100);
Color color4 = const Color.fromRGBO(89, 130, 145, 100);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.white,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(20.0, 50.0, 0, 0),
child: SizedBox(
// child: Image.asset('assets/MenuIcon.png'),
child:Icon(Icons.menu,color:Colors.black)
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 50.0, 20.0, 0),
child: SizedBox(
// child: Image.asset('assets/SearchIcon.png')
child:Icon(Icons.search,color:Colors.black)
),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left:50,right:50),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize:MainAxisSize.min,
children: <Widget>[
Text(
"Erwachsen werden",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: color1,
child: Text('Button')),
),
),
SizedBox(height: 10.0),
Text(
"Glückliches Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: color2,
child: Text('Button')),
),
),
SizedBox(height: 10.0),
Text(
"Ab in das Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: color3,
child: Text('Button')),
)),
SizedBox(height: 10.0),
Text(
"Alleine Leben",
style: TextStyle(
fontWeight: FontWeight.w200,
color: Colors.black,
fontSize: 28.0,
),
),
SizedBox(height: 10.0),
SizedBox(
width: double.infinity,
child: ButtonTheme(
minWidth: 300,
height: 70,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(13.0),
),
onPressed: () {},
color: color4,
child: Text('Button')),
),
),
],
),
),
),
],
),
),
);
}
}