dynamic add form field in flutter
6,660
You are applying completely wrong logic here. You can't directly return widget
in an widget onPressed()
function.
example abstract of your code to add TextFormField:
class MyClass extends StatefulWidget {
@override
_MyClassState createState() => new _MyClassState();
}
class _MyClassState extends State<MyClass> with SingleTickerProviderStateMixin {
List<Widget> _phoneWidgets = List<Widget>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Text(
'+',
style: TextStyle(fontSize: 20.0),
),
shape: RoundedRectangleBorder(
side: BorderSide(
color: Colors.black26, width: 1.0, style: BorderStyle.solid),
borderRadius: BorderRadius.circular(10.0)),
onPressed: () {
setState(() {
_phoneWidgets.add(Phone(
fieldName: 'Phone Number',
));
});
},
),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 25, vertical: 30),
child: Column(
children: List.generate(_phoneWidgets.length, (i) {
return _phoneWidgets[i];
}),
)),
);
}
}
class Phone extends StatelessWidget {
String fieldName;
Phone({this.fieldName = ''});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 8.0),
child: TextFormField(
keyboardType: TextInputType.phone,
decoration: InputDecoration(
contentPadding:
EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide(color: Colors.white, width: 0.1),
),
filled: true,
icon: Icon(
Icons.phone,
color: Colors.black,
size: 20.0,
),
labelText: fieldName,
labelStyle: TextStyle(
fontSize: 15.0,
height: 1.5,
color: Color.fromRGBO(61, 61, 61, 1)),
fillColor: Color(0xffD2E8E6),
),
maxLines: 1,
),
);
}
}
Author by
santosh adhikari
Updated on December 03, 2022Comments
-
santosh adhikari over 1 year
I want this in flutter but I did not get add phone number when click on
[+]
button. When I've called Phone widget I got this error:W/AccessibilityBridge(16228): can't invoke getChildId with reflection
below is my code:
RaisedButton( child: Text( '+', style: TextStyle(fontSize: 20.0), ), shape: RoundedRectangleBorder( side: BorderSide( color: Colors.black26,width: 1.0, style: BorderStyle.solid), borderRadius: BorderRadius.circular(10.0)), onPressed: () { Phone(); print('add phone is pressed'); }, color: Colors.white, ), class Phone extends StatelessWidget { @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(right: 8.0), child: TextFormField( keyboardType: TextInputType.phone, decoration: const InputDecoration( contentPadding: EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.all( Radius.circular(10.0), ), borderSide: BorderSide(color: Colors.white, width: 0.1), ), filled: true, icon: Icon( Icons.phone, color: Colors.black, size: 20.0, ), labelText: 'Phone Number', labelStyle: TextStyle( fontSize: 15.0, height: 1.5, color: Color.fromRGBO(61, 61, 61, 1)), fillColor: Color(0xffD2E8E6), ), maxLines: 1, ), ); } }
-
santosh adhikari over 4 yearsThank you for your reply it really solve my problem
-
proversion over 4 yearsNow, how can I get the value in each dynamically created fields?