To design a Parent-Child UI page in Flutter for receiving data from the user, you can follow these steps:
- Create a Parent Widget class: This widget will serve as the main container for the child widgets and will manage the state between them. Let's call this widget "GroupPage".
class GroupPage extends StatefulWidget {
@override
_GroupPageState createState() => _GroupPageState();
}
class _GroupPageState extends State<GroupPage> {
// State variables to store data entered by the user
String arabicName = '';
String englishName = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Group Page'),
),
body: Column(
children: [
// Child widgets go here
],
),
);
}
}
- Create Child Widgets: Create child widget classes for each level of child groups. Each child widget will have two text input fields for the Arabic and English names, allowing the user to enter data. Let's call them "ChildGroupWidget" and "SubChildGroupWidget".
class ChildGroupWidget extends StatelessWidget {
final ValueChanged<String> onArabicNameChanged;
final ValueChanged<String> onEnglishNameChanged;
ChildGroupWidget({
required this.onArabicNameChanged,
required this.onEnglishNameChanged,
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Child Group'),
TextField(
onChanged: onArabicNameChanged,
decoration: InputDecoration(labelText: 'Arabic Name'),
),
TextField(
onChanged: onEnglishNameChanged,
decoration: InputDecoration(labelText: 'English Name'),
),
],
);
}
}
class SubChildGroupWidget extends StatelessWidget {
final ValueChanged<String> onArabicNameChanged;
final ValueChanged<String> onEnglishNameChanged;
SubChildGroupWidget({
required this.onArabicNameChanged,
required this.onEnglishNameChanged,
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Sub Child Group'),
TextField(
onChanged: onArabicNameChanged,
decoration: InputDecoration(labelText: 'Arabic Name'),
),
TextField(
onChanged: onEnglishNameChanged,
decoration: InputDecoration(labelText: 'English Name'),
),
],
);
}
}
- Add Child Widgets to the Parent Widget: Inside the build method of the GroupPage widget, add the ChildGroupWidget and SubChildGroupWidget as children of a ListView.builder. The builder will dynamically display as many child widgets as the user wants to add.
class _GroupPageState extends State<GroupPage> {
List<String> childGroupArabicNames = [];
List<String> childGroupEnglishNames = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Group Page'),
),
body: ListView.builder(
itemCount: childGroupArabicNames.length + 1,
itemBuilder: (context, index) {
if (index < childGroupArabicNames.length) {
return ChildGroupWidget(
onArabicNameChanged: (value) {
setState(() {
childGroupArabicNames[index] = value;
});
},
onEnglishNameChanged: (value) {
setState(() {
childGroupEnglishNames[index] = value;
});
},
);
} else {
return SubChildGroupWidget(
onArabicNameChanged: (value) {
setState(() {
childGroupArabicNames.add(value);
});
},
onEnglishNameChanged: (value) {
setState(() {
childGroupEnglishNames.add(value);
});
},
);
}
},
),
);
}
}
In this example, the user can add ChildGroupWidgets by entering the Arabic and English names. There is also a SubChildGroupWidget for adding child groups inside the child groups. The entered data is stored in the state variables and can be accessed as needed.
Feel free to customize the design and functionality of the Parent-Child UI page according to your specific requirements.