How can I add validations to the username password when we click on the submit button.
I have created 2 different classes for Widget _buildUserNameTF() and Widget _buildPasswordTF()
when we click on Widget _buildLoginBtn() the validation must be done onWidget _buildUserNameTF() and Widget _buildPasswordTF()
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/utilities/constants.dart';
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => new _LoginPageState();
// State<StatefulWidget> createState() {
// return _LoginPageState();
// }
}
class _LoginPageState extends State<LoginPage> {
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
// FormType _formType = FormType.login;
String _userName = "";
String _password = "";
void validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
// performLogin();
}
}
void performLogin() {
final snackbar = new SnackBar(
content: new Text("Username : $_userName, password : $_password"),
);
scaffoldKey.currentState.showSnackBar(snackbar);
}
Widget _buildUserNameTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Username',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
keyboardType: TextInputType.text,
key: formKey,
autovalidate: _autoValidate,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.account_circle,
color: Colors.white,
),
hintText: 'Enter your Username',
hintStyle: kHintTextStyle,
),
validator: (value) {
return value.isEmpty ? 'Username is Required.' : null;
},
onSaved: (value) {
return _userName = value;
},
),
),
],
);
}
Widget _buildPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
obscureText: true,
key: formPassKey,
autovalidate: _autoValidate,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Colors.white,
),
hintText: 'Enter your Password',
hintStyle: kHintTextStyle,
),
validator: (String value) {
if (value.isEmpty) {
return 'Password is Required.';
}
if (value.length < 6) {
return 'Password too short.';
}
return null;
// return value.isEmpty ? 'Password is Required.' : null;
// || value.length < 6 ? 'Password too short' : null;
},
onSaved: (String value) {
return _password = value;
},
// validator: (val) =>
// val.length < 6 ? 'Password too short' : null,
// onSaved: (val) => _password = val,
),
),
],
);
}
Widget _buildLoginBtn() {
return Container(
padding: EdgeInsets.symmetric(vertical: 25.0),
width: double.infinity,
child: Form(
child: RaisedButton(
elevation: 5.0,
onPressed: (
){
validateAndSave();
},
// => print('Login Button Pressed'),
padding: EdgeInsets.all(15.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Colors.white,
child: Text(
'LOGIN',
style: TextStyle(
color: Color(0xFF527DAA),
letterSpacing: 1.5,
fontSize: 18.0,
fontWeight: FontWeight.bold,
fontFamily: 'OpenSans',
),
),
),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF73AEF5),
Color(0xFF61A4F1),
Color(0xFF478DE0),
Color(0xFF398AE5),
],
stops: [0.1, 0.4, 0.7, 0.9],
),
),
),
Container(
height: double.infinity,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 120.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
// key: formKey,
children: <Widget>[
Text(
'Sign In',
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 30.0),
_buildUserNameTF(),
SizedBox(
height: 30.0,
),
_buildPasswordTF(),
_buildLoginBtn(),
],
),
),
)
],
),
),
),
);
}
}
I tried the following as well but the output interfaces runs like this
You can copy paste run full code below
Step 1: You can remove formKey in TextFormField
TextFormField(
keyboardType: TextInputType.text,
//key: formKey,
Step 2: Wrap Column with Form and provide formKey
child: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Sign In',
working demo
full code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
final kHintTextStyle = TextStyle(
color: Colors.white54,
fontFamily: 'OpenSans',
);
final kLabelStyle = TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'OpenSans',
);
final kBoxDecorationStyle = BoxDecoration(
color: Color(0xFF6CA8F1),
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 6.0,
offset: Offset(0, 2),
),
],
);
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => new _LoginPageState();
// State<StatefulWidget> createState() {
// return _LoginPageState();
// }
}
class _LoginPageState extends State<LoginPage> {
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
// FormType _formType = FormType.login;
String _userName = "";
String _password = "";
void validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
// performLogin();
}
}
void performLogin() {
final snackbar = new SnackBar(
content: new Text("Username : $_userName, password : $_password"),
);
scaffoldKey.currentState.showSnackBar(snackbar);
}
Widget _buildUserNameTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Username',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
//decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
keyboardType: TextInputType.text,
//key: formKey,
autovalidate: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.account_circle,
color: Colors.white,
),
hintText: 'Enter your Username',
hintStyle: kHintTextStyle,
),
validator: (value) {
return value.isEmpty ? 'Username is Required.' : null;
},
onSaved: (value) {
return _userName = value;
},
),
),
],
);
}
Widget _buildPasswordTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
obscureText: true,
//key: formPassKey,
autovalidate: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.lock,
color: Colors.white,
),
hintText: 'Enter your Password',
hintStyle: kHintTextStyle,
),
validator: (String value) {
if (value.isEmpty) {
return 'Password is Required.';
}
if (value.length < 6) {
return 'Password too short.';
}
return null;
// return value.isEmpty ? 'Password is Required.' : null;
// || value.length < 6 ? 'Password too short' : null;
},
onSaved: (String value) {
return _password = value;
},
// validator: (val) =>
// val.length < 6 ? 'Password too short' : null,
// onSaved: (val) => _password = val,
),
),
],
);
}
Widget _buildLoginBtn() {
return Container(
padding: EdgeInsets.symmetric(vertical: 25.0),
width: double.infinity,
child: Form(
child: RaisedButton(
elevation: 5.0,
onPressed: () {
validateAndSave();
},
// => print('Login Button Pressed'),
padding: EdgeInsets.all(15.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Colors.white,
child: Text(
'LOGIN',
style: TextStyle(
color: Color(0xFF527DAA),
letterSpacing: 1.5,
fontSize: 18.0,
fontWeight: FontWeight.bold,
fontFamily: 'OpenSans',
),
),
),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF73AEF5),
Color(0xFF61A4F1),
Color(0xFF478DE0),
Color(0xFF398AE5),
],
stops: [0.1, 0.4, 0.7, 0.9],
),
),
),
Container(
height: double.infinity,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 120.0,
),
child: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
// key: formKey,
children: <Widget>[
Text(
'Sign In',
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 30.0),
_buildUserNameTF(),
SizedBox(
height: 30.0,
),
_buildPasswordTF(),
_buildLoginBtn(),
],
),
),
),
)
],
),
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginPage(),
);
}
}
Related
I am trying to send data to API, But I can't make it to work. I use model to get data from users.
Here is the model
class SurgeryModel {
String id;
String name;
DateTime date;
String eye;
SurgeryModel(
{required this.id,
required this.date,
required this.name,
required this.eye});
}
And I am adding data like this.
void addNewSurgeryRecord(BuildContext ctx) {
showDialog(
context: context,
builder: (context) {
// we use statefulbilder because we need to use setState to update the dropdown menu state.
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
10.0,
),
),
content: SingleChildScrollView(
child: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Text(
'Surgery Name',
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
),
),
TextField(
keyboardType: TextInputType.multiline,
maxLength: 1000,
maxLines: null,
controller: sNameController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(
15.0,
),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(15.0)),
borderSide: BorderSide(color: Colors.blue),
),
// labelText: 'Drug name',
// labelStyle: TextStyle(color: Colors.blue[200]),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blueAccent,
),
borderRadius: BorderRadius.circular(
15.0,
),
),
),
),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Text(
'Date',
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
),
),
TextFieldContainer(
child: DateTimeField(
format: format,
onChanged: (getPrescriptionDate) {
formattedTodate = getPrescriptionDate!;
},
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
},
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Select Date of Visited',
hintStyle: TextStyle(
fontSize: 15, fontWeight: FontWeight.normal),
),
initialValue: formattedTodate.toLocal(),
validator: (dateOfBirthValue) {
// dob = dateOfBirthValue.toString();
return null;
},
),
),
// TextFieldContainer(
// keyboardType: TextInputType.multiline,
// maxLength: 1000,
// maxLines: null,
// // controller: surgeryDateController,
// decoration: InputDecoration(
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(
// 15.0,
// ),
// ),
// focusedBorder: OutlineInputBorder(
// borderRadius:
// BorderRadius.all(Radius.circular(15.0)),
// borderSide: BorderSide(color: Colors.blue),
// ),
// // labelText: 'Drug name',
// // labelStyle: TextStyle(color: Colors.blue[200]),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(
// color: Colors.blueAccent,
// ),
// borderRadius: BorderRadius.circular(
// 15.0,
// ),
// ),
// ),
// ),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Text(
'Eye',
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
),
),
TextFieldContainer(
child: DropdownButtonHideUnderline(
child: DropdownButton(
icon: Visibility(
visible: false,
child: Icon(Icons.arrow_downward)),
isExpanded: true,
value: dropDownValue,
items: eyeSide.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
onChanged: ((String? value) {
setState(() {
print(value);
dropDownValue = value!;
print(' print 2$value');
});
}),
),
),
),
// TextField(
// keyboardType: TextInputType.multiline,
// maxLength: 1000,
// maxLines: null,
// controller: sEyeController,
// decoration: InputDecoration(
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(
// 15.0,
// ),
// ),
// focusedBorder: OutlineInputBorder(
// borderRadius:
// BorderRadius.all(Radius.circular(15.0)),
// borderSide: BorderSide(color: Colors.blue),
// ),
// // labelText: 'Drug name',
// // labelStyle: TextStyle(color: Colors.blue[200]),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(
// color: Colors.blueAccent,
// ),
// borderRadius: BorderRadius.circular(
// 15.0,
// ),
// ),
// ),
// ),
],
),
],
),
),
actions: [
Align(
alignment: Alignment.bottomRight,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
onPressed: () {
widget.addSurgery(
sNameController.text,
dropDownValue,
formattedTodate,
);
// _addNewSurgery(sNameController.text, sEyeController.text);
print(sNameController.text);
print(sEyeController.text);
print(formattedTodate.toString());
Navigator.of(context).pop();
},
child: Text('Done'),
),
),
],
);
});
});
}
Below function is in another screen I pass this function to the above code screen.
void _addNewSurgery(String sName, String sEye, DateTime date) {
final newProp = SurgeryModel(
date: date,
name: sName,
eye: sEye,
id: DateTime.now().toString(),
);
setState(() {
widget.surgeData.add(newProp);
});
}
With help of these codes I am sending data to the web like below code
testFunction() async {
var localStorage = await SharedPreferences.getInstance();
var data = {
'surgeryHistory ': surgeData.toList(),
};
print('viewing pres data $data');
var res = await Network().authData(data, '/route');
debugPrint('printing before response ${res.body}');
var body = json.decode(res.body);
debugPrint('printing coming response ${body}');
}
I am getting this data like this on the web side(in laravel)
public function testWebFunction(Request $request)
{
$surgery_history = $request->get('surgeryHistory');
$surgery_history_index = $surgery_history;
return $surgery_history;
}
But I am getting error like this,
Am I doing something wrong here? I need to get those data to the web and save that data in to the database using for loop. In backend side I am using foreach to save data but i cant get sizeof the variable. Any answer will help me to overcome this situation. Thank You.
I am making meme generator app . And i need to show multiple texts over the images , I am able to get one draggable text but i want the user to add multiple text . can you please help me out .
I am fetching the image via my api and i have created a stacked layout on which i have added my text .
I have provided some text customization option to users . I want users to add multiple text and they can also customize them
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'dart:convert';
import 'package:admob_flutter/admob_flutter.dart';
import 'package:bordered_text/bordered_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:meme_app/Screens/CustomMemes.dart';
import 'package:meme_app/Services/admob_services.dart';
import 'dart:ui' as ui;
import 'package:meme_app/image editor/lists.dart';
import 'package:path_provider/path_provider.dart';
class CustomSceen3 extends StatefulWidget {
final String imgUrl;
final File imgFile;
CustomSceen3({
this.imgUrl,
this.imgFile,
});
#override
_CustomSceen3State createState() => _CustomSceen3State();
}
class _CustomSceen3State extends State<CustomSceen3> {
GlobalKey<ScaffoldState> scaffoldState = GlobalKey();
Size size = Size(600, 800);
List<Widget> data = <Widget>[];
Offset offset = Offset.zero;
String addedText = '';
String newText;
int total=0;
//settings
Color pickerColor = Color(0xffffffff);
Color currentColor = Color(0xffffffff);
double textSize = 28.0;
String customFont = 'Aileron-Black';
GlobalKey _key = GlobalKey();
File _imageFile;
Random rng = new Random();
int whiteSpace = 0;
bool strike = false;
double strokeSize = 2.0;
Color strokePickerColor = Color(0xff000000);
Color strokeCurrentColor = Color(0xff000000);
TextEditingController myController = TextEditingController()
..text = "";
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: Container(
color: Colors.black,
child: Center(
child: SingleChildScrollView(
child: Container(
color: Colors.white,
child: RepaintBoundary(
key: _key,
child: Stack(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: whiteSpace.toDouble() * 5,
color: Colors.white,
),
widget.imgFile == null
? Image(
image: NetworkImage(widget.imgUrl),
)
: Image.file(widget.imgFile, height: 300,
width: MediaQuery
.of(context)
.size
.width,
fit: BoxFit.fill,),
],
),
Positioned(
left: offset.dx,
top: offset.dy,
child: GestureDetector(
onPanUpdate: (details) {
setState(() {
offset = Offset(
offset.dx + details.delta.dx, offset.dy + details.delta.dy);
});
},
child: Container(
child: BorderedText(
strokeColor: strokeCurrentColor,
strokeWidth: strokeSize ,
child: Text(
addedText,
style: TextStyle(
fontFamily: customFont,
fontSize: textSize,
color: currentColor
),
),
),
),
),
),
],
),
),
),
),
),
)
),
Container(
color: Colors.black,
height: 80.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DropdownButton<int>(
style: TextStyle(
color: Colors.white,
),
value: whiteSpace,
dropdownColor: Colors.black87,
isDense: true,
icon: Icon(Icons.padding, color: Colors.white,),
iconSize: 30,
onChanged: (int newValue) {
setState(() {
whiteSpace = newValue;
});
},
items: whiteSpaceList
.map<DropdownMenuItem<int>>((int value) {
return DropdownMenuItem<int>(
value: value,
child: Text(value.toString() + "%"),
);
}).toList(),
),
Center(
child: Text(
"Add Space", maxLines: 2, overflow: TextOverflow
.clip,
style: TextStyle(
fontSize: 11, color: Colors.white),
),
)
],
),
),
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: Icon(Icons.text_format, color: Colors.white,
size: 30,),
onPressed: () {
TextDialog();
}
),
Text(
"Text", maxLines: 2, overflow: TextOverflow.clip,
style: TextStyle(fontSize: 11, color: Colors.white),
)
],
),
),
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: Icon(
Icons.emoji_emotions_outlined, color: Colors
.white, size: 30,),
onPressed: null
),
Text(
"Sticker", maxLines: 2, overflow: TextOverflow.clip,
style: TextStyle(fontSize: 11, color: Colors.white),
)
],
),
),
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: Icon(Icons.download_outlined, color: Colors
.white, size: 30,),
onPressed: null
),
Text(
"Save", maxLines: 2, overflow: TextOverflow.clip,
style: TextStyle(fontSize: 11, color: Colors.white),
)
],
),
),
Padding(
padding: EdgeInsets.all(5.0),
child: GestureDetector(
onTap: (){
setTextStyle();
},
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
"Aa",style:TextStyle(
fontSize: 30,color: Colors.white,fontFamily: 'ComicRelief'
),
),
Text(
"Font", maxLines: 2, overflow: TextOverflow.clip,
style: TextStyle(fontSize: 11, color: Colors.white),
)
],
),
),
),
],
)
)
],
),
),
);
}
Widget setTextStyle(){
showDialog(
context: context,
builder: (BuildContext context){
return Dialog(
backgroundColor: Colors.black54,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: SizedBox(
height: 400,
child: Center(
child: Column(
children: [
CustomTextStyle(
tittle: "Black Text",
color: Colors.white,
strokecolor: Colors.white,
font: 'OpenSans-Regular',
strokewidth: 0.0,
onTap: (){
setState(() {
strokeSize= 0.0;
currentColor=Colors.black;
customFont= 'OpenSans-Regular';
strokeCurrentColor=Colors.white;
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "STANDARD MEME",
color: Colors.white,
strokecolor: Colors.black,
font: 'Anton',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Colors.white;
customFont= 'Anton';
strokeCurrentColor=Colors.black;
addedText=addedText.toUpperCase();
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "Modern Meme",
color: Colors.black,
strokecolor: Colors.white,
font: 'OpenSans-Regular',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Colors.black;
customFont= 'OpenSans-Regular';
strokeCurrentColor=Colors.white;
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "Dark Modern Meme",
color: Colors.white,
strokecolor: Colors.black,
font: 'OpenSans-Regular',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Colors.white;
customFont= 'OpenSans-Regular';
strokeCurrentColor=Colors.black;
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "Black Object White Outline",
color: Colors.black,
strokecolor: Colors.white,
font: 'Aileron-Bold',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Colors.black;
customFont= 'Aileron-Bold';
strokeCurrentColor=Colors.white;
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "White Object Black Outline",
color: Colors.white,
strokecolor: Colors.black,
font: 'Aileron-Bold',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Colors.white;
customFont= 'OpenSans-Regular';
strokeCurrentColor=Colors.black;
Navigator.of(context).pop();
});
},
),
CustomTextStyle(
tittle: "Yellow Subtittle",
color: Color(0xfeFFE400),
strokecolor: Colors.black,
font: 'OpenSans-Regular',
strokewidth: 3.0,
onTap: (){
setState(() {
strokeSize= 3.0;
currentColor=Color(0xfeFFE400);
customFont= 'OpenSans-Regular';
strokeCurrentColor=Colors.black;
Navigator.of(context).pop();
});
},
)
],
),
),
),
),
);
}
);
}
Widget TextDialog() {
showDialog(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return ListView(
children:[ Container(color: Colors.black54,
child: Dialog(
backgroundColor: Colors.transparent,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
IconButton(
icon: Icon(Icons.invert_colors, color: Colors.white),
onPressed: null),
IconButton(
icon: Icon(Icons.add_circle, color: Colors.white),
onPressed: null),
IconButton(
icon: Icon(Icons.remove_circle, color: Colors.white),
onPressed: null),
IconButton(icon: Icon(Icons.format_align_center_outlined), onPressed: null)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
OutlineButton(
borderSide: BorderSide(width: 1.0,
color: Colors.white,
style: BorderStyle.solid),
onPressed: () {
setState(() {
addedText = myController.text;
Navigator.of(context).pop();
}
);
},
child: Text(
"Got it", style: TextStyle(color: Colors.white),
),
)
],
)
],
),
textfield(),
Container(height: 50,
color: Colors.black,
child: ListView.builder(
itemCount: colors.length,
scrollDirection: Axis.horizontal, physics: ScrollPhysics(),
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(5.0),
child: GestureDetector(
onTap: () {
setState(() {
currentColor = colors[index];
Navigator.of(context).pop();
TextDialog();
});
},
child: Container(
width: 40.0,
decoration: BoxDecoration(
color: colors[index], //this is the important line
shape: BoxShape.circle,
border: Border.all(
width: 2.0, color: Colors.white60)
),
),
),
);
},
),
),
],
),
),
),
),
],
);
}
);
}
Widget textfield() {
return TextField(
controller: myController,
maxLines: 10,
);
}
}
class CustomTextStyle extends StatelessWidget {
CustomTextStyle({this.color,this.tittle,this.strokecolor,this.font,this.strokewidth,this.onTap
});
final Color color;
final Color strokecolor;
final double strokewidth;
final String tittle;
final String font;
final Function onTap;
#override
Widget build(BuildContext context) {
return ListTile(
onTap: onTap,
title: BorderedText(
strokeColor: strokecolor,
strokeWidth: strokewidth,
child: Text(
tittle,
style: TextStyle(
color: color,fontFamily: font,fontSize: 17,letterSpacing: 1.05
),
),
),
);
}
}
I need to put a var in the string place in email validator:
var email;
void validation(){
bool emailvalidated = EmailValidator.validate(email);
if (emailvalidated) {
print('object');
}
}
but it throw me this error :
Invalid arguments: email.
Help!!
You are getting the error because the validate method needs a string.
Replace your code with the one below:
It works perfectly well:
// change your email variable to a have a default string
var email = 'youremail.com';
void validation(){
bool emailvalidated = EmailValidator.validate(email);
if (emailvalidated) {
print('object');
}
}
I hope it helps.
UPDATED
Add the variables below to your class:
// define a controller to assign to your custom text field
TextEditingController emailEditingController = TextEditingController();
// declare the email variable here
String email;
Replace your emailTextFormField widget with the one below:
Widget emailTextFormField() {
return CustomTextField(
// assign your email controller to the custom text field here
textEditingController: emailEditingController,
keyboardType: TextInputType.emailAddress,
icon: Icons.email,
hint: "Email ID",
textEditingController: email,
);
}
Replace this with your validation function
void validation() {
// email should be instantiated here
email = emailEditingController.text;
bool emailvalidated = EmailValidator.validate(email);
if (emailvalidated) {
print('Email validated');
} else{
print('email not validated');
}
}
I hope this helps.
import 'package:flutter/material.dart';
import 'package:login_signup/constants/constants.dart';
import 'package:login_signup/ui/widgets/custom_shape.dart';
import 'package:login_signup/ui/widgets/customappbar.dart';
import 'package:login_signup/ui/widgets/responsive_ui.dart';
import 'package:login_signup/ui/widgets/textformfield.dart';
import 'package:email_validator/email_validator.dart';
class SignUpScreen extends StatefulWidget {
#override
_SignUpScreenState createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State<SignUpScreen> {
bool checkBoxValue = false;
double _height;
double _width;
double _pixelRatio;
bool _large;
bool _medium;
var email;
void validation(var val) {
bool emailvalidated = EmailValidator.validate(val);
if (emailvalidated) {
print('object');
}
}
#override
Widget build(BuildContext context) {
_height = MediaQuery.of(context).size.height;
_width = MediaQuery.of(context).size.width;
_pixelRatio = MediaQuery.of(context).devicePixelRatio;
_large = ResponsiveWidget.isScreenLarge(_width, _pixelRatio);
_medium = ResponsiveWidget.isScreenMedium(_width, _pixelRatio);
return Material(
child: Scaffold(
body: Container(
height: _height,
width: _width,
margin: EdgeInsets.only(bottom: 5),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Opacity(opacity: 0.88, child: CustomAppBar()),
clipShape(),
form(),
acceptTermsTextRow(),
SizedBox(
height: _height / 35,
),
button(),
infoTextRow(),
socialIconsRow(),
//signInTextRow(),
],
),
),
),
),
);
}
Widget clipShape() {
return Stack(
children: <Widget>[
Opacity(
opacity: 0.75,
child: ClipPath(
clipper: CustomShapeClipper(),
child: Container(
height: _large
? _height / 8
: (_medium ? _height / 7 : _height / 6.5),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange[200], Colors.pinkAccent],
),
),
),
),
),
Opacity(
opacity: 0.5,
child: ClipPath(
clipper: CustomShapeClipper2(),
child: Container(
height: _large
? _height / 12
: (_medium ? _height / 11 : _height / 10),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange[200], Colors.pinkAccent],
),
),
),
),
),
Container(
height: _height / 5.5,
alignment: Alignment.center,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
spreadRadius: 0.0,
color: Colors.black26,
offset: Offset(1.0, 10.0),
blurRadius: 20.0),
],
color: Colors.white,
shape: BoxShape.circle,
),
child: GestureDetector(
onTap: () {
print('Adding photo');
},
child: Icon(
Icons.add_a_photo,
size: _large ? 40 : (_medium ? 33 : 31),
color: Colors.orange[200],
)),
),
// Positioned(
// top: _height/8,
// left: _width/1.75,
// child: Container(
// alignment: Alignment.center,
// height: _height/23,
// padding: EdgeInsets.all(5),
// decoration: BoxDecoration(
// shape: BoxShape.circle,
// color: Colors.orange[100],
// ),
// child: GestureDetector(
// onTap: (){
// print('Adding photo');
// },
// child: Icon(Icons.add_a_photo, size: _large? 22: (_medium? 15: 13),)),
// ),
// ),
],
);
}
Widget form() {
return Container(
margin: EdgeInsets.only(
left: _width / 12.0, right: _width / 12.0, top: _height / 20.0),
child: Form(
child: Column(
children: <Widget>[
firstNameTextFormField(),
SizedBox(height: _height / 60.0),
lastNameTextFormField(),
SizedBox(height: _height / 60.0),
emailTextFormField(),
SizedBox(height: _height / 60.0),
phoneTextFormField(),
SizedBox(height: _height / 60.0),
passwordTextFormField(),
],
),
),
);
}
Widget firstNameTextFormField() {
return CustomTextField(
keyboardType: TextInputType.text,
icon: Icons.person,
hint: "First Name",
);
}
Widget lastNameTextFormField() {
return CustomTextField(
keyboardType: TextInputType.text,
icon: Icons.person,
hint: "Last Name",
);
}
Widget emailTextFormField() {
return CustomTextField(
keyboardType: TextInputType.emailAddress,
icon: Icons.email,
hint: "Email ID",
textEditingController: email,
);
}
Widget phoneTextFormField() {
return CustomTextField(
keyboardType: TextInputType.number,
icon: Icons.phone,
hint: "Mobile Number",
);
}
Widget passwordTextFormField() {
return CustomTextField(
keyboardType: TextInputType.text,
obscureText: true,
icon: Icons.lock,
hint: "Password",
);
}
Widget acceptTermsTextRow() {
return Container(
margin: EdgeInsets.only(top: _height / 100.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Checkbox(
activeColor: Colors.orange[200],
value: checkBoxValue,
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}),
Text(
"I accept all terms and conditions",
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: _large ? 12 : (_medium ? 11 : 10)),
),
],
),
);
}
Widget button() {
return RaisedButton(
elevation: 0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
onPressed: () {
validation(email);
},
textColor: Colors.white,
padding: EdgeInsets.all(0.0),
child: Container(
alignment: Alignment.center,
// height: _height / 20,
width: _large ? _width / 4 : (_medium ? _width / 3.75 : _width / 3.5),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
gradient: LinearGradient(
colors: <Color>[Colors.orange[200], Colors.pinkAccent],
),
),
padding: const EdgeInsets.all(12.0),
child: Text(
'SIGN UP',
style: TextStyle(fontSize: _large ? 14 : (_medium ? 12 : 10)),
),
),
);
}
Widget infoTextRow() {
return Container(
margin: EdgeInsets.only(top: _height / 40.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Or create using social media",
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: _large ? 12 : (_medium ? 11 : 10)),
),
],
),
);
}
Widget socialIconsRow() {
return Container(
margin: EdgeInsets.only(top: _height / 80.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircleAvatar(
radius: 15,
backgroundImage: AssetImage("assets/images/googlelogo.png"),
),
SizedBox(
width: 20,
),
CircleAvatar(
radius: 15,
backgroundImage: AssetImage("assets/images/fblogo.jpg"),
),
SizedBox(
width: 20,
),
CircleAvatar(
radius: 15,
backgroundImage: AssetImage("assets/images/twitterlogo.jpg"),
),
],
),
);
}
Widget signInTextRow() {
return Container(
margin: EdgeInsets.only(top: _height / 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Already have an account?",
style: TextStyle(fontWeight: FontWeight.w400),
),
SizedBox(
width: 5,
),
GestureDetector(
onTap: () {
Navigator.of(context).pop(SIGN_IN);
print("Routing to Sign up screen");
},
child: Text(
"Sign in",
style: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.orange[200],
fontSize: 19),
),
)
],
),
);
}
}
Here:
import 'package:flutter/material.dart';
import 'package:login_signup/ui/widgets/responsive_ui.dart';
class CustomTextField extends StatelessWidget {
final String hint;
final TextEditingController textEditingController;
final TextInputType keyboardType;
final bool obscureText;
final IconData icon;
double _width;
double _pixelRatio;
bool large;
bool medium;
CustomTextField(
{this.hint,
this.textEditingController,
this.keyboardType,
this.icon,
this.obscureText= false,
});
#override
Widget build(BuildContext context) {
_width = MediaQuery.of(context).size.width;
_pixelRatio = MediaQuery.of(context).devicePixelRatio;
large = ResponsiveWidget.isScreenLarge(_width, _pixelRatio);
medium= ResponsiveWidget.isScreenMedium(_width, _pixelRatio);
return Material(
borderRadius: BorderRadius.circular(30.0),
elevation: large? 12 : (medium? 10 : 8),
child: TextFormField(
controller: textEditingController,
keyboardType: keyboardType,
cursorColor: Colors.orange[200],
decoration: InputDecoration(
prefixIcon: Icon(icon, color: Colors.orange[200], size: 20),
hintText: hint,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide.none),
),
),
);
}
}
Here, I am trying flutter_form_builder for the dropdown. but there is some problem when I check the validation of all fields while button clicks on the Next button. it will check the form state is valid or not. if I click on the next button it will show all the required filed it will show me dropdown also but id I am not the select value from drop-down then it needs do not redirect any other page without selecting dropdown value because there is required validation. so the issue is drop-down validation is showing but not working.
Here is code of my screen :
class _AddWalkinServiceScheduleScreenState
extends State<AddWalkinServiceScheduleScreen>
with TickerProviderStateMixin {
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
AddWalkinModel model;
bool autovalidate = false;
final TextEditingController _bspBusinessLegalAddress =
TextEditingController();
LocationResult _pickedLocation;
Map<String, dynamic> _typeValue;
AnimationController controller;
Animation<double> animation;
final TextEditingController _serviceDate = TextEditingController();
TextEditingController _serviceTime = new TextEditingController();
String _isoDate;
String addresschoice;
List<String> _imageFilesList2 = [];
List<File> _licenseImages2 = [];
bool _isFlexible = false;
String _serviceType;
List<dynamic> _type = <dynamic>[];
#override
void initState() {
super.initState();
}
Widget _builddate() {
return Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 11),
child: Text(
"Date",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
_buildservicedate(),
],
),
);
}
Widget _buildselectAddress() {
return Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 11),
child: Text(
"Select Address",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
_buildaddresschoice(),
addresschoice == "Current Location"
? _addressTextfield()
: (addresschoice == "Select from address book" ||
model.address != null)
? _addressTextfield()
: SizedBox(),
_buildServicetype()
],
),
);
}
Widget _addressTextfield() {
return TudoTextWidget(
prefixIcon: Icon(FontAwesomeIcons.mapMarkedAlt),
labelText: "Address",
hintText: "Address",
controller: _bspBusinessLegalAddress,
validator: (val) =>
Validators.validateRequired(val, "Address"),
);
}
Widget _buildServicetype() {
return FormBuilder(
autovalidate: autovalidate,
child: FormBuilderCustomField(
attribute: "Select Address",
validators: [FormBuilderValidators.required()],
formField: FormField(
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: InputDecoration(
prefixIcon: Icon(Icons.business_center),
errorText: field.errorText,
),
isEmpty: _typeValue == [],
child: new DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text("Service Type"),
isExpanded: true,
items: [
"Normal",
"Urgent",
"Emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: field.value,
onChanged: (value) {
field.didChange(value);
_serviceType = value;
},
),
),
);
},
)),
);
}
Widget content(BuildContext context, AddWalkinServiceDetailViewModel awsdVm) {
var colorStyles = Theming.colorstyle(context);
Orientation orientation = MediaQuery.of(context).orientation;
return Scaffold(
backgroundColor: colorStyles['primary'],
appBar: AppBar(
elevation: 0,
title: Text("Service Details"),
centerTitle: true,
),
bottomNavigationBar: Container(
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new FlatButton.icon(
icon: Icon(FontAwesomeIcons.arrowCircleRight),
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
label: Text('Search'),
color: colorStyles["primary"],
textColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
onPressed: () {
setState(() {
autovalidate = true;
});
if (_formkey.currentState.validate()) {
List<ServicePicture> id1Images = [];
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ServiceProviderMapScreen(
addWalkinModel: model,
),
),
);
}
}
),
],
),
),
body: FadeTransition(
opacity: animation,
child: Container(
child: Form(
autovalidate: autovalidate,
key: _formkey,
child: Stack(
children: <Widget>[
SingleChildScrollView(
padding: EdgeInsets.all(16.0),
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_builddate(),
_buildflexible(),
],
),
),
)
],
),
),
),
),
);
}
#override
Widget build(BuildContext context) {
return new StoreConnector<AppState, AddWalkinServiceDetailViewModel>(
converter: (Store<AppState> store) =>
AddWalkinServiceDetailViewModel.fromStore(store),
builder: (BuildContext context, AddWalkinServiceDetailViewModel awsdVm) =>
content(context, awsdVm),
);
}
}
You should be using FormBuilderDropdown instead. This is included in flutter_form_builder.
DropdownButtonHideUnderline(
child: FormBuilderDropdown(
name: 'dropdown'
hint: Text("Service Type"),
isExpanded: true,
items: [
"Normal",
"Urgent",
"Emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),,
),
),
Using this, the dropdown value can be extracted by calling it from the Map GlobalKey<FormState>.currentState.value using the name set earlier as the key.
_formKey.currentState.value['dropdown']
I have a for Loop where articles get loaded into a Widget and I only want one to be enabled, so how can I do that?
So far I got it working, but it doesn't recognise if I pressed another Button and I am relatively new to Flutter so I don't exactly know how I can implement such a feature.
Widget for Articles
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wiegon/icons/nuleo_icons.dart';
import 'package:wiegon/main.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:wiegon/pages/control_screen.dart';
import 'package:wiegon/widgets/widgets.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:page_transition/page_transition.dart';
class ArticleandAmount extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: new SvgPicture.asset(wiegonLogo,
allowDrawingOutsideViewBox: true,
semanticsLabel: 'Wiegon Logo'),
onPressed: null,
);
},
),
title: Text(
"Artikel und Menge",
style: TextStyle(fontSize: 18, color: Color(0xffFDFEFE)),
),
backgroundColor: Color(0xff466988),
),
body: Articleamount(),
endDrawer: Enddrawer());
}
}
class Articleamount extends StatefulWidget {
#override
_ArticleamountState createState() {
return _ArticleamountState();
}
}
String displayedWeight;
String displayedTotalPrice;
String displayedPrice;
String articleID;
String unit = "";
String articleSelected = "Artikel wählen";
class _ArticleamountState extends State<Articleamount> {
final amountController = TextEditingController();
static Color disabledColor = mainColor.withOpacity(0.5);
double price = 0.0;
double inputAmount = 0;
bool isArticleSelected = false;
bool isPriceSelected = false;
Color buttonColor = disabledColor;
String totalPrice = "0";
#override
void dispose() {
// Clean up the paremeters when the widget is disposed.
unit = "";
articlesList = {};
articleSelected = "Artikel wählen";
amountController.dispose();
super.dispose();
}
Widget articleCard(
String articleName, IconData icon, double priceForArticle) {
String tempunit = articleName.split("|")[1];
String tempID = articleName.split("&")[1].split("|")[0];
articleName = articleName.split("&")[0];
return SingleChildScrollView(
child: Container(
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(20),
),
width: 300,
height: 120,
padding: EdgeInsets.all(5),
child: MaterialButton(
child: Align(
alignment: Alignment.centerLeft,
child: Text(
articleName,
overflow: TextOverflow.fade,
style: TextStyle(fontWeight: FontWeight.bold),
)),
highlightColor: Colors.blue,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
10,
)),
elevation: 2,
onPressed: () {
articleSelected = articleName;
articleID = tempID;
setState(() {
amountController.text = "";
totalPrice = "0";
buttonColor = disabledColor;
inputAmount = 0;
unit = tempunit;
price = priceForArticle;
isArticleSelected = true;
});
},
),
));
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(children: <Widget>[
FractionallySizedBox(
widthFactor: deviceWidth(context),
child: Center(
child: Container(
width: 600,
margin: EdgeInsets.only(top: 35),
color: Colors.transparent,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
progressBar(Nucleo.checked, "Bürger", true),
progressLine(true, context),
progressBar(
Nucleo.selected_two, "Artikel und Menge", true),
progressLine(false, context),
progressBar(Nucleo.three, "Buchung", false),
],
),
Container(
margin: EdgeInsets.only(top: 35),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Bürger".toUpperCase(),
style: TextStyle(
fontSize: 13,
color: Colors.grey,
fontWeight: FontWeight.bold),
),
new Padding(
padding: EdgeInsets.only(top: 3),
),
Text(
getCitizenInformation(),
style: TextStyle(
fontSize: 17, color: secondaryColor),
)
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(top: 30),
// width: 600,
child: Row(children: [
Column(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Artikel und Menge wählen",
style: TextStyle(
color: secondaryColor,
fontSize: 21,
fontWeight: FontWeight.bold),
),
Container(
margin: EdgeInsets.only(top: 4),
height: 5,
width: 30,
color: secondaryColor)
],
),
]),
)
],
),
Row(
children: [
Container(
margin: EdgeInsets.only(top: 40),
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.transparent),
width: 600,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Wrap(
alignment: WrapAlignment.spaceBetween,
children: <Widget>[
for (var article in articlesList.keys)
articleCard(
article,
MdiIcons.chessQueen,
articlesList[article])
],
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 40),
height: 70,
width: 600,
decoration: new BoxDecoration(
borderRadius:
BorderRadius.circular(10),
color: Colors.white),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
Container(
width: 220,
margin:
EdgeInsets.only(left: 10),
child: TextField(
onChanged: (text) {
totalPrice = calculatePrice(
text, price);
},
keyboardType:
TextInputType.number,
controller: amountController,
decoration: InputDecoration(
labelStyle: TextStyle(
fontSize: 13),
labelText:
"$articleSelected"),
),
),
Text(
"á € $price / $unit",
style: TextStyle(
color: primaryColor,
fontSize: 20),
),
Text(
"$totalPrice€",
style: TextStyle(
color: secondaryColor,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
],
),
),
],
),
Container(
margin: EdgeInsets.only(top: 30),
child: Row(
crossAxisAlignment:
CrossAxisAlignment.end,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
ButtonTheme(
minWidth: 280,
height: 50,
child: OutlineButton(
borderSide: BorderSide(
color: mainColor, width: 2),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10)),
child: Text(
"Zurück",
style: TextStyle(
color: mainColor,
fontSize: 16,
),
),
onPressed: () {
Navigator.pop(context);
},
),
),
MaterialButton(
minWidth: 280,
height: 50,
color: buttonColor,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10)),
child: Text(
"Weiter",
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
onPressed: () {
if (isArticleSelected == true &&
isPriceSelected == true &&
inputAmount != 0) {
Navigator.push(
context,
PageTransition(
type: PageTransitionType
.rightToLeftWithFade,
child:
Controlscreen()));
} else {
Fluttertoast.showToast(
msg:
"Eingaben bitte überprüfen",
toastLength:
Toast.LENGTH_SHORT,
gravity:
ToastGravity.BOTTOM,
timeInSecForIos: 1,
backgroundColor:
Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
}),
],
),
)
]),
),
],
),
]))))
]));
}
String calculatePrice(String inputText, double price) {
double amount;
if (inputText.isNotEmpty) {
amount = double.parse(inputText);
} else {
amount = 0;
}
double total = (amount * price);
setState(() {
totalPrice = total.toStringAsFixed(2);
isPriceSelected = true;
inputAmount = amount;
buttonColor = mainColor;
});
displayedTotalPrice = totalPrice;
displayedWeight = amount.toString();
displayedPrice = price.toString();
return totalPrice;
}
}
You can use a variable to know if a button is already pressed. Like this:
var isSelected = false;
FlatButton(
onPressed: () => { isSelected = true },
child: Text(
"Selling"
),
color: isSelected ? Theme.of(context).primaryColor : Colors.grey,
),