【问题标题】:How to insert border on material widget如何在材质小部件上插入边框
【发布时间】:2020-09-09 11:47:54
【问题描述】:

我是 Flutter 的初学者,我无法在 Material Widget 上插入圆角边框。我已经插入了适当的borderRadius: new BorderRadius.circular(6.0),,但我看不出我做错了:

我的代码:

Widget _buildCategoryItem(index) {
return new GestureDetector(
  onTap: () {
    onTabCategory(index);
  },
  child: new Center(
    child: new Container(
      margin: new EdgeInsets.only(left: 10.0),
      child: new Material(
        elevation: 2.0,
        borderRadius: new BorderRadius.circular(6.0),
        child: new Container(
          padding: new EdgeInsets.only(
              left: 12.0, top: 7.0, bottom: 7.0, right: 12.0),
          color: _category_selected == index
              ? Colors.orange[800]
              : Colors.orange[500],
          child: new Text(
            _categorys[index],
            style: new TextStyle(color: Colors.white),
          ),
        ),
      ),
    ),
  ),
);

}

类别标签的格式为正方形,但我想将其设置为圆形:

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    您可以通过其 shape 属性为Material widget 插入边框

      Material(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
            side: const BorderSide(color: Colors.red, width: 1),
          ),
          child: const Padding(
            padding: EdgeInsets.all(8.0),
            child: Text('Material widget'),
          ),
        ),
    

    【讨论】:

      【解决方案2】:

      我得到了解决方案。我可以使用ClipRRect Widget 来包裹 Material Widget 并使角变圆。

      我的代码已修复:

      Widget _buildCategoryItem(index) {
      return new GestureDetector(
        onTap: () {
          onTabCategory(index);
        },
        child: new Center(
          child: new Container(
            margin: new EdgeInsets.only(left: 10.0),
            child: new ClipRRect(
              borderRadius: BorderRadius.circular(6.0),
              child: new Material(
                elevation: 2.0,
                child: new Container(
                  padding: new EdgeInsets.only(
                      left: 12.0, top: 7.0, bottom: 7.0, right: 12.0),
                  color: _category_selected == index
                      ? Colors.orange[800]
                      : Colors.orange[500],
                  child: new Text(
                    _categorys[index],
                    style: new TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
        ),
      );
      

      }

      【讨论】:

      • 感谢您的评论。你的代码和我在问题中写的一样
      猜你喜欢
      • 2022-08-18
      • 2019-01-17
      • 2017-10-12
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2018-12-15
      相关资源
      最近更新 更多