The Width of the Elevated Button is not reducing, it is coming in taking all the width on the screen.The parent widget is ListView.I have even tried to reduce it through ButtomTheme but still it is not working. I have added the code below. Everywhere I have seen the way to reduce the width is this way.But don't know why it is not reducing the width
ListView(
children: [
Row(
children: [
RotatedBox(
quarterTurns: 3,
child: Padding(
padding: const EdgeInsets.only(right: 30.0),
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontFamily: 'Pacifico'),
),
),
),
SizedBox(
width: 20,
),
Text(
'BRUXTER',
style: TextStyle(
fontSize: 30,
fontFamily: 'RockSalt',
color: Colors.black,
fontWeight: FontWeight.bold),
)
],
),
SizedBox(
height: 50,
),
Form(
child: Column(
children: [
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Name',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Email',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your PassWord',
hintStyle: TextStyle(color: Colors.white60),
),
),
],
),
),
SizedBox(
height: 20,
),
Container(
width: MediaQuery.of(context).size.width *
0.5, // Will take 50% of screen space
child: ElevatedButton(
child: Text('Go to screen two'),
onPressed: () {},
),
)
],
You need to replace your ListView with Column
Column(
children: [
Row(
children: [
RotatedBox(
quarterTurns: 3,
child: Padding(
padding: const EdgeInsets.only(right: 30.0),
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontFamily: 'Pacifico'),
),
),
),
SizedBox(
width: 20,
),
Text(
'BRUXTER',
style: TextStyle(
fontSize: 30,
fontFamily: 'RockSalt',
color: Colors.black,
fontWeight: FontWeight.bold),
)
],
),
SizedBox(
height: 50,
),
Form(
child: Column(
children: [
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Name',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Email',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your PassWord',
hintStyle: TextStyle(color: Colors.white60),
),
),
],
),
),
SizedBox(
height: 20,
),
Container(
width: MediaQuery.of(context).size.width *
0.5, // Will take 50% of screen space
child: ElevatedButton(
child: Text('Go to screen two'),
onPressed: () {},
),
)
],
)
Please refer to below code
Replace width with margin in Container widget
Container(
// Replace width with margin
margin: EdgeInsets.symmetric(horizontal: 80.0,),
// width: MediaQuery.of(context).size.width *
// 0.5, // Will take 50% of screen space
child: ElevatedButton(
child: Text('Go to screen two'),
onPressed: () {},
),
)
Complete code
import 'package:flutter/material.dart';
const 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 ListView(children: [
Row(
children: [
RotatedBox(
quarterTurns: 3,
child: Padding(
padding: const EdgeInsets.only(right: 30.0),
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white, fontSize: 50, fontFamily: 'Pacifico'),
),
),
),
SizedBox(
width: 20,
),
Text(
'BRUXTER',
style: TextStyle(
fontSize: 30,
fontFamily: 'RockSalt',
color: Colors.black,
fontWeight: FontWeight.bold),
)
],
),
SizedBox(
height: 50,
),
Form(
child: Column(
children: [
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Name',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your Email',
hintStyle: TextStyle(color: Colors.white60),
),
),
TextFormField(
style: TextStyle(color: Colors.black, fontSize: 30),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter Your PassWord',
hintStyle: TextStyle(color: Colors.white60),
),
),
],
),
),
SizedBox(
height: 20,
),
Container(
// Replace width with margin
margin: EdgeInsets.symmetric(
horizontal: 80.0,
),
// width: MediaQuery.of(context).size.width *
// 0.5, // Will take 50% of screen space
child: ElevatedButton(
child: Text('Go to screen two'),
onPressed: () {},
),
)
]);
}
}
Instead of using Column, I would actually recommend staying with ListView for performance reasons in most cases.
You could also fix this issue by wrapping the Container in a Center widget (or an Align if you want a different alignment than centered).
These widgets provide loose constraints to their children so they can be any size they want, but no larger than the parent, which is exactly what you want in this case.
It wasn't working in your case, because the ListView forces children to take up the entire width, so what size the direct children want to take up is ignored.
Related
I'm trying to use Align(alignment: Alignment.bottomCenter, but it's not working. From the class I took to do this, all he had was a sized box to fit it at the bottom of the screen. But that isn't working for different screen sizes..
Here is everything inside SingleChildScrollView. RichText is near the bottom. (Changed from register screen to my login screen because it has less code).
Widget build(BuildContext context) => SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 45, horizontal: 50),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 50),
Image(
image: AssetImage('assets/image.jpg'),
),
SizedBox(height: 50),
TextFormField(
style: TextStyle(color: Colors.white),
controller: emailController,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
hintText: "Enter Email",
hintStyle: TextStyle(color: Color(0xFFD6D6D6)),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
keyboardType: TextInputType.emailAddress,
),
SizedBox(height: 4),
TextFormField(
style: TextStyle(color: Colors.white),
controller: passwordController,
obscureText: isHiddenPassword,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
hintText: "Enter Password",
hintStyle: TextStyle(color: Color(0xFFD6D6D6)),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
suffixIcon: InkWell(
onTap: _togglePasswordView,
child: Icon(
isHiddenPassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
color: Colors.white),
),
),
),
SizedBox(height: 20),
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size.fromHeight(40),
primary: Colors.white,
),
child: Text(
'Sign In',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF162242)),
),
onPressed: signIn,
),
SizedBox(height: 20),
SignInButton(
Buttons.Facebook, onPressed: () {},
text: "Sign in with Facebook",
),
SizedBox(height: 16),
SignInButton(
Buttons.Google,
text: "Sign in with Google",
onPressed: () {
signInWithGoogle();
},
),
SizedBox(height: 20),
GestureDetector(
child: Text(
'Forgot Password?',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
decoration: TextDecoration.underline,
color: Colors.white,
),
),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ForgotPasswordPage(),
)),
),
// SizedBox(
// height: 100),
Align(
alignment: Alignment.bottomCenter,
child: RichText(
text: TextSpan(
style: TextStyle(color: Color(0xFFD6D6D6), fontSize: 20),
text: 'No account? ',
children: [
TextSpan(
recognizer: TapGestureRecognizer()
..onTap = widget.onClickedSignUp,
text: 'Sign Up',
style: TextStyle(
decoration: TextDecoration.underline,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
),
],
),
);
Thank you for your time!
You can use Spacer to push the child till the end of the Column like so:
Column(
children: [
Container(
height: 200,
color: Colors.black,
),
Container(
height: 200,
color: Colors.green,
),
Spacer(),
RichText(
textAlign: TextAlign.center,
text: TextSpan(
text: 'Already have an account?',
style: TextStyle(color: Colors.black),
children: [
TextSpan(
text: 'Sign In',
)
],
),
),
],
)
This is a design I want:
This is my current design:
I am new to using flutter. My question is how to set the container width so that the textfield box looks more fit and neat. I tried to set the container width now but still failed to make the textfield box look fit. What should I do? Please help from stack overflow.
This is my code:
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
//SizedBox(height: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 1,
child: Center(
child: FutureBuilder(
future: _getSignedURL(
widget.patientProfile.avatar),
builder: (BuildContext context,
AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
color: Colors.white,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Color.fromRGBO(
255, 255, 255, 0.3),
border: Border.all(
color: Colors.black12,
width: 1.0,
),
borderRadius: BorderRadius.all(
Radius.circular(200.0)),
),
),
);
} else {
return CircleAvatar(
radius: 100,
backgroundImage:
NetworkImage(snapshot.data),
);
}
},
),
),
),
],
),
),
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'First Name',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style:
const TextStyle(color: Colors.black),
controller: firstName,
onSaved: (String? value) {
firstName.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16),
),
),
),
],
)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Last Name',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller: lastName,
onSaved: (String? value) {
lastName.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Last Name',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Date Of Birth',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller: dateOfBirth,
onSaved: (String? value) {
dateOfBirth.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Date Of Birth',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
)
],
)),
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Gender',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style:
const TextStyle(color: Colors.black),
controller: gender,
onSaved: (String? value) {
gender.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Gender',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16),
),
),
),
],
)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Marital Status',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller: maritalStatus,
onSaved: (String? value) {
maritalStatus.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Marital Status',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'ID or Passport',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller:govermentIssuedID,
onSaved: (String? value) {
govermentIssuedID.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'ID or Passport',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
)
],
)),
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Phone Number',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style:
const TextStyle(color: Colors.black),
controller: contactNumber,
onSaved: (String? value) {
contactNumber.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Phone Number',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16),
),
),
),
],
)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Email',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller: email,
onSaved: (String? value) {
email.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Email',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'District',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 400,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller:address,
onSaved: (String? value) {
address.text = value!;
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'District',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
)
],
)),
You can control the position of elements in a row by using the alignment property.. Try
Container(
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisAlignemt : MainAxisAlignment.spaceBetween, // or MainAxisAlignment.spaceAround
children: []
)
)
There is a padding property in Container widget you can use that property for this design. Add padding to the top container like this:
Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: ...
)
First of all I hope your all well in this globally very hard hitting period. I'm refactoring parts of my screens codes and I'm stuck with this.
I have a bottom modal sheet I extracted into a separate file to keep my MapScreen UI code short and clear but something goes wrong. The error I get is BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc. Does that men that I have to declare a BlocProvider also in the separate file? Doesn't it get passed to the widget with the context:context parameter? I then tried adding it but still get the error. Can you spot what I'm doing wrong?
As always thank you very much for your time and help, especially in this very hard time.
UI modal bottom sheet:
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (modal) {
// return AndroidTrackingSheet(routeName,
// isTracking, _textEditingController);
return Container(
color: Color(0xff757575),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
left: 20,
right: 20,
bottom: MediaQuery.of(modal)
.viewInsets
.bottom),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(20)),
),
child: Center(
child: Column(
// mainAxisAlignment:
// MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(
height: 10,
),
Text(
'Nuovo percorso',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
color: Colors.orangeAccent,
fontWeight: FontWeight.w400,
),
),
SizedBox(
height: 10,
),
Text(
'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.w400),
),
SizedBox(
height: 10,
),
TextField(
controller:
_textEditingController,
autofocus: true,
textAlign: TextAlign.center,
showCursor: true,
decoration: InputDecoration(
hintText: isTracking
? routeName
: 'nome percorso',
labelStyle: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight:
FontWeight.w100),
border: OutlineInputBorder(),
// focusColor:
// Colors.lightGreenAccent,
focusedBorder:
OutlineInputBorder(
borderSide: BorderSide(
color: Colors.orange,
width: 1,
),
),
),
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.orangeAccent,
child: Text(
isTracking
? "Fine tracking"
: 'Inizia tracking',
style: TextStyle(
fontSize: 18,
color: Colors.white),
),
onPressed: () {
print(
"Action 2 is been clicked");
routeName =
_textEditingController.text;
Navigator.pop(context);
isTracking = !isTracking;
BlocProvider.of<TrackingBloc>(
context)
.add(StartStopTracking());
},
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.redAccent,
child: Text(
'Cancella',
style: TextStyle(
fontSize: 18,
color: Colors.white),
),
onPressed: () {
Navigator.pop(context);
},
),
SizedBox(
height: 10,
),
],
),
),
),
),
);
});
Separate widget modal sheet:
class AndroidTrackingSheet extends StatelessWidget {
TextEditingController _textEditingController;
bool isTracking;
String routeName;
AndroidTrackingSheet(
this.routeName, this.isTracking, this._textEditingController);
#override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
left: 20,
right: 20,
bottom: MediaQuery.of(context).viewInsets.bottom),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: Center(
child: Column(
// mainAxisAlignment:
// MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(
height: 10,
),
Text(
'Nuovo percorso',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
color: Colors.orangeAccent,
fontWeight: FontWeight.w400,
),
),
SizedBox(
height: 10,
),
Text(
'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.w400),
),
SizedBox(
height: 10,
),
TextField(
controller: _textEditingController,
autofocus: true,
textAlign: TextAlign.center,
showCursor: true,
decoration: InputDecoration(
hintText: isTracking ? routeName : 'nome percorso',
labelStyle: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.w100),
border: OutlineInputBorder(),
// focusColor:
// Colors.lightGreenAccent,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.orange,
width: 1,
),
),
),
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.orangeAccent,
child: Text(
isTracking ? "Fine tracking" : 'Inizia tracking',
style: TextStyle(fontSize: 18, color: Colors.white),
),
onPressed: () {
print("Action 2 is been clicked");
routeName = _textEditingController.text;
Navigator.pop(context);
isTracking = !isTracking;
BlocProvider.of<TrackingBloc>(context)
.add(StartStopTracking());
},
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.orangeAccent,
child: Text(
'Cancella',
style: TextStyle(fontSize: 18, color: Colors.white),
),
onPressed: () {
Navigator.pop(context);
},
),
SizedBox(
height: 10,
),
],
),
),
),
),
);
}
}
Separate bottom sheet with bloc provider:
class AndroidTrackingBottomSheet extends StatelessWidget {
TextEditingController _textEditingController;
bool isTracking;
String routeName;
AndroidTrackingBottomSheet(
this.routeName, this.isTracking, this._textEditingController);
#override
Widget build(BuildContext context) {
return BlocProvider<TrackingBloc>(
create: (context) => TrackingBloc(),
child: Container(
color: Color(0xff757575),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
left: 20,
right: 20,
bottom: MediaQuery.of(context).viewInsets.bottom),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: Center(
child: Column(
// mainAxisAlignment:
// MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(
height: 10,
),
Text(
'Nuovo percorso',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
color: Colors.orangeAccent,
fontWeight: FontWeight.w400,
),
),
SizedBox(
height: 10,
),
Text(
'Inserisci un nome per il tuo nuovo percorso, e scegli Inizia tracking. Quando sarai arrivato a destinazione premi di nuovo il bottone Tracking e scegli Fine tracking.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.w400),
),
SizedBox(
height: 10,
),
TextField(
controller: _textEditingController,
autofocus: true,
textAlign: TextAlign.center,
showCursor: true,
decoration: InputDecoration(
hintText: isTracking ? routeName : 'nome percorso',
labelStyle: TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.w100),
border: OutlineInputBorder(),
// focusColor:
// Colors.lightGreenAccent,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.orange,
width: 1,
),
),
),
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.orangeAccent,
child: Text(
isTracking ? "Fine tracking" : 'Inizia tracking',
style: TextStyle(fontSize: 18, color: Colors.white),
),
onPressed: () {
print("Action 2 is been clicked");
routeName = _textEditingController.text;
Navigator.pop(context);
isTracking = !isTracking;
BlocProvider.of<TrackingBloc>(context)
.add(StartStopTracking());
},
),
SizedBox(
height: 10,
),
FlatButton(
color: Colors.orangeAccent,
child: Text(
'Cancella',
style: TextStyle(fontSize: 18, color: Colors.white),
),
onPressed: () {
Navigator.pop(context);
},
),
SizedBox(
height: 10,
),
],
),
),
),
),
),
);
}
}
I finally found out that the Bloc has to be provided to the bottom sheet via BlocProvider.value, not in the widget file ,so the working code is :
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (modal) {
return BlocProvider.value(
value: BlocProvider.of<TrackingBloc>(context),
child: AndroidTrackingBottomSheet(
widget.key,
routeName,
isTracking,
_textEditingController),
);
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: Container(
child: Column(
children: <Widget>[
Stack(
fit: StackFit.loose,
children: <Widget>[
HeadersVC(),
Container(
margin: EdgeInsets.only(top: 40.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FlatButton(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onPressed: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back,
size: 30,
color: Colors.white,
),
),
Text(
widget.headertitleladderPre,
style: TextStyle(
fontSize: 22.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
],
),
)
],
),
SingleChildScrollView(
padding: EdgeInsets.only(top: 20.0, left: 30.0, right: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 10.0),
),
//******************************************** Date Picker */
TextFormField(
onTap: () async {
FocusScope.of(context).requestFocus(new FocusNode());
_selectDate(context);
},
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.black26, width: 1.5)),
contentPadding: EdgeInsets.only(left: 0.0, right: 0.0),
labelText: formatDate(formatedDate),
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black),
suffixIcon: new Padding(
padding: const EdgeInsets.only(
top: 0.0, left: 0.0, right: 0.0, bottom: 0.0),
child: SizedBox(
height: 15,
child: Icon(
Icons.calendar_today,
color: Colors.red,
),
),
)),
),
Padding(
padding: EdgeInsets.only(left: 0.0, top: 10.0),
),
//******************************************** DropDown Button */
Container(
child: DropdownButton(
underline: Container(
height: 1.0,
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black26, width: 1.5))),
),
value: _selectedladleno,
items: _dropDownladleItems,
onChanged: changedDropDownItem,
isExpanded: true,
isDense: false,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.red,
),
iconSize: 30,
elevation: 12,
style: TextStyle(
fontSize: 15,
),
),
),
//******************************************** Radio Button */
ButtonBar(
alignment: MainAxisAlignment.start,
buttonPadding: EdgeInsets.only(left: 0.0),
children: <Widget>[
Radio(
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
print(_character);
});
},
value: SingingCharacter.New,
activeColor: Colors.red,
),
Text(
"New",
style: TextStyle(fontSize: 13.0),
),
Radio(
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
print(_character);
});
},
value: SingingCharacter.Repair,
activeColor: Colors.red,
),
Text(
"Repair",
style: TextStyle(fontSize: 13.0),
),
Radio(
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
print(_character.toString());
});
},
value: SingingCharacter.OnlyMachineChange,
activeColor: Colors.red,
),
Text(
"Machine Change",
style: TextStyle(fontSize: 13.0),
),
],
),
//******************************************** Machine Number */
TextFormField(
decoration: InputDecoration(
labelText: "Machine Number *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
)
],
),
)
],
),
),
);
}
I got Error Something Like this :
Performing hot reload... ⣟flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during layout:
flutter: A RenderFlex overflowed by 158 pixels on the bottom.
flutter:
flutter: The relevant error-causing widget was:
flutter: Column file:///Users/baps/Projects/Flutter%20Projects/TRLApp/trlapp/lib/ladlePreheating.dart:64:16
flutter:
flutter: The overflowing RenderFlex has an orientation of Axis.vertical.
flutter: The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
flutter: black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
flutter: Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
flutter: RenderFlex to fit within the available space instead of being sized to their natural size.
flutter: This is considered an error condition because it indicates that there is content that cannot be
flutter: seen. If the content is legitimately bigger than the available space, consider clipping it with a
flutter: ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
flutter: like a ListView.
flutter: The specific RenderFlex in question is: RenderFlex#4bdfd relayoutBoundary=up1 OVERFLOWING:
flutter: needs compositing
flutter: creator: Column ← Container ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ←
flutter: CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ←
flutter: _InkFeatures-[GlobalKey#9c17c ink renderer] ← NotificationListener ←
flutter: PhysicalModel ← ⋯
flutter: parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body (can use size)
flutter: constraints: BoxConstraints(0.0<=w<=414.0, 0.0<=h<=736.0)
flutter: size: Size(414.0, 736.0)
flutter: direction: vertical
flutter: mainAxisAlignment: start
flutter: mainAxisSize: max
flutter: crossAxisAlignment: center
flutter: verticalDirection: down
flutter: ◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
Missed to Expand the ScrollView as Column container would not know how much space it needs to take and due to that, it calculates the size which is more than of screen size.
So Just place Expanded in to it:
...
Expanded(
child: SingleChildScrollView(
padding:...
SingleChildScrollView should be Wrapped around Column:
Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: Container(
child: SingleChildScrollView( // <--- Put it here.
child: Column( // <--- Your Column.
Solution: You need to use SingleChildScrollView before column .If you want to keep separated stack part ,you can use CustomScrollView . CustomScrollView
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: SingleChildScrollView(
padding: EdgeInsets.only(top: 20.0, left: 30.0, right: 30.0),
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Stack(
fit: StackFit.loose,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 40.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FlatButton(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onPressed: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back,
size: 30,
color: Colors.white,
),
),
Text(
"headertitleladderPre",
style: TextStyle(
fontSize: 22.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
],
),
)
],
),
Padding(
padding: EdgeInsets.only(top: 10.0),
),
//******************************************** Date Picker */
TextFormField(
onTap: () async {},
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.black26, width: 1.5)),
contentPadding: EdgeInsets.only(left: 0.0, right: 0.0),
labelText: "formatedDate",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black),
suffixIcon: new Padding(
padding: const EdgeInsets.only(
top: 0.0, left: 0.0, right: 0.0, bottom: 0.0),
child: SizedBox(
height: 15,
child: Icon(
Icons.calendar_today,
color: Colors.red,
),
),
)),
),
Padding(
padding: EdgeInsets.only(left: 0.0, top: 10.0),
),
//******************************************** DropDown Button */
Container(
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
//******************************************** Radio Button */
ButtonBar(
alignment: MainAxisAlignment.start,
buttonPadding: EdgeInsets.only(left: 0.0),
children: <Widget>[
Text("Radio Here"),
Text(
"New",
style: TextStyle(fontSize: 13.0),
),
Text("One More Radio Here"),
Text(
"Repair",
style: TextStyle(fontSize: 13.0),
),
Text("One More Radio Here"),
Text(
"Machine Change",
style: TextStyle(fontSize: 13.0),
),
],
),
//******************************************** Machine Number */
TextFormField(
decoration: InputDecoration(
labelText: "Machine Number *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
),
TextFormField(
decoration: InputDecoration(
labelText: "Well Block Party *",
labelStyle: TextStyle(
fontFamily: "Montserrat", color: Colors.black)),
)
],
),
),
));
}
Avoid Using
child: ConstrainedBox(
constraints: BoxConstraints()),
In side SingleChildScrollView when i remove this widget rendering error is gone.
Sample Code of my layout.
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: kBackgroundColorGray,
appBar: commonAppBar,
body: SafeArea(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
],
),
),
)
);
}
I am trying to figure out the responsive aspect of flutter. So as you can see from the image things are not going all that well for the different screen sizes of iPhone.
I am working with a Stack and FractionallySizedBox but not sure if what I am doing is correct. Any help is appreciate.
Code is available here -> https://gist.github.com/GY22/1eefb5e48fdca9d785365cbccbdcb478
import 'package:flutter/material.dart';
class SignIn extends StatelessWidget {
//textfields + logo
List<Widget> _buildChildrenForm() {
return [
//logo
Text(
'THE GUEST LIST',
style: TextStyle(
color: Colors.white,
fontFamily: 'futura',
fontSize: 60.0
)
),
//email
TextField(
cursorColor: Colors.white,
cursorWidth: 3.0,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white
)
),
labelText: 'EMAIL',
labelStyle: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
//hintText: 'DEMO#MAIL.COM',
hintStyle: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
style: TextStyle(
color: Colors.white,
fontSize: 20.0
),
),
SizedBox(height: 20.0,),
//password
TextField(
cursorColor: Colors.white,
cursorWidth: 3.0,
obscureText: true,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white
)
),
labelText: 'PASSWORD',
labelStyle: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
hintStyle: TextStyle(
color: Colors.white
),
),
style: TextStyle(
color: Colors.white,
),
),
SizedBox(height: 65.0,),
//submit button
FlatButton(
child: Text(
'SIGN IN',
style: TextStyle(
fontSize: 35.0,
color: Colors.white
),
),
onPressed: () {},
),
SizedBox(height: 10.0),
//forget password
FlatButton(
child: Text(
'FORGOT PASSWORD',
style: TextStyle(
color: Colors.white,
fontSize: 17.0
),
),
onPressed: () {},
)
];
}
Widget _formSignIn(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
children: _buildChildrenForm(),
),
);
}
#override
Widget build(BuildContext context) {
final double screenHeight = MediaQuery.of(context).size.height;
final double halfScreen = screenHeight / 2;
final double finalHeight = halfScreen / screenHeight;
debugPrint(MediaQuery.of(context).size.height.toString());
//debugPrint(MediaQuery.of(context).size.width.toString());
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
//bg image
FractionallySizedBox(
alignment: Alignment.topLeft,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/backgroundWithOpacity.png'),
fit: BoxFit.cover
)
),
),
),
//form
FractionallySizedBox(
alignment: Alignment.center,
heightFactor: 1 - finalHeight,
child: ListView(
children: <Widget>[
_formSignIn(context)
],
),
)
],
),
);
}
}
Use FittedBox for compressing the title in device width.
Use Align for centering, we need ListView only for keyboard appearences, normally the content you wanna show to the user is even small enough for iPhone 5s.
You have extra line of codes and I removed.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
body: SignIn(),
)));
}
class SignIn extends StatelessWidget {
List<Widget> _buildChildrenForm() => [
//logo
SizedBox(
width: double.infinity,
child: FittedBox(
fit: BoxFit.fitWidth,
child: Text('THE GUEST LIST',
style: TextStyle(
color: Colors.white, fontFamily: 'futura', fontSize: 60.0)),
),
),
//email
TextField(
cursorColor: Colors.white,
cursorWidth: 3.0,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
labelText: 'EMAIL',
labelStyle: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
//hintText: 'DEMO#MAIL.COM',
hintStyle: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
//password
SizedBox(height: 20),
TextField(
cursorColor: Colors.white,
cursorWidth: 3.0,
obscureText: true,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
labelText: 'PASSWORD',
labelStyle: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
hintStyle: TextStyle(color: Colors.white),
),
style: TextStyle(
color: Colors.white,
),
),
//submit button
SizedBox(height: 65),
FlatButton(
child: Text(
'SIGN IN',
style: TextStyle(fontSize: 35.0, color: Colors.white),
),
onPressed: () {},
),
SizedBox(height: 10),
//forget password
FlatButton(
child: Text(
'FORGOT PASSWORD',
style: TextStyle(color: Colors.white, fontSize: 17.0),
),
onPressed: () {},
)
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
//bg image
FractionallySizedBox(
alignment: Alignment.topLeft,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/backgroundWithOpacity.png'),
fit: BoxFit.cover)),
),
),
//form
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: ListView(
shrinkWrap: true,
children: _buildChildrenForm(),
),
),
)
],
),
);
}
}
Avoid SizeConfig (custom class) and hard coded dimensions as much as you can.
Example: MediaQuery.of(context).size.width - someValue
Best easiest way to to make responsive UI for different screen size is Sizer plugin.
Check it this plugin ⬇️
https://pub.dev/packages/sizer
You can use SingleChildScrollView or ListView, and using relative proportions