【问题标题】:Flutter - Add a container or box underneath a rowFlutter - 在一行下方添加一个容器或盒子
【发布时间】:2022-01-24 03:01:58
【问题描述】:

我正在尝试添加一个框(希望以后可以为其分配一个 onPressed 函数)。我尝试在骰子下方添加一个容器,但盒子紧挨着第二个骰子,但我实际上需要它在两个骰子下方。

我也尝试过 Listview,但没有运气。这是我的代码:

 Widget build(BuildContext context) {
return Center(
    child: Row(
  children: <Widget>[
    Expanded(
      child: TextButton(
        onPressed: () {
          changeDiceFace();
        },
        child: Image.asset('images/dice$leftDiceNumber.png'),
      ),
    ),
    Expanded(
      child: TextButton(
        onPressed: () {
          changeDiceFace();
        },
        child: Image.asset('images/dice$rightDiceNumber.png'),
      ),
    ),
  ],
));

} }

提前致谢!

我对 Flutter 和编码世界非常陌生:D

【问题讨论】:

  • 骰子下方容器的代码在哪里?
  • 您好 Gretal,感谢您的回复。容器代码不起作用所以我把它拿出来。
  • 如果您添加您尝试添加容器的方式,那么您在哪里做错了可以说...
  • 谢谢你。很抱歉给您带来了困惑,下次我会尝试更好地解释我的问题。

标签: flutter flutter-layout


【解决方案1】:

我想你想要的答案是这个。

如果要在点击时触发事件,可以使用 InkWell 或 GestureDetector。

如果您想更改点击状态,请使用setState

Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: size.width * 0.4,
                height: size.height * 0.2,
                alignment: Alignment.center,
                child: Image(image: AssetImage('imagePath'),),
              ),
              SizedBox(
                width: size.width * 0.1,
              ),
              Container(
                width: size.width * 0.4,
                height: size.height * 0.2,
                alignment: Alignment.center,
                child: Image(image: AssetImage('imagePath'),),
              ),
            ],
          ),
          SizedBox(
            height: size.height * 0.05,
          ),
          InkWell(
            onTap: (){ changeDiceFace(); },
            child: Container(
              width: size.width * 0.6,
              height: size.height * 0.1,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.yellow, width: 7.0),
              ),
              alignment: Alignment.center,
              child: Text(
                'Click',
                style:
                    TextStyle(color: Colors.yellow, fontSize: size.width * 0.1),
              ),
            ),
          ),
        ],
      );

【讨论】:

  • 非常感谢 Jungwon!我试试看
猜你喜欢
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多