代码之家  ›  专栏  ›  技术社区  ›  Ujang Aripin

Flutter:如何在盒子内创建图像使用borderRadius with Text装饰

  •  0
  • Ujang Aripin  · 技术社区  · 2 年前

    我使用下面的代码,如何使图像显示在遵循borderRadius图案的盒子装饰中?

    Flexible(
      child: GestureDetector(
        onTap: () {
          print("Menu 1");
        },
        child: Container(
          height: 150,
          width: double.infinity,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.white
          ),
          child: Column(
              children: [
                Image.asset(
                  "assets/images/new_images/korean_food.jpg",
                  fit: BoxFit.cover,
                ),
                SizedBox(height: 10,),
                Text(
                  'Paket Korea',
                  style: TextStyle(
                    fontFamily: "Khand",
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Text(
                  'Rp 30.000',
                  style: TextStyle(
                    fontFamily: "Khand",
                    fontSize: 14,
                    color: Color.fromRGBO(2, 161, 113, 1),
                    fontWeight: FontWeight.w300,
                  ),
                ),
              ],
            ),
        ),
      ),
    ),
    

    这就是borderRadius图像后面的设计应该是什么样子

    enter image description here

    这是我运行代码后的结果

    enter image description here

    有人能帮我解决这个问题吗?

    1 回复  |  直到 2 年前
        1
  •  3
  •   Vivek Chib    2 年前

    新答案 使用 clipBehaviour 容器属性:

    Container(
          margin: const EdgeInsets.all(8.0),
          clipBehavior: Clip.antiAlias,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10), color: Colors.green),
          child: Column(children: ...),
        )
    

    老答案 去除 borderRadius 从容器中取出并用包裹容器 ClipRRect 小装置:

    ClipRRect(
            borderRadius: BorderRadius.circular(12.0),
            child: Container(
              color: Colors.green,
              child: Column(
                children: [
                  Container(
                    height: 100,
                    color: Colors.orange,
                    alignment: Alignment.center,
                    child: const Text("Image"),
                  ),
                  const SizedBox(height: 8),
                  const Text("Title"),
                  const SizedBox(height: 8),
                  const Text("Price"),
                  const SizedBox(height: 8),
                ],
              ),
            ),
          )