Hello Everyone!
I am using Modal BottomSheet for view comments. When I click TextField (Comment Page) all widgets rebuilding also parents! Why all Flutter rebuilding all widgets. And why also parents widgets rebuilding? I know when keyboard appears or rotation changed eg. both StatefulWidget and StateLessWidget rebuilded. But I can’t do something in this situation. Please help me
Here CommentPage.
class CommentPage extends StatelessWidget {final String activityname;
final String postid;
final String usernameuid;
const CommentPage(
{Key key,
@required this.activityname,
@required this.postid,
@required this.usernameuid,
}): super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
height: MediaQuery.of(context).size.height * 0.8,
child: Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('Coments'),
centerTitle: true,
),
body: Container(
height:MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
child: ChangeNotifierProvider(
create: (context) => CommentLikeController(),
builder: (context, child) => FutureBuilder<List<Comment>>(
future: Provider.of<CommentLikeController>(context,
listen: false)
.initial(),
builder: (context, snapshot) {
if (snapshot.hasData)
return Stack(children: [
Positioned(
bottom: 50,
child: Container(
height:
MediaQuery.of(context).size.height * 0.8 -
105,
width: MediaQuery.of(context).size.width,
child: AnimatedList(
shrinkWrap: true,
reverse: true,
key: Provider.of<CommentLikeController>(
context)
.listkey,
initialItemCount: snapshot.data.length,
itemBuilder: (context, index, animation) {
return ListItem(
index: index,
postid: postid,
activityname: activityname,
usernameuid: usernameuid,
);
},
),
),
),
Positioned(
bottom: 0,
right: 0,
child: IconButton(
icon: Icon(Icons.add),
onPressed: () {
Provider.of<CommentLikeController>(
context,
listen: false)
.add();
})),
Positioned(
bottom: 0,
left: 0,
child: Container(
height: 50,
width:
MediaQuery.of(context).size.width - 50,
child: TextField()))
]);
else
return LinearProgressIndicator();
})),
)),
),
);
}
}
Parents CommentPage
class PageviewItem extends StatelessWidget {
final DBcontroller value;
final int index;
final String activityname;
final String username;
const PageviewItem(
{Key key,
@required this.value,
@required this.index,
@required this.activityname,
@required this.username})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: [
Container(
child: value.posts[index].urln.contains('.mp4')
? VideoItem(value: value, index: index)
: PhotoItem(value: value, index: index),
),
UserInfo(value: value, index: index),
Positioned(
bottom: 5,
left: 5,
child: GestureDetector(
onTap: () {
showpopup(context);
}, //show pop up
child: Container(
decoration: BoxDecoration(
color: Colors.blue[400].withOpacity(0.3),
borderRadius: BorderRadius.all(Radius.circular(5))),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: RichText(text: TextSpan(text: 'Comment')),
),
),
)),
Header(activityname: activityname),
],
),
);
}
showpopup(context) {
return showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10), topRight: Radius.circular(10))),
isScrollControlled: true,
context: context,
builder: (context1) {
return CommentPage(
activityname: activityname,
postid: value.posts[index].from_uid,
usernameuid: username,
);
},
);
}
}
Note I am also have PageviewItem parents cLass. And each one rebuilded when click TextField(keyboard appears )