Reputation: 51
The page is working fine. if I click the "add medication" button, the action is performed. but if I repeated it more than once. The page is frozen. Error: Incorrect parent use of the widget.
If I click the button, the page is struck.
How to solve this?
class _AdditionalBackState extends State<AdditionalBack> {
String title = 'Additional Background Info';
List<Widget> list = new List();
bool valuee = false;
int conselectedRadio;
String value;
@override
void initState() {
super.initState();
conselectedRadio = 0;
}
setSelectedRadio(int val) {
setState(() {
conselectedRadio = val;
});
}
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
backgroundColor: Color(0xfff38c0fa),
title: Text(title),
leading: IconButton(
icon: Icon(
Icons.chevron_left,
color: Colors.white,
),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => Reasonvisit()));
},
),
actions: [
IconButton(
icon: Icon(
Icons.chevron_right,
color: Colors.white,
),
onPressed: () {
// do something
// Navigator.push(context, MaterialPageRoute(builder: (context)=> AdditionalBack()));
},
),
],
),
body: Form(
child: SingleChildScrollView(
child: Column(
children: [
Container(
child: ListTile(
contentPadding: EdgeInsets.all(24),
title: Text(
'Are you taking any medication?',
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
),
),
),
Container(
child: ListTile(
leading: Radio(
value: 2,
groupValue: conselectedRadio,
activeColor: Colors.lightBlueAccent,
onChanged: (val) {
setState(() {
conselectedRadio = val;
});
}),
title: Text('Yes'),
),
),
Container(
child: ListTile(
leading: Radio(
value: 1,
groupValue: conselectedRadio,
activeColor: Colors.lightBlueAccent,
onChanged: (val) {
setSelectedRadio(val);
setState(() {
valuee = true;
});
if (valuee) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MedicalHistory()));
}
}),
title: Text('No'),
),
),
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
validator: (value) {
if (value.isEmpty) return "Name cant be empty";
return null;
},
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
hintText: 'Name of the medication ',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
filled: true,
fillColor: Colors.grey[200],
),
onChanged: (text) {
value = text;
},
),
),
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
validator: (value) {
if (value.isEmpty) return "Name cant be empty";
return null;
},
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
hintText: 'Dosage',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
filled: true,
fillColor: Colors.grey[200],
),
onChanged: (text) {
value = text;
},
),
),
Container(
padding: EdgeInsets.all(29),
child: RaisedButton.icon(
padding: EdgeInsets.all(13),
color: Color(0xfff38c0fa),
label: Text(
"Add another medication",
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
onPressed: () {
list.add(
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
hintText: 'Name of the medication ',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
filled: true,
fillColor: Colors.grey[200],
),
),
),
);
list.add(
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
hintText: 'Dosage ',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
filled: true,
fillColor: Colors.grey[200],
),
),
),
);
setState(() {});
},
splashColor: Colors.amber,
icon: Icon(
Icons.add,
color: Colors.white,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
Container(
padding: EdgeInsets.all(20.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
Widget widget = list.elementAt(index);
return widget;
},
itemCount: list.length,
),
),
Container(
padding: EdgeInsets.all(29),
child: RaisedButton.icon(
padding: EdgeInsets.all(13),
color: Color(0xfff38c0fa),
label: Text(
"Continue",
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MedicalHistory()));
},
splashColor: Colors.amber,
icon: Icon(
Icons.arrow_forward,
color: Colors.white,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
),
);
}
}
Upvotes: 0
Views: 91
Reputation: 540
Remove this line
resizeToAvoidBottomPadding: false,
and add physics to Listview
ListView.builder(
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
Widget widget = list.elementAt(index);
return widget;
},
itemCount: list.length,
),
Upvotes: 0
Reputation: 2130
I run your code and it looks right to me. But when you click on add meditation more than once then the widget data will add to your listview and it converts into scroll so when you tried to go up this doesn't work. So you need to stop scrolling of your listview and it will solve
ListView.builder(
physics: NeverScrollableScrollPhysics(), // May be by adding this line your issue will solve
....
itemCount: list.length,
)
Upvotes: 1