【问题标题】:Flutter :- Align a icon to the right bottom of a containerFlutter :- 将图标与容器的右下角对齐
【发布时间】:2020-07-28 02:10:33
【问题描述】:

我尝试了一个带有对齐图标的列,但它没有正确显示

如果有人有任何想法可以提供帮助。这将不胜感激。谢谢

这是我尝试过的代码

    List<Widget> temp = [];
    listAirline.map((airline) {
      listAirlineName.map((item) {
        if (airline.name == item) {
          temp.add(
            Column(
              children: <Widget>[
                Container(
                  height: 20,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius:
                        BorderRadius.all(Radius.elliptical(100.0, 60.0)),
                  ),
                  child: Image.asset(
                    "images/Airlines/" + airline.id + ".jpg",
                    fit: BoxFit.fitWidth,
                  ),
                ),
                Align(
                  alignment: Alignment.bottomRight,
                  child: Icon(
                    Icons.remove_circle,
                    color: Colors.white,
                  ),
                )
              ],
            ),
          );
        }
      }).toList();
    }).toList();

    return temp;
  }```



【问题讨论】:

  • @Dohan,请检查以下解决方案,如果有问题请告诉我

标签: image flutter icons alignment containers


【解决方案1】:

您需要使用 Stack 小部件,我已经使用 Stack 小部件完成了类似的任务,请查看以下解决方案

class HomeScreen extends StatefulWidget {


  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomeScreen();
  }
}

class _HomeScreen extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text("Home"),
        ),
        body: Container(
            height: 100.0,
            child: Align(
          alignment: Alignment.topCenter,
          child: Stack(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(top: 20.0),
                height: MediaQuery.of(context).size.width*0.15,
                width: MediaQuery.of(context).size.width*0.4,
                child: Container(
                  margin: EdgeInsets.all(5.0),
                  decoration: BoxDecoration(
                    color: Colors.black,
                    borderRadius: BorderRadius.all(Radius.elliptical(20.0, 20.0)),
                  ),

                ),
              )
              ,
              Positioned(
                right: 5.0,
                bottom: 0.0,
                child:
                   Icon(
                    Icons.remove_circle,
                    color: Colors.red,
                  ),

              )
            ],
          ),
        )));
  }


}

以上代码的输出如下

【讨论】:

  • Stack 不是一个好的解决方案,因为您必须使用一些像 5 这样的幻数来将右下角的 x 图标居中 - 如果您的图标大小未知(并且您无法知道Stack 不起作用的Icon 的默认大小 - 在这种情况下,您可以使用Align,如我上面的评论或SizedOverflowBox
  • 类似:child: Center( child: Container( // width: 100, // height: 100, color: Colors.green, alignment: Alignment.topRight, child: SizedOverflowBox( size: Size.zero, alignment: Alignment.center, child: Material(type: MaterialType.circle, color: Colors.white, child: Icon(Icons.add)), ), ), ),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 2020-07-07
  • 2019-08-11
相关资源
最近更新 更多