为什么向下滚动时我的小部件会自行重建?

时间:2019-07-23 19:03:33

标签: flutter dart

我正在使用Flutter构建应用程序, 主页的主要窗口小部件是无状态窗口小部件,其中有许多子级,其中一些是有状态窗口小部件, 这些孩子都有一个[未来建设者]孩子, 问题是 : 当我向下滚动页面顶部的小部件时,重新创建它们自己

 Widget build(BuildContext context) {
    checkLogin();
    return Scaffold(
      key: mainscaffold,
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            Container(
              child: DrawerHeader(
                child: Column(
                  children: <Widget>[
                    FutureBuilder(
                      future: this.setLogged(),
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (!snapshot.hasData) {
                          return CircularProgressIndicator();
                        } else {
                          if (snapshot.data['success'] == 0) {
                            return Row(
                              children: <Widget>[
                                FlatButton(
                                  onPressed: () {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => Login()),
                                    );
                                  },
                                  child: AutoSizeText("Login"),
                                ),
                                FlatButton(
                                  onPressed: () {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => SignUp()),
                                    );
                                  },
                                  child: AutoSizeText("Sign Up"),
                                )
                              ],
                            );
                          } else {
                            return Column(
                              mainAxisSize: MainAxisSize.max,
                              crossAxisAlignment: CrossAxisAlignment.stretch,
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Icon(
                                  Icons.person_pin,
                                  color: Theme.of(context).primaryColor,
                                  size: 70,
                                ),
                                FlatButton(
                                  padding: EdgeInsets.all(0),
                                  onPressed: () {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => DashBoard()),
                                    );
                                  },
                                  child: Column(
                                    mainAxisSize: MainAxisSize.max,
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    children: <Widget>[
                                      Align(
                                        alignment: Alignment.topLeft,
                                        child: AutoSizeText(
                                            snapshot.data['data']['firstname']),
                                      ),
                                      Align(
                                        alignment: Alignment.topLeft,
                                        child: AutoSizeText(
                                            snapshot.data['data']['email']),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            );
                          }
                        }
                      },
                    ),
                  ],
                ),
              ),
            ),
            FutureBuilder(
                future: categories(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (!snapshot.hasData)
                    return new Center(
                      child: new CircularProgressIndicator(),
                    );
                  return Column(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.topLeft,
                        child: FlatButton(
                          color: Colors.transparent,
                          splashColor: Theme.of(context).primaryColor,
                          padding: EdgeInsets.only(left: 20),
                          child: Row(
                            children: <Widget>[
                              AutoSizeText(
                                "NEW ARRIVALS",
                              ),
                            ],
                          ),
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => Catalog(6, 0)),
                            );
                          },
                        ),
                      ),
                      Column(
                          children: List.from(snapshot.data.map((i) {
                        if (i['categories'].length > 0) {
                          return ExpansionTile(
                            title: AutoSizeText(
                              i['name'],
                            ),
                            children: List.from(i['categories'].map((subc) {
                              return Align(
                                alignment: Alignment.topLeft,
                                child: FlatButton(
                                  color: Colors.transparent,
                                  splashColor: Theme.of(context).primaryColor,
                                  padding: EdgeInsets.only(left: 20),
                                  child: Row(
                                    children: <Widget>[
                                      AutoSizeText(
                                        pr
                                            .parse(subc['name'])
                                            .body
                                            .text
                                            .toString(),
                                      ),
                                    ],
                                  ),
                                  onPressed: () {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) =>
                                              Catalog(2, subc['category_id'])),
                                    );
                                  },
                                ),
                              );
                            }).toList()),
                          );
                        } else
                          return Align(
                            alignment: Alignment.topLeft,
                            child: FlatButton(
                              color: Colors.transparent,
                              splashColor: Theme.of(context).primaryColor,
                              padding: EdgeInsets.only(left: 20),
                              child: Row(
                                children: <Widget>[
                                  AutoSizeText(
                                    pr.parse(i['name']).body.text.toString(),
                                  ),
                                ],
                              ),
                              onPressed: () {
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) =>
                                          Catalog(2, i['category_id'])),
                                );
                              },
                            ),
                          );
                      }).toList())),
                    ],
                  );
                })
          ],
        ),
      ),
      appBar: AppBar(
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Cart()),
              );
            },
            child: Padding(
              padding: const EdgeInsets.all(8),
              child: FutureBuilder(
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (!snapshot.hasData) {
                    return Container();
                  } else {
                    return Stack(
                      alignment: Alignment.center,
                      children: <Widget>[
                        Container(
                            child: Image.asset('assets/bag.png')),
                        Container(
                          child: Align(
                            alignment:Alignment.center,
                            child: AutoSizeText(snapshot.data.toString(),
                                  style: TextStyle(color: Colors.black)),
                          ),
                        ),
                      ],
                    );
                  }
                },
                future: cartContent(),
              ),
            ),
          )
        ],
        leading: IconButton(
          icon: Icon(
            Icons.menu,
            color: Colors.black,
          ),
          onPressed: () {
            mainscaffold.currentState.openDrawer();
          },
        ),
        backgroundColor: Colors.white,
        centerTitle: true,
        title: AutoSizeText(
          'ZYMTA',
          style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
        ),
        elevation: 0.2,
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            SearchRow(),
            MainSlider(),
            CategoriesHomeBuilder(),
            LatestProducts()
          ],
        ),
      ),
    );
  }

categoryHomeBuilder()向下滚动后会自动重建

0 个答案:

没有答案