首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变更值onTap

变更值onTap
EN

Stack Overflow用户
提问于 2022-01-24 21:52:56
回答 1查看 563关注 0票数 0

我有两个Inkwell,它们是pageView的控制器,当我按下任何一个is切换到相应的页面时。

代码语言:javascript
复制
                    Padding(
                  padding:
                      const EdgeInsetsDirectional.fromSTEB(0, 12, 1, 0),
                  child: SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Padding(
                          padding: const EdgeInsetsDirectional.fromSTEB(
                              16, 0, 0, 0),
                          child: InkWell(
                            onTap: () async {
                              isSecondButton = false;
                              isFirstButton = true;
                              setState(() {});
                              await pageViewController.animateToPage(
                                0,
                                duration: const Duration(milliseconds: 500),
                                curve: Curves.ease,
                              );
                            },
                            child: Material(
                              color: Colors.transparent,
                              elevation: 2,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(8),
                              ),
                              child: Container(
                                width: 100,
                                height: 100,
                                decoration: BoxDecoration(
                                    color: const Color(0xFF0D1821),
                                    borderRadius: BorderRadius.circular(8),
                                    shape: BoxShape.rectangle,
                                    border: isFirstButton
                                        ? Border.all(color: Colors.white)
                                        : null),
                                child: Stack(
                                  children: [
                                    Align(
                                      alignment: const AlignmentDirectional(
                                          0, -0.05),
                                      child: Column(
                                        mainAxisSize: MainAxisSize.max,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          Image.asset(
                                            'assets/images/memes/standardbuild.jpg',
                                            width: 50,
                                            height: 50,
                                            fit: BoxFit.cover,
                                          ),
                                          Padding(
                                            padding: EdgeInsetsDirectional
                                                .fromSTEB(0, 8, 0, 0),
                                            child: AutoSizeText(
                                              'STANDARD BUILD',
                                              textAlign: TextAlign.center,
                                              style: GoogleFonts.lexendDeca(
                                                color: Color(0xFF8B97A2),
                                                fontSize: 13,
                                                fontWeight:
                                                    FontWeight.normal,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsetsDirectional.fromSTEB(
                              16, 0, 0, 0),
                          child: Material(
                            color: Colors.transparent,
                            elevation: 2,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8),
                            ),
                            child: Container(
                              width: 100,
                              height: 100,
                              decoration: BoxDecoration(
                                  color: const Color(0xFF0D1821),
                                  borderRadius: BorderRadius.circular(8),
                                  border: isSecondButton
                                      ? Border.all(color: Colors.white)
                                      : null),
                              child: InkWell(
                                onTap: () async {
                                  isSecondButton = true;
                                  isFirstButton = false;
                                  setState(() {});
                                  await pageViewController.animateToPage(
                                    1,
                                    duration:
                                        const Duration(milliseconds: 500),
                                    curve: Curves.ease,
                                  );
                                },
                                child: Stack(
                                  children: [
                                    Align(
                                      alignment: const AlignmentDirectional(
                                          0, -0.05),
                                      child: Column(
                                        mainAxisSize: MainAxisSize.max,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          Image.asset(
                                            'assets/images/memes/pepechad.png',
                                            width: 50,
                                            height: 50,
                                            fit: BoxFit.cover,
                                          ),
                                          Padding(
                                            padding: EdgeInsetsDirectional
                                                .fromSTEB(0, 8, 0, 0),
                                            child: AutoSizeText(
                                              'ALTERNATIVE BUILD',
                                              textAlign: TextAlign.center,
                                              style: GoogleFonts.lexendDeca(
                                                color: Color(0xFF8B97A2),
                                                fontSize: 13,
                                                fontWeight:
                                                    FontWeight.normal,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),

最重要的是,在同一个onTap上更改小部件下面的另一个值,这是一个简单的字符串,存储在模型数据中,并从widget.data.tier1<- first值和widget.data.tier2 <-2值调用:

代码语言:javascript
复制
                  Padding(
                padding:
                    const EdgeInsetsDirectional.fromSTEB(0, 0, 0, 15),
                child: AutoSizeText(
                  ' lane',
                  style: GoogleFonts.lexendDeca(
                    color: Color(0xFF8B97A2),
                  ),
                ),
              ),
              Container(
                width: MediaQuery.of(context).size.width,
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    AutoSizeText(
                      widget.data.tier,
                      style: GoogleFonts.lexendDeca(
                        color: Color(0xFFFFFFFF),
                        fontSize: 45,
                        fontWeight: FontWeight.normal,
                      ),
                    ),

例如:

onTap第一个按钮->相应的页面浏览页+ widget.data.tier1第二个按钮->相应的页面查看页+ widget.data.tier2

基本上添加将widget.data从1更改为2的函数。

任何澄清都是非常感谢的,如果有什么不清楚的地方,我也很抱歉。

以下是对相应数据的“S”更改的工作方式

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-25 01:41:16

要想快速而肮脏的解决方案使其工作,请创建一个保持层状态的单例。添加以下类:

代码语言:javascript
复制
class GlobalState {
  static final GlobalState _globalState = GlobalState._();

  factory GlobalState() {
    return _globalState;
  }

  GlobalState._();

  String tier = 'S';
}

onTap of your InkWells中,将该层设置为任何您想要的内容:

代码语言:javascript
复制
setState(() {
  GlobalState().tier = 'S';
});

显示层时,从单例获取值:

代码语言:javascript
复制
AutoSizeText(
  GlobalState().tier,
  ...

我强烈建议您研究如何实现像河荚这样的状态管理库。

另外,我注意到您将isFirstButtonisSecondButton初始化为false。如果要默认为标准层,可以将isFirstButton初始化为true,并将层变量初始化为'S‘(如上面的单例所示)。

如果希望更新整个数据对象,还需要将它们存储在单例中,以便应用程序的不同部分能够访问/更新它们。这就是为什么全局状态不是最佳解决方案的原因,应该实现适当的状态管理。但如果你愿意的话可以和单身孩子一起做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70840959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档