I am building a flutter project on both app and web. I am having a couple of issues with the login page. First my column widget fills the whole width on Flutter web but I want it to be more central. Secondly I am trying to center align the Don't have an account? Sign up button which has found itself aligning to the left. Please may someone assist. Here's my code and a couple of screenshots
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Card(
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
validator: (value) =>
value!.isEmpty ? 'Email cannot be empty' : null,
onSaved: (value) => _email = value!,
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: 'Email',
hintText: 'Email',
border: OutlineInputBorder(),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
validator: (value) =>
value!.isEmpty ? 'Password cannot be empty' : null,
onSaved: (value) => _password = value!,
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: 'Password',
hintText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
),
),
Padding(
padding: const EdgeInsets.all(0),
child: TextButton(
onPressed: () {
Navigator.pushNamed(context, '/resetpassword');
},
child: const Text('Forgot Password?'),
style: TextButton.styleFrom(
primary: Colors.black,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: validateAndSubmit,
child: const Text('Login'),
style: ElevatedButton.styleFrom(
primary: Colors.black,
onSurface: Colors.black,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(15.0)),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
const Text('Dont have an account?'),
TextButton(
onPressed: () {
Navigator.pushNamed(context, '/signup');
},
child: const Text(
'Signup',
style: TextStyle(fontWeight: FontWeight.bold),
),
style: TextButton.styleFrom(
primary: Colors.black,
),
),
],
),
),
],
),
),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
To center the 'Dont have an account?' text and 'Signup' TextButton,
use mainAxisAlignment in Row.
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,//<-- this
children: [
const Text('Dont have an account?'),
TextButton(
onPressed: () {
Navigator.pushNamed(context, '/signup');
},
child: const Text(
'Signup',
style: TextStyle(fontWeight: FontWeight.bold),
),
style: TextButton.styleFrom(
primary: Colors.black,
),
),
],
),
),
To responsive web and app,
I use this method,
void main() async {
runApp(
const ResponsiveLayout(),
);
}
In my ResponsiveLayout const webScreenSize = 600;
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > webScreenSize) {
//webscreen
return widget.webScreenLayout; // <-- send user Web screen
} else {
//mobile screen
return widget.mobileScreenLayout; // <-- send user mobile screen
}
},
);
}
Read more about LayoutBuilder -
https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html
Related
It won't allow me to change the radio button inside the form, the value also does not change no matter what is selected. I have tried the code outside the form and it works fine but when i put it inside the form then it stops working for reasons I do not know.
FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('New User'),
content: Stack(
children: <Widget>[
Positioned(
right: -40.0,
top: -40.0,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(Icons.close),
backgroundColor: Colors.blue,
),
),
),
Form(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: nameController,
decoration: InputDecoration(
labelText: 'Name',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: surnameController,
decoration: InputDecoration(
labelText: 'Surname',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: userController,
decoration: InputDecoration(
labelText: 'Username',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: passController,
decoration: InputDecoration(
labelText: 'Password',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: mobileController,
decoration: InputDecoration(
labelText: 'Mobile num',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: emailController,
decoration: InputDecoration(
labelText: 'Email',
),
),
),
//radioButtons
Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
Text(
"What is your gender?",
style: TextStyle(fontSize: 18),
),
Divider(),
RadioListTile(
title: Text("Male"),
value: "male",
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
),
RadioListTile(
title: Text("Female"),
value: "female",
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
),
RadioListTile(
title: Text("Other"),
value: "other",
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
)
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
child: Text("Submit"),
onPressed: () {
//createUser();
print(gender);
Navigator.pop(context);
},
),
)
],
),
),
),
],
),
);
});
},
child: Icon(Icons.person),
backgroundColor: Colors.blue,
),
it has a few text fields and then the radio buttons, the gender variable is declared as a string.
It's Because you are assigned a value in the dialog. Just Wrap Your Alert Dialog Widget with statfulBuilder and then stateState.
example
FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: const Text('New User'),
content: Stack(
children: <Widget>[
Positioned(
right: -40,
top: -40,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: const CircleAvatar(
backgroundColor: Colors.blue,
child: Icon(Icons.close),
),
),
),
Form(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: nameController,
decoration: const InputDecoration(
labelText: 'Name',
),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: surnameController,
decoration: const InputDecoration(
labelText: 'Surname',
),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: userController,
decoration: const InputDecoration(
labelText: 'Username',
),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: passController,
decoration: const InputDecoration(
labelText: 'Password',
),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: mobileController,
decoration: const InputDecoration(
labelText: 'Mobile num',
),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: emailController,
decoration: const InputDecoration(
labelText: 'Email',
),
),
),
//radioButtons
Container(
padding: const EdgeInsets.all(20),
child: Column(
children: [
const Text(
'What is your gender?',
style: TextStyle(fontSize: 18),
),
const Divider(),
RadioListTile(
title: const Text('Male'),
value: 'male',
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
),
RadioListTile(
title: const Text('Female'),
value: 'female',
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
),
RadioListTile(
title: const Text('Other'),
value: 'other',
groupValue: gender,
onChanged: (value) {
setState(() {
gender = value.toString();
});
},
)
],
),
),
Padding(
padding: const EdgeInsets.all(8),
child: FloatingActionButton(
child: const Text('Submit'),
onPressed: () {
//createUser();
print(gender);
Navigator.pop(context);
},
),
)
],
),
),
),
],
),
);
},
);
},
);
},
backgroundColor: Colors.blue,
child: Icon(Icons.person),
);
When the keyboard appears the fab is placed in the middle of the screen instead of staying on board the keyboard
There is a lot of post on this subject on the web, but I cannot find the adequate solution to mine.
Here is the code.
Widget build(BuildContext context) {
final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
return Scaffold(
backgroundColor: backgroundColor,
appBar: AppBar(
iconTheme: const IconThemeData(color: Colors.black),
backgroundColor: Colors.transparent,
//****************************** EDITBUTTON ***************************** */
elevation: 0,
actions: [
IconButton(
onPressed: () async {
await Databahelper.instance.deleteNote(widget.note!.id!);
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const StickyNote()));
},
icon: const Icon(Icons.delete)),
//*************************** DELETE_BUTTON ***************************** */
],
),
body: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
const SizedBox(height: 8),
TextField(
maxLines: 1,
controller: titleController,
decoration: const InputDecoration(
label: Text(
'Title',
style: TextStyle(fontSize: 20),
),
),
),
const SizedBox(
height: 30,
),
Expanded(
child: TextField(
keyboardType: TextInputType.multiline,
controller: descController,
maxLines: null,
decoration: const InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.all(10),
hintText: "Enter Your Text...",
hintStyle: TextStyle(
fontSize: 12,
color: Colors.purple,
fontStyle: FontStyle.italic,
),
),
),
)
],
),
),
//---------------------------bottomNavigation --------------------------------------
bottomNavigationBar: Visibility(
visible: !isKeyboard,
child: BottomAppBar(
elevation: 4,
color: backgroundColor,
child: SizedBox(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.add_box_outlined)),
const Text('July 2050'),
IconButton(
onPressed: () {}, icon: const Icon(Icons.more_vert)),
],
),
),
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
setState(() {
title = titleController.text;
description = descController.text;
});
NotedB notelocal = NotedB(title: title, description: description);
addNote(notelocal);
Navigator.push(
context, MaterialPageRoute(builder: (_) => const StickyNote()));
},
label: const Text('Save note'),
icon: const Icon(Icons.note_add_outlined),
));
}
screenshot
in the bellow code I got the error on " controller: urlController," this line
var url;
final urlController = TextEditingController();
#override
void dispose() {
urlController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xff16352A),
title: const Text(
'Downloader',
style: TextStyle(color: Color(0xffEDAB24)),
),
),
body: Container(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 50.0),
const TextField(
controller: urlController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Paste URL',
),
),
const SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
onPressed: () {},
child: const Text(
'Download',
style: TextStyle(fontSize: 30.0),
),
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: const Color(0xff284635),
),
),
const SizedBox(width: 20.0),
TextButton(
onPressed: () {
setState(() {
url = urlController.text;
});
},
child: const Text(
'Clear',
style: TextStyle(fontSize: 30.0),
),
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: const Color(0xff284635),
),
),
],
)
],
),
),
);
}
}
Just remove const value in front of TextField
TextField(
controller: urlController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Paste URL',
),
)
Whenever you face Invalid constant value error, It sounds like you're using the const keyword but are not providing a constant value.
The fix is to remove the const keyword
Hi I'm routing menu with Drawer. It works fine for 2 pages but there is one page gives error.
The menu works correctly when I first press it, then when I go to another page and come back it gives this error
Also I tryed routing with routes on mainApp buut this way seemed easier to me
My drawer
class MyDrawer extends StatefulWidget {
#override
State<StatefulWidget> createState() => _MyDrawerState();
}
class _MyDrawerState extends State {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
child: Align(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Test",
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
],
),
),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Main'),
trailing: Icon(Icons.arrow_right),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Main(),
));
},
),
ListTile(
leading: Icon(Icons.perm_device_information),
title: Text('How'),
trailing: Icon(Icons.arrow_right),
onTap: () {
//Navigator.pushNamed(context, "/how");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HowItWorks(),
));
},
),
ListTile(
leading: Icon(Icons.keyboard),
title: Text('Contact'),
trailing: Icon(Icons.arrow_right),
onTap: () {
//Navigator.pushNamed(context, "/contact");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SendMail(),
));
},
),
],
),
);
}
}
this is the page it gives error on second route and drawer is not visible
final _formKey2 = GlobalKey<FormState>();
class SendMail extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
body: Center(
child: Form(
key: _formKey2,
child: Padding(
padding: const EdgeInsets.all(18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(48.0),
child: Text(
'İletişim',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Text('Contact Me'),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.text,
validator: (value) {
if (value.isEmpty) {
return 'Can not be empty';
}
return null;
},
decoration: InputDecoration(
filled: true,
hintText: 'Name',
border: InputBorder.none),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
filled: true,
hintText: 'Email',
border: InputBorder.none),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
maxLines: 7,
validator: (value) {
if (value.isEmpty) {
return 'Can not be empty';
}
return null;
},
decoration: InputDecoration(
filled: true,
hintText: 'Message',
border: InputBorder.none),
),
),
],
),
)
],
),
Detailed error
I have a form builder application, I have stateless widgets which get added to list view, each widget has a delete icon, I want to delete the widget and update the list view.
one of the code widget code is this
class HeaderTextWidget extends StatelessWidget {
final VoidCallback onDelete;
HeaderTextWidget({Key key, this.onDelete}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 12.0),
decoration: BoxDecoration(
color: Colors.blueGrey,
borderRadius: BorderRadius.only(
topRight: Radius.circular(15.0),
topLeft: Radius.circular(15.0),
),
),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(6.0),
child: Text(
'Header',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
Card(
elevation: 5.0,
color: Colors.blueGrey.shade50,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0),
topRight: Radius.circular(15.0),
),
),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 10.0, 8.0, 10.0),
child: TextField(
textCapitalization: TextCapitalization.words,
keyboardType: TextInputType.text,
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
decoration: InputDecoration(
hintText: 'untitled header',
hintStyle: TextStyle(
color: Colors.grey,
fontStyle: FontStyle.italic,
fontSize: 14.0,
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 4.0, 8.0, 20.0),
child: TextField(
textCapitalization: TextCapitalization.words,
keyboardType: TextInputType.text,
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.normal,
),
decoration: InputDecoration(
hintText: 'Description (optional)',
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 14.0,
),
),
),
),
Divider(
indent: 4.0,
endIndent: 4.0,
thickness: 2.0,
),
IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
IconButton(
icon: Icon(Icons.content_copy),
iconSize: 24.0,
color: Colors.blue,
onPressed: () {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('COpy'),
),
);
},
),
IconButton(
icon: Icon(Icons.delete),
iconSize: 24.0,
color: Colors.red,
onPressed: this.onDelete,
),
VerticalDivider(
thickness: 2.0,
endIndent: 6.0,
indent: 4.0,
),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: SwitchWidget(),
),
],
),
),
],
),
),
],
),
);
}
}
This is my Listview builder
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FormBuilder(
key: widget.fbKey,
autovalidate: true,
child: Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: ListView.builder(
itemCount: widget.data.widgets.length,
controller: _scrollController,
itemBuilder: (context, index) {
return widget.data.widgets[index];
},
),
),
)),
],
),
);
This is how the widget is being added the view is getting changed from selected the widget to List view builder page, when clicked on that particular icon. Where should be the delete function be added, in the List view or in the widget itself ?
Scaffold(
appBar: AppBar(
title: Text('Form Widgets'),
),
body: LayoutBuilder(
builder:
(BuildContext context, BoxConstraints viewPointConstrainsts) {
return SingleChildScrollView(
padding: EdgeInsets.all(12.0),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewPointConstrainsts.maxHeight,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FormWidget(
key: UniqueKey(),
iconData: Icons.title,
widgetTxT: 'Header',
onTap: () {
setState(() {
widget.data.widgets.add(HeaderTextWidget());
widget.pickWidgetsView = false;
});
},
),
Try the following approach:
Store only the data, not the Widget itself in the list
List<DataModel> data = []
Create list view item using createListViewItem() which should return widget as the following
ListView.builder(
itemCount: widget.dataList.length,
controller: _scrollController,
itemBuilder: (context, index) {
return createListViewItem(index);
},
),
Now inside createListViewItem(index) you can create data as per widget.dataList[index] and delete the list item on click of delete icon
setState(){
widget.dataList.deleteAt(index);
}
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton(
items: _Mothe.map(
(String dropdownMenuItem) {
return DropdownMenuItem<String>(
value: dropdownMenuItem,
child: Text(dropdownMenuItem),
);
},
).toList(),
onChanged: (String? value) {
setState(
() {
name == value!;
},
);
},
value: name,
isExpanded: true,
icon: SizedBox(),
),
],
),
);
}