Expanded textfield strach a lot - flutter

and at the signup screen I got some problems when I run my app on the device. This happens with the screen : photo with textfields. What I should change to my code to everything to be in place? The app is made with flutter , should I add a height and width to containers? Or should I erase the Expanded and try something else
class SignupPage extends StatefulWidget {
#override
_SignupPageState createState() => _SignupPageState();
}
class _SignupPageState extends State<SignupPage> {
final _formKey = GlobalKey<FormState>();
String email = '';
String password = '';
String error = '';
String _selectedRole = 'Select a User Role';
String get selectedRole => _selectedRole;
int selectedValue;
String selectedString;
// void setSelectedRole(String role) {
// _selectedRole = role;
// }
bool loading = false;
final Authentication authentication = Authentication();
int selectedIndex1 = 0;
final elements1 = [
"Customer",
"Business",
];
List<Widget> _buildItems1() {
return elements1
.map((val) => MySelectionItem(
title: val,
))
.toList();
}
#override
Widget build(BuildContext context) {
return Scaffold(
// resizeToAvoidBottomInset: false,
// resizeToAvoidBottomPadding: false,
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 30.0),
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios, color: Colors.blue),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen()));
},
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: RichText(
text: TextSpan(
text: 'Tariffo',
style: TextStyle(
color: Colors.blue,
fontFamily: 'SignPainter',
fontSize: 60),
),
),
),
Expanded(
child: Container(
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 30.0),
child: TextFormField(
validator: (val) => val.isEmpty ? 'enter name' : null,
onChanged: (val) {
setState(() => name = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'Enter Name',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 40.0),
child: TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter Email' : null,
onChanged: (val) {
setState(() => email = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'Enter email',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 10.0),
child: TextFormField(
validator: (val) => val.length < 8
? 'enter password > 8 digits'
: null,
onChanged: (val) {
setState(() => password = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'Enter Password',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
obscureText: true,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical:10.0),
child: DirectSelect(
itemExtent: 50.0,
selectedIndex: selectedIndex1,
backgroundColor: Colors.white30,
child: MySelectionItem(
isForList: false,
title: elements1[selectedIndex1],
),
onSelectedItemChanged: (index) {
setState(() {
selectedIndex1 = index;
});
},
items: _buildItems1()),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical:40.0),
child: MaterialButton(
color: Colors.blue,
height: 30,
minWidth: 300,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await authentication
.createUserWithEmailAndPassword(email, password,
name, elements1[selectedIndex1]);
if (elements1[selectedIndex1] == 'Business') {
Navigator.push(
context,
new MaterialPageRoute(
builder: (_) => BusinessPage()));
} else {
Navigator.push(
context,
new MaterialPageRoute(
builder: (_) => Homepage()));
}
if (result == null) {
setState(() => error =
'Sorry,These credentials will not work out');
loading = false;
}
}
},
child: Text(
'Sign up',
style: TextStyle(
fontFamily: 'Antra', color: Colors.white),
),
),
),
),
],
),
),
),
),
_createAccountLabel(),
],
),
),
);
}
Widget _createAccountLabel() {
return InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => LoginPage()));
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 20),
padding: EdgeInsets.all(15),
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Do you have an account ?',
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
SizedBox(
width: 10,
),
Text(
'Login',
style: TextStyle(
color: Colors.blue,
fontSize: 13,
fontWeight: FontWeight.w600),
),
],
),
),
);
}
}

Related

Pixel Overflow Problem While Opening the Keyboard

For the following code, I am getting a pixel overflow whenever I am trying to open the keyboard to search for something in the search bar.
I cannot remove the container having the specified height of 500 because doing so produces other errors.
import 'package:chatapp/services/database.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class Search extends StatefulWidget {
_Search createState() => _Search();
}
class _Search extends State<Search> {
QuerySnapshot<Map<String, dynamic>>? searchResultSnapshot;
bool isLoading = false;
bool haveUserSearched = false;
DatabaseMethods databaseMethods = new DatabaseMethods();
var searchEditingController = TextEditingController();
InitiateSearch() async {
if (searchEditingController.text.isNotEmpty) {
setState(() {
isLoading = true;
});
await databaseMethods.GetUserByUsername(searchEditingController.text)
.then((snapshot) {
searchResultSnapshot = snapshot;
print("$searchResultSnapshot");
setState(() {
isLoading = false;
haveUserSearched = true;
});
});
}
}
Widget UserList() {
return Container(
height: 500,
child: isLoading
? Container(
child: Center(
child: CircularProgressIndicator(),
),
)
: ListView.builder(
itemCount: searchResultSnapshot?.docs.length ?? 0,
itemBuilder: (context, index) {
return ListTile(
searchResultSnapshot?.docs[index].data()["userName"],
searchResultSnapshot?.docs[index].data()["userEmail"],
);
}),
);
}
Widget ListTile(String? userName, String? userEmail) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
userName ?? "",
style: TextStyle(color: Colors.white, fontSize: 16),
),
Text(
userEmail ?? "",
style: TextStyle(color: Colors.white, fontSize: 16),
)
],
),
Spacer(),
GestureDetector(
onTap: () {
null;
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(24)),
child: Text(
"Message",
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
)
],
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
titleSpacing: 0,
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context);
});
},
),
title: Text('Search'),
),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
Container(
height: 36,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
boxShadow: [
BoxShadow(
color: Colors.black,
),
BoxShadow(
color: Colors.black,
spreadRadius: -12.0,
blurRadius: 12.0,
),
],
),
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 8.0),
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 0, horizontal: 10.0),
child: TextFormField(
style: TextStyle(color: Colors.black),
controller: searchEditingController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Search',
hintStyle: TextStyle(color: Colors.grey),
icon: GestureDetector(
onTap: () {
InitiateSearch();
},
child: GestureDetector(
onTap: () {
InitiateSearch();
print('tap');
},
child: Icon(
Icons.search,
color: Colors.grey,
),
),
),
suffixIcon: IconButton(
onPressed: searchEditingController.clear,
icon: Icon(
Icons.cancel_rounded,
color: Colors.grey,
),
),
),
),
),
), //Search
SizedBox(
height: 12,
),
UserList(),
],
),
));
}
}
Could anyone solve this? Also are there anyways to make the container height dynamic. Your help will be highly appreciated.
Okay I found the solution myself. Under UserList() I replaced:
return Container(...);
with:
return Expanded(...);

Flutter error: The method 'validate' was called on null. Receiver: null Tried calling: validate()

I am currently working on a flutter project. I made a flat button in the bottom and made the function _submit() as the onPressed method. But it seems like _formKey.currentState.validate() is having an error. The error message is like what's shown below
The method 'validate' was called on null.
Receiver: null
Tried calling: validate()
I found similar questions to mine and tried to fix it. But I can't find my mistake.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class AddTaskScreen extends StatefulWidget {
AddTaskScreen({Key key}) : super(key: key);
#override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String _priority;
DateTime _date = DateTime.now();
TextEditingController _dateController = TextEditingController();
final DateFormat _dateFormatter = DateFormat('MMM dd, yyy');
final List<String> _priorities = ['Low', 'Medium', 'High'];
_handleDatePicker() async {
final DateTime date = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime(2000),
lastDate: DateTime(2100));
if (date != null && date != _date) {
setState(() {
_date = date;
});
_dateController.text = _dateFormatter.format(date);
}
}
_submit() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print('$_title $_date $_priority');
Navigator.pop(context);
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 40.0, vertical: 80.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: () => Navigator.pop(context),
child: Icon(
Icons.arrow_back_ios,
size: 30.0,
color: Colors.black,
),
),
SizedBox(
height: 20.0,
),
Text('Add Task',
style: TextStyle(
color: Colors.black,
fontSize: 40.0,
fontWeight: FontWeight.bold)),
SizedBox(height: 10.0),
Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Title',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => input.trim().isEmpty
? 'Please enter a task title'
: null,
onSaved: (input) => _title = input,
initialValue: _title),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
readOnly: true,
controller: _dateController,
style: TextStyle(fontSize: 18.0),
onTap: _handleDatePicker,
decoration: InputDecoration(
labelText: 'Date',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: DropdownButtonFormField(
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 22.0,
items: _priorities.map((String priority) {
return DropdownMenuItem(
value: priority,
child: Text(
priority,
style: TextStyle(
color: Colors.black, fontSize: 18.0),
),
);
}).toList(),
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Priority',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => _priority == null
? 'Please select a priority level'
: null,
onChanged: (value) {
setState(() {
_priority = value;
});
},
value: _priority,
)),
Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(30.0)),
child: FlatButton(
onPressed: _submit(),
child: Text(
'Add',
style: TextStyle(
color: Colors.white, fontSize: 20.0),
)),
)
],
),
)
],
),
),
),
),
);
}
}
You can copy paste run full code below
You can change _submit() to _submit
Because _submit() means execute function
code snippet change from
FlatButton(
onPressed: _submit(),
to
FlatButton(
onPressed: _submit,
working demo
full code
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class AddTaskScreen extends StatefulWidget {
AddTaskScreen({Key key}) : super(key: key);
#override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String _priority;
DateTime _date = DateTime.now();
TextEditingController _dateController = TextEditingController();
final DateFormat _dateFormatter = DateFormat('MMM dd, yyy');
final List<String> _priorities = ['Low', 'Medium', 'High'];
_handleDatePicker() async {
final DateTime date = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime(2000),
lastDate: DateTime(2100));
if (date != null && date != _date) {
setState(() {
_date = date;
});
_dateController.text = _dateFormatter.format(date);
}
}
_submit() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print('$_title $_date $_priority');
Navigator.pop(context);
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 40.0, vertical: 80.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: () => Navigator.pop(context),
child: Icon(
Icons.arrow_back_ios,
size: 30.0,
color: Colors.black,
),
),
SizedBox(
height: 20.0,
),
Text('Add Task',
style: TextStyle(
color: Colors.black,
fontSize: 40.0,
fontWeight: FontWeight.bold)),
SizedBox(height: 10.0),
Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Title',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => input.trim().isEmpty
? 'Please enter a task title'
: null,
onSaved: (input) => _title = input,
initialValue: _title),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
readOnly: true,
controller: _dateController,
style: TextStyle(fontSize: 18.0),
onTap: _handleDatePicker,
decoration: InputDecoration(
labelText: 'Date',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: DropdownButtonFormField(
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 22.0,
items: _priorities.map((String priority) {
return DropdownMenuItem(
value: priority,
child: Text(
priority,
style: TextStyle(
color: Colors.black, fontSize: 18.0),
),
);
}).toList(),
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Priority',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => _priority == null
? 'Please select a priority level'
: null,
onChanged: (value) {
setState(() {
_priority = value;
});
},
value: _priority,
)),
Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(30.0)),
child: FlatButton(
onPressed: _submit,
child: Text(
'Add',
style: TextStyle(
color: Colors.white, fontSize: 20.0),
)),
)
],
),
)
],
),
),
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AddTaskScreen(),
);
}
}

Incorrect use of ParentDataWidget when using TextField

I get Incorrect use of ParentDataWidget every time I try to type in my TextField or when I move from the signup page to the login page by pressing "You already have an account?" button.
This is the code that I used for the UI
import 'package:flutter/material.dart';
import 'package:tariffo/HomePage.dart';
import 'auth.dart';
import 'LoginPage.dart';
import 'LoginScreen.dart';
import 'package:firebase_auth/firebase_auth.dart';
class SignupPage extends StatefulWidget {
#override
_SignupPageState createState() => _SignupPageState();
}
class _SignupPageState extends State<SignupPage> {
final _formKey = GlobalKey<FormState>();
String email = '';
String password = '';
String error = '';
bool loading = false;
final Authentication authentication = Authentication();
#override
Widget build(BuildContext context) {
Widget _backButton() {
return InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 0, top: 10, bottom: 10),
child: Icon(Icons.keyboard_arrow_left, color: Colors.black),
),
Text('Back',
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500))
],
),
),
);
}
return loading
? Homepage()
: Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
body: Container(
height: 900.0,
width: 500.0,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 50.0),
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios, color: Colors.white),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen()));
},
)
],
),
),
Positioned(top: 40, left: 0, child: _backButton()),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: RichText(
text: TextSpan(
text: 'Tariffo',
style: TextStyle(
color: Colors.blue,
fontFamily: 'SignPainter',
fontSize: 60),
),
),
),
SizedBox(
height: 400.0,
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 80.0, right: 80.0, top: 40.0),
child: TextFormField(
validator: (val) =>
val.isEmpty ? 'enter email' : null,
onChanged: (val) {
setState(() => email = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter email',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
),
),
Padding(
padding: const EdgeInsets.only(
left: 80.0, right: 80.0, top: 40.0),
child: TextFormField(
validator: (val) => val.length < 8
? 'enter password > 8 digits'
: null,
onChanged: (val) {
setState(() => password = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter password',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
obscureText: true,
),
),
SizedBox(height: 50),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: MaterialButton(
color: Colors.blue,
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await authentication
.registerwithEmailAndPassword(
email, password);
if (result == null) {
setState(() => error =
'Sorry,These credentials will not work out');
loading = false;
}
}
},
child: Text(
'Sign up',
style: TextStyle(
fontFamily: 'Antra', color: Colors.white),
),
),
),
],
),
)),
_createAccountLabel(),
],
),
),
);
}
Widget _createAccountLabel() {
return InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => LoginPage()));
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 20),
padding: EdgeInsets.all(15),
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Do you have an account ?',
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
SizedBox(
width: 10,
),
Text(
'Login',
style: TextStyle(
color: Colors.blue,
fontSize: 13,
fontWeight: FontWeight.w600),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
import 'SignUp.dart';
import 'brazierContainer.dart';
import 'package:google_fonts/google_fonts.dart';
import 'LoginScreen.dart';
import 'HomePage.dart';
import 'auth.dart';
class LoginPage extends StatefulWidget {
LoginPage({Key key, this.title}) : super(key: key);
final String title;
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String email = '';
String password = '';
String error = '';
bool loading = false;
final Authentication authentication = Authentication();
#override
Widget build(BuildContext context) {
Widget _backButton() {
return InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 0, top: 10, bottom: 10),
child: Icon(Icons.keyboard_arrow_left, color: Colors.black),
),
Text('Back',
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500))
],
),
),
);
}
return loading
? Homepage()
: Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
body: Container(
height: 900.0,
width: 500.0,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 50.0),
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios, color: Colors.white),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen()));
},
)
],
),
),
Positioned(top: 40, left: 0, child: _backButton()),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: RichText(
text: TextSpan(
text: 'Tariffo',
style: TextStyle(
color: Colors.blue,
fontFamily: 'SignPainter',
fontSize: 60),
),
),
),
SizedBox(
height: 400.0,
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 80.0, right: 80.0, top: 40.0),
child: TextFormField(
validator: (val) =>
val.isEmpty ? 'enter email' : null,
onChanged: (val) {
setState(() => email = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter email',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
),
),
Padding(
padding: const EdgeInsets.only(
left: 80.0, right: 80.0, top: 40.0),
child: TextFormField(
validator: (val) => val.length < 8
? 'enter password > 8 digits'
: null,
onChanged: (val) {
setState(() => password = val);
},
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: 'enter password',
hintStyle: TextStyle(
fontFamily: 'Antra',
fontSize: 12.0,
color: Colors.black)),
obscureText: true,
),
),
SizedBox(height: 50),
Padding(
padding: const EdgeInsets.only(top: 40.0),
child: MaterialButton(
color: Colors.blue,
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await authentication
.signUpWithEmailAndPassword(
email, password);
if (result == null) {
setState(() => error =
'Sorry,These credentials will not work out');
loading = false;
}
}
},
child: Text(
'Sign in',
style: TextStyle(
fontFamily: 'Antra', color: Colors.white),
),
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 10),
alignment: Alignment.centerRight,
child: Text('Forgot Password ?',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500)),
),
],
),
)),
_createAccountLabel(),
],
),
),
);
}
Widget _createAccountLabel() {
return InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => SignupPage()));
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 20),
padding: EdgeInsets.all(15),
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Don/t you have an account ?',
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
SizedBox(
width: 10,
),
Text(
'Register',
style: TextStyle(
color: Colors.blue,
fontSize: 13,
fontWeight: FontWeight.w600),
),
],
),
),
);
}
}
And this is the error:
════════ Exception caught by widgets library ═══════════════════════════════════
Incorrect use of ParentDataWidget.
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Positioned(left: 0.0, top: 40.0) wants to apply ParentData of type StackParentData to a RenderObject, which has been set up to accept ParentData of incompatible type FlexParentData.
Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. Typically, Positioned widgets are placed directly inside Stack widgets.
The offending Positioned is currently placed inside a Column widget.
You are using Positioned in a Column. Positioned can only be used in a Stack. So consider replacing your Column with a Stack or use Alignment(x,y) to align it

how to return value from cupertinoPicker in flutter

i have a button with a Text widget as child, inside a showModalBottomSheet which builds
another showModalBottomSheet holding a cupertinoPicker.
how can i return the value of cupertinopicker?
it only returns the value when i close down the first bottomsheet!! instead of doing so when the cupertinoPicker(bottomsheet) is dismissed!!
thank you all
this is my cod
FlatButton(
color: Colors.white,
onPressed: () => showModalBottomSheet<dynamic>(
isScrollControlled: true,
context: context,
builder: (BuildContext context) => Container(
child: Container(
color: Colors.black45,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding:
const EdgeInsets.only(
top: 50.0,
left: 120),
child: Text(
'Gå med',
style: TextStyle(
fontSize: 34,
color:
Colors.white),
),
),
SizedBox(
width: 40,
),
FlatButton(
onPressed: () =>
Navigator.pop(
context),
child: Icon(
Icons.clear,
color: Colors.white,
),
)
],
),
Form(
key: _formKey,
child: Column(
mainAxisSize:
MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets
.symmetric(
horizontal:
30.0,
vertical: 5.0),
child: TextFormField(
decoration: InputDecoration(
labelText:
'Restaurangens namn',
labelStyle: TextStyle(
fontWeight:
FontWeight
.bold)),
validator: (input) =>
input
.trim()
.isEmpty
? 'please enter a valid name'
: null,
onSaved: (input) =>
_name = input,
),
),
Padding(
padding: EdgeInsets
.symmetric(
horizontal:
30.0,
vertical: 5.0),
child: TextFormField(
decoration: InputDecoration(
labelText:
'Email',
labelStyle: TextStyle(
fontWeight:
FontWeight
.bold)),
validator: (input) =>
!input.contains(
'#')
? 'please enter a valid email'
: null,
onSaved: (input) =>
_email = input,
),
),
Padding(
padding:
const EdgeInsets
.only(
left: 30.0,
right: 0.0),
child: Row(
children: <Widget>[
SizedBox(
width: 200,
child:
TextFormField(
decoration: InputDecoration(
labelText:
'Telefon nummer',
labelStyle: TextStyle(
fontWeight:
FontWeight.bold)),
keyboardType:
TextInputType
.phone,
validator: (input) => input
.trim()
.isEmpty
? 'please enter a valid phone Number'
: null,
onChanged:
(input) =>
tel =
input,
),
),
],
),
),
Padding(
padding:
const EdgeInsets
.only(
left: 30.0,
right: 0.0),
child: Row(
children: <Widget>[
SizedBox(
width: 200,
),
Padding(
padding:
const EdgeInsets
.only(
top: 50.0,
left: 0),
child:
CupertinoButton(
child: Text(citys[selectedcity]),
onPressed: () =>
showModalBottomSheet(
context:
context,
builder: (BuildContext
context) =>
Container(
color: Colors
.white,
height: 200,
width: MediaQuery.of(
context)
.size
.width,
child:
CupertinoPicker(
onSelectedItemChanged:
(int
index) {
setState(
() {
selectedcity = index;
print(citys[selectedcity]);
});
},
itemExtent:
30,
children: List<
Widget>.generate(
citys
.length,
(index) {
return Center(
child:
Text(citys[index]),
);
}),
),
),
)
)
)],
),
),
Padding(
padding: EdgeInsets
.symmetric(
horizontal:
30.0,
vertical: 5.0),
child: TextFormField(
controller: _pass,
decoration: InputDecoration(
labelText:
'Lösenord',
labelStyle: TextStyle(
fontWeight:
FontWeight
.bold)),
validator: (input) =>
input.length < 6
? 'must contain at least 6 characters'
: null,
onSaved: (input) =>
_password = input,
obscureText: true,
),
),
Padding(
padding: EdgeInsets
.symmetric(
horizontal:
30.0,
vertical: 0.0),
child: TextFormField(
controller:
_confirmPass,
decoration: InputDecoration(
labelText:
'Lösernord igen',
labelStyle: TextStyle(
fontWeight:
FontWeight
.bold)),
validator: (val) {
if (val !=
_pass.text) ;
return 'Not Match';
},
onSaved: (input) =>
_password = input,
obscureText: true,
),
),
SizedBox(
height: 20,
),
Container(
width: 140,
child: OutlineButton(
onPressed: _submit,
color: Colors.black,
padding:
EdgeInsets.all(
5.0),
child: Text(
'Gå med',
style: TextStyle(
color: Colors
.white,
fontSize: 18.0),
),
),
),
SizedBox(
width: 20,
),
],
),
),
],
),
),
),
),
),
child: Text(
' Registera',
style: TextStyle(
color: Colors.black, fontSize: 18.0),
))
Every showModal...
is async and has Future return type
pushes route to Navigator - so result can be poped from this route Navigator.pop(ctx, result);
Here usable example
final dateTime = await showModalBottomSheet<DateTime>(builder: widget that does Navigator.pop(context, selectedDateTime) )
if (dateTime != null) { /* do what you need */ }

Clicking in TextFormField causes rebuild and text field subsequently loses focus

My Flutter app has been working fine right up until yesterday at which point my login screen started acting strange. As soon as I click in either the username or password text fields the keyboard shows, immediately hides, and then everything rebuilds. I've seen several threads on similar issues online, but nothing that's helped me figure out what's going wrong here. This is the gist of the code:
static final _formKey = GlobalKey<FormState>();
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(body: SingleChildScrollView(child: _buildBody()));
}
Widget _buildBody() {
return BlocProvider(create: (context) {
_loginBloc = LoginBloc(auth);
return _loginBloc;
}, child: BlocBuilder<LoginBloc, LoginState>(builder: (context, state) {
if (state is LoginFailure) {
_onWidgetDidBuild(() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${state.error}'),
backgroundColor: Colors.red,
),
);
});
_showErrorMessage(state.error);
}
return Container(
height: Styles().height(),
child: LoadingOverlay(
color: Colors.black,
opacity: 0.5,
isLoading: (state is LoginLoading),
progressIndicator: SpinKitThreeBounce(color: Colors.white),
child: Container(
padding: EdgeInsets.fromLTRB(30, 50, 30, 0),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [AppColors.DARK_BLUE, AppColors.GREEN])),
child: Column(children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 50),
child: _showLogo()),
Card(
color: Colors.white,
elevation: 5,
child: Padding(
padding: EdgeInsets.all(10),
child: Form(
key: _formKey,
child: ListView(
shrinkWrap: true,
children: <Widget>[
_buildEmailInput(),
_buildPasswordInput(),
Padding(
padding: EdgeInsets.only(top: 20),
child: ButtonTheme(
height: 50,
child: FlatButton(
color: AppColors.MEDIUM_GREEN,
child: Text(
_formMode == FormMode.LOGIN
? 'Sign in with Email'
: 'Create Account',
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight:
FontWeight.bold)),
onPressed: _validateAndSubmit,
))),
_showSecondaryButton(),
Align(
alignment: Alignment.center,
child: GestureDetector(
onTap: _showLoginRecoveryModal,
child: Text(
'Forgot Password?',
style: TextStyle(
color: AppColors.LIGHT_BLUE,
fontSize: 15,
fontWeight: FontWeight.bold),
))),
],
),
))),
Padding(
padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
child: Align(
alignment: Alignment.center,
child: Text(
'OR',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
)),
),
_buildGoogleButton()
]))));
}));
}
Widget _buildEmailInput() {
return Padding(
padding: const EdgeInsets.only(top: 20),
child: TextFormField(
maxLines: 1,
controller: _emailController,
keyboardType: TextInputType.emailAddress,
autofocus: false,
decoration: InputDecoration(
hintText: 'Email',
icon: Icon(
Icons.mail,
color: Colors.grey,
)),
validator: (value) => !EmailEditingRegexValidator().isValid(value)
? 'Invalid email address'
: null,
onSaved: (value) {
if (StringUtils.isNotNullOrEmpty(value)) {
_email = value.trim();
} else {
_email = null;
}
},
),
);
}
Widget _buildPasswordInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
child: TextFormField(
maxLines: 1,
controller: _passwordController,
obscureText: _hidePassword,
autofocus: false,
decoration: InputDecoration(
hintText: 'Password',
icon: Icon(
Icons.lock,
color: Colors.grey,
),
suffix: GestureDetector(
onTap: _toggleShowPassword,
child: Icon(
AntIcons.eye,
color: Colors.grey,
))),
validator: (value) => !NonEmptyStringValidator().isValid(value)
? 'Password can\'t be empty'
: null,
onSaved: (value) {
if (StringUtils.isNotNullOrEmpty(value)) {
_password = value.trim();
} else {
_password = null;
}
},
),
);
}
I'm not sure what I need to do here to ensure that, as soon as the keyboard shows, everything does not get rebuilt.