crossAxisAlignment: CrossAxisAlignment.end doesn't work - flutter

I am pretty new to programming and also to Flutter, so please excuse me if my question is silly. I am trying to push Skip button to the bottom left corner of the screen. But the crossAxisAlignment: CrossAxisAlignment.end code doesn't work. How could I do this the right way so it can adapt to all screen formats properly? The problematic part is at the bottom of the entire code. Thank you in advance!
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget {
const MainPage({Key? key}) : super(key: key);
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(left: 15, top: 30),
width: 230,
decoration: BoxDecoration(),
child: Image.asset('images/logo1.png'),
),
Container(
margin: EdgeInsets.only(left: 15, top: 30),
child: Text(
'Login',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
),
Container(
margin: EdgeInsets.only(left: 15, right: 15, top: 30),
height: 50,
width: double.infinity,
child: Text(
'E-mail',
style: TextStyle(fontSize: 20, color: Colors.black54),
),
padding: EdgeInsets.only(top: 10, left: 15, bottom: 10),
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.black54,
),
borderRadius: BorderRadius.all(
Radius.circular(7),
),
),
),
Container(
margin: EdgeInsets.only(left: 15, right: 15, top: 15),
height: 50,
width: double.infinity,
child: Text(
'Password',
style: TextStyle(fontSize: 20, color: Colors.black54),
),
padding: EdgeInsets.only(top: 10, left: 15, bottom: 10),
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.black54,
),
borderRadius: BorderRadius.all(
Radius.circular(7),
),
),
),
Container(
margin: EdgeInsets.only(left: 15, right: 15, top: 15),
child: MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
height: 50.0,
minWidth: double.infinity,
color: Colors.orangeAccent,
child: Text(
"Login",
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () => {},
splashColor: Colors.redAccent,
),
),
Container(
margin: EdgeInsets.only(top: 5),
child: Center(
child: TextButton(
style: TextButton.styleFrom(
textStyle: TextStyle(fontSize: 17),
),
onPressed: () {},
child: Text(
'Forgot your password?',
style: TextStyle(color: Colors.black54),
),
),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
margin: EdgeInsets.only(top: 5),
child: TextButton(
style: TextButton.styleFrom(
textStyle: TextStyle(fontSize: 17),
),
onPressed: () {},
child: Text(
'Skip',
style: TextStyle(color: Colors.black54),
),
),
),
],
),
],
),
);
}
}
Mobile Display

Remove Row(). Add alignment: Alignment.centerRight, to your Container.
Container(
margin: EdgeInsets.only(top: 5),
alignment: Alignment.centerRight,
child: TextButton(
style: TextButton.styleFrom(
textStyle: TextStyle(fontSize: 17),
),
onPressed: () {},
child: Text(
'Skip',
style: TextStyle(color: Colors.black54),
),
),
),

To change the position for Row children horizontally
you need to use mainAxisAlignment property
mainAxisAlignment property:
How the children should be placed along the main axis.
For example, MainAxisAlignment.start, the default, places the children at the start (i.e., the left for a Row or the top for a Column) of the main axis.
source flutter https://api.flutter.dev/flutter/widgets/Flex/mainAxisAlignment.html

You can use Spacer widget before Row. By default, row children will start from left.
///others widgets
const Spacer(), /// just add this
Row(....
More about Spacer widget.

Related

How do I put a button in a block in flutter?

Used expanded, container. Does not work. I tried to put the Elevated Button in Expanded. And all the time that child can't use something else. Most likely, I don't quite understand the structure. I tried to put the Elevated Button in Expanded.
import 'package:percent_indicator/percent_indicator.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: Pomodoro(),
));
}
class Pomodoro extends StatefulWidget {
const Pomodoro({Key? key}) : super(key: key);
#override
State<Pomodoro> createState() => _PomodoroState();
}
class _PomodoroState extends State<Pomodoro> {
double percent = 0;
// ignore: non_constant_identifier_names, unused_field
static int TimeInMinut = 25;
// ignore: non_constant_identifier_names
int TimeInSec = TimeInMinut = 60;
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xff1542bf), Color(0xff51a8ff)],
begin: FractionalOffset(0.5, 1)),
),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.only(top: 25.0),
child: Text(
'Pomodoro Clock',
style: TextStyle(color: Colors.white, fontSize: 40.0),
),
),
Expanded(
child: SizedBox(
height: 20.0,
width: 50.0,
child: CircularPercentIndicator(
percent: percent,
animation: true,
animateFromLastPercent: true,
radius: 90.0,
lineWidth: 20.0,
progressColor: Colors.white,
center: Text(
'$TimeInMinut',
style: const TextStyle(
color: Colors.white, fontSize: 30.0),
),
),
),
),
Expanded(
child: Container(
width: double.infinity,
decoration: const BoxDecoration(
color: Color.fromARGB(255, 163, 48, 48),
borderRadius: BorderRadius.only(
topRight: Radius.circular(30.0),
topLeft: Radius.circular(30.0)),
),
child: Padding(
padding: const EdgeInsets.only(
top: 30.0, left: 20.0, right: 20.0),
child: Row(
children: [
Expanded(
child: Column(
children: const [
Text(
'Study Time',
style: TextStyle(
fontSize: 30.0,
),
),
SizedBox(
height: 10.0,
),
Text(
'25',
style: TextStyle(
fontSize: 80.0,
),
),
],
),
),
Expanded(
child: Column(
children: const [
Text(
'Pause Timer',
style: TextStyle(
fontSize: 30.0,
),
),
Text(
'5',
style: TextStyle(
fontSize: 80.0,
),
),
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Text(
'hello',
),
))
],
),
),
],
),
),
),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100.0),
),
),
child: const Padding(
padding: EdgeInsets.all(20.0),
child: Text(
'start studing',
style: TextStyle(color: Colors.white, fontSize: 22.0),
),
),
),
),
]),
),
),
);
}
}
If you like to use Expanded on Text(hello) widget. You need to get constrains from top level/parent widget, but this is not the solution you like to archive.
I will recommend checking /layout/constraints
As for the answer, you need to wrap Row widget Column to place another child just after the row widget. It could be skipped if you we had same color as background.
import 'package:percent_indicator/percent_indicator.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: Pomodoro(),
));
}
class Pomodoro extends StatefulWidget {
const Pomodoro({Key? key}) : super(key: key);
#override
State<Pomodoro> createState() => _PomodoroState();
}
class _PomodoroState extends State<Pomodoro> {
double percent = 0;
// ignore: non_constant_identifier_names, unused_field
static int TimeInMinut = 25;
// ignore: non_constant_identifier_names
int TimeInSec = TimeInMinut = 60;
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xff1542bf), Color(0xff51a8ff)],
begin: FractionalOffset(0.5, 1)),
),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.only(top: 25.0),
child: Text(
'Pomodoro Clock',
style: TextStyle(color: Colors.white, fontSize: 40.0),
),
),
Expanded(
child: SizedBox(
height: 20.0,
width: 50.0,
child: CircularPercentIndicator(
percent: percent,
animation: true,
animateFromLastPercent: true,
radius: 90.0,
lineWidth: 20.0,
progressColor: Colors.white,
center: Text(
'$TimeInMinut',
style: const TextStyle(
color: Colors.white, fontSize: 30.0),
),
),
),
),
Expanded(
child: Container(
width: double.infinity,
padding: const EdgeInsets.only(
top: 30.0, left: 20.0, right: 20.0),
decoration: const BoxDecoration(
color: Color.fromARGB(255, 163, 48, 48),
borderRadius: BorderRadius.only(
topRight: Radius.circular(30.0),
topLeft: Radius.circular(30.0)),
),
child: Column(
children: [
Row(
children: [
Expanded(
child: Column(
children: const [
Text(
'Study Time',
style: TextStyle(
fontSize: 30.0,
),
),
SizedBox(
height: 10.0,
),
Text(
'25',
style: TextStyle(
fontSize: 80.0,
),
),
],
),
),
Column(
children: const [
Text(
'Pause Timer',
style: TextStyle(
fontSize: 30.0,
),
),
Text(
'5',
style: TextStyle(
fontSize: 80.0,
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Text(
'hello',
),
)
],
),
],
),
Container(
height: 100,
width: 200,
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100.0),
),
),
child: const Padding(
padding: EdgeInsets.all(20.0),
child: Text(
'start studing',
style: TextStyle(
color: Colors.white, fontSize: 22.0),
),
),
),
),
],
),
),
),
]),
),
),
);
}
}

Renderflex overflowed error, also when i wrap column with SingleChildScrollView or ListView my UI disappered

I am creating a Login page, when i click on text field due to overlapping keyboard i got error......................................................................................................
Renderflex overflowed error, also when i wrap column with SingleChildScrollView or ListView my UI disappered. new to flutter lagging in concept , plz help.
import 'package:flutter/material.dart';
import 'package:flutter_chat_application_1/modals/customtheme.dart';
import 'package:flutter_chat_application_1/widgets/customappbar.dart';
import 'package:flutter_chat_application_1/widgets/widget.dart';
class SignIn extends StatefulWidget {
const SignIn({Key? key}) : super(key: key);
#override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 24),
child: Column(
children: [
Spacer(),
Form(
child: Column(
children: [
TextFormField(
style: myTextStyle(),
decoration: textFieldInputDecoration('email')),
TextFormField(
style: myTextStyle(),
decoration: textFieldInputDecoration('password'),
)
],
),
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
padding:
EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: TextButton(
onPressed: () {},
child: Text(
'Forgot Password?',
style: myTextStyle(),
),
))
],
),
SizedBox(
height: 16,
),
GestureDetector(
onTap: () {},
child: Container(
padding: EdgeInsets.symmetric(vertical: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(
colors: [Color(0xff007EF4), Color(0xff2A75BC)])),
width: MediaQuery.of(context).size.width,
child: Text(
'Sign In',
style: biggerTextStyle(),
textAlign: TextAlign.center,
),
),
),
SizedBox(
height: 16,
),
GestureDetector(
onTap: () {},
child: Container(
padding: EdgeInsets.symmetric(vertical: 16),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(30)),
child: Text(
'Sign in with Google',
style:
TextStyle(fontSize: 17, color: CustomTheme.textColor),
textAlign: TextAlign.center,
),
),
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Don\'t have account?',
style: myTextStyle(),
),
TextButton(
onPressed: () {},
child: Text('Register Now',
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.white,
fontSize: 16,
)))
],
),
SizedBox(
height: 10,
)
],
)));
}
}

What's causing "BoxConstraints forces an infinite width" in this Flutter code?

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class SignupPage extends StatelessWidget {
const SignupPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueAccent,
body: ListView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Container(
padding: const EdgeInsets.all(5.0),
margin:
const EdgeInsets.only(top: 160.0, left: 70.0, right: 70.0),
child: const Text(
'Sign up',
style: TextStyle(
fontFamily: 'Ephesis',
fontWeight: FontWeight.bold,
fontSize: 25.0,
color: Colors.white,
),
),
),
),
Center(
child: Container(
padding: const EdgeInsets.all(7.0),
margin: const EdgeInsets.only(left: 18.0, right: 18.0),
child: const Text(
'Wishes, deliver the wishes to your loved ones',
style: TextStyle(
fontSize: 12.0,
color: Colors.white,
fontFamily: 'Ephesis',
fontWeight: FontWeight.bold,
),
),
),
),
const SizedBox(
height: 30.0,
),
Container(
color: Colors.white,
child: const TextField(
decoration: InputDecoration(
filled: true,
labelText: 'Username',
constraints: BoxConstraints(
maxWidth: 300.0,
),
),
),
),
// spacer
const SizedBox(height: 12.0),
// [Password]
Container(
color: Colors.white,
child: const TextField(
decoration: InputDecoration(
filled: true,
labelText: 'Password',
constraints: BoxConstraints(
maxWidth: 300.0,
),
),
obscureText: true,
),
),
const SizedBox(
child: Divider(
height: 70.0,
thickness: 1.0,
indent: 20.0,
endIndent: 20.0,
color: Colors.black,
),
),
Card(
elevation: 7.0,
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Row(
children: const [
ListTile(
minLeadingWidth: 6.0,
leading: Icon(
Icons.people_alt_rounded,
),
title: Text(
'Sign up with apple',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 10.0,
),
),
),
],
),
)
],
),
],
),
);
}
}
There are several problems with the code you posted, in terms of layout hierarchy. The one that's causing the immediate problem is in the child your Card widget. You currently have: Card > Row > ListTile.
You should remove the Row widget, so it becomes Card > ListTile, and it will fix the crash for you.
However, there are other areas of improvement, for example, you have a Column widget being the only child of a ListView etc. I recommend you take a look at the basics of how Flutter Layout works. You can start with this official doc: Understanding constraints.

How to add circular border to dialog in flutter?

How to add circular border for dialog box in a flutter?,I tried the below code but I can't able to get the desired output, I already added circular border but it's not working, I need circular border for dialog,Refer the expected output for details, please guide
My code :
`
class CustomDialog extends StatelessWidget {
#override
Widget build(BuildContext context) {
const double padding = 1.0;
return Dialog(
backgroundColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Container(
margin: EdgeInsets.all(1),
width: double.infinity,
child: Text('title',
style: TextStyle(fontSize: 30, color: Colors.white)),
color: Colors.green,
),
Container(
color: Colors.white,
padding: EdgeInsets.all(10),
child: ListView(
shrinkWrap: true,
children: [
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
],
),
),
Divider(
color: Colors.white,
),
Container(
color: Colors.white,
height: 50,
padding: EdgeInsets.all(5),
alignment: Alignment.centerRight,
child: Text(
'CANCEL',
style: TextStyle(fontSize: 20),
)),
])));
}
}
`
My expectation:
current output:
Just need to add ClipBehavior to Dialog.
import 'package:flutter/material.dart';
class CustomDialog extends StatelessWidget {
#override
Widget build(BuildContext context) {
const double padding = 1.0;
return Dialog(
backgroundColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
clipBehavior: Clip.antiAlias, // add clipBehavior
child: Column(mainAxisSize: MainAxisSize.min, children: [
Container(
margin: EdgeInsets.all(1),
width: double.infinity,
child: Text('title',
style: TextStyle(fontSize: 30, color: Colors.white)),
color: Colors.green,
),
Container(
color: Colors.white,
padding: EdgeInsets.all(10),
child: ListView(
shrinkWrap: true,
children: [
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
Container(
margin: EdgeInsets.only(left: 10, bottom: 10),
height: 30,
child: Text('one',
style: TextStyle(
fontSize: 20,
))),
],
),
),
Divider(
color: Colors.white,
),
Container(
color: Colors.white,
height: 50,
padding: EdgeInsets.all(5),
alignment: Alignment.centerRight,
child: Text(
'CANCEL',
style: TextStyle(fontSize: 20),
)),
]),
);
}
}
The issue was with the Container you used to wrap the other widgets, you can add specific border radius to each container to fix.
I added a demo and code to get what you wanted your output to look like:
class CustomDialog extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Container(
height: 340,
child: Column(
children: [
Container(
height: 60,
width: double.infinity,
padding: EdgeInsets.all(
15.0,
),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(
15.0,
),
topRight: Radius.circular(
15.0,
),
),
),
child: Text(
'Baby Names',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
...List.generate(
5,
(index) => Padding(
padding: const EdgeInsets.all(10.0),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'List Names',
style: TextStyle(
fontSize: 18,
),
),
),
),
),
Divider(
color: Colors.grey[200],
thickness: 1.5,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Align(
alignment: Alignment.centerRight,
child: Text(
'CANCEL',
style: TextStyle(
fontSize: 18,
color: Colors.green,
),
),
),
),
],
),
),
);
}
}
RESULT:
You added RoundedRectangleBorder(),
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: MyWidget(),
),
);
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Dialog(
backgroundColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
child: Container(
padding: EdgeInsets.only(
top: 10.0,
bottom: 5,
left: 5,
right: 5,
),
margin: EdgeInsets.only(top: 5),
decoration: new BoxDecoration(
color: Colors.white,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10.0,
offset: const Offset(0.0, 10.0),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min, // To make the card compact
children: <Widget>[
Text(
"Baby",
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.w700,
),
),
Divider(color: Colors.grey,),
SizedBox(height: 16.0),
Text(
"text",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16.0,
),
),
SizedBox(height: 24.0),
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
onPressed: () {
Navigator.of(context).pop(); // To close the dialog
},
child: Text("buttonText"),
),
),
],
),
),
);
}
}

trigger flutter IconButton ripple animation programmtically?

Is there a way to programmatically trigger the IconButton ripple animation? The following is some code:
#override
Widget build(BuildContext context) {
return Material(
child: Container(
margin: EdgeInsets.only(top: 12, left: 18, right: 18),
padding: EdgeInsets.only(top: 8, bottom: 8),
decoration: BoxDecoration(
color: _bgColor,
border: new Border.all(color: _bgColor),
borderRadius: BorderRadius.all(Radius.circular(
10.0) // <--- border radius here
),
),
child: Row(children: [
IconButton(
padding: EdgeInsets.only(right: 16),
icon: Icon(Icons.play_arrow, color: Colors.white, size: 48),
tooltip: 'Start ${issue.issueName}',
onPressed: () {},
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
issue.title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
softWrap: true,
),
),
Text(
issue.issueName,
style: TextStyle(
color: Colors.white,
),
),
],
),
),
])));
}