Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
917 views
in Technique[技术] by (71.8m points)

Flutter state rebuild when keyboard appears

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 )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...