【问题标题】:Flutter add button without margin颤振添加按钮无边距
【发布时间】:2019-05-13 15:48:56
【问题描述】:

我正在尝试实现一个没有边距的按钮。

我的代码是:

  @override
  Widget build(BuildContext context) {
    return new AppState(
      child: new Scaffold(

          appBar: AppBar(
            backgroundColor: Color(0xFF031e39),
            title: Text("MY APP"),
          ),
          body:

          ButtonTheme(
            buttonColor: Color(0xFF031e39),
            minWidth: double.infinity,
            child: FlatButton(
              color: Color(0xFF81A483),
              onPressed: () {
                launchSearch();
              },
              child: Text('Search',style: TextStyle(color: Colors.white),),
            ),
          )
      ),
    );
  }

结果是:

我尝试了所有不同的方法,但我无法找到解决方案,因此按钮没有边距。

如果我将一个小部件放在我的按钮顶部的列中,我会得到相同的结果:

我怎样才能有一个没有任何边距的平面按钮?

【问题讨论】:

标签: android flutter


【解决方案1】:

使用: ma​​terialTapTargetSize:MaterialTapTargetSize.shrinkWrap,

FlatButton(
    textColor: GFColors.WHITE,
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        child: Text(
            "BLOG",
            style: TextStyle( 
            fontSize: 12.0, 
            fontWeight: FontWeight.normal 
        ),
    ),
    onPressed: () {
    },
),

【讨论】:

    【解决方案2】:

    我知道了,但做了一些修改。 我没有使用ButtonThemeFlatButton,而是使用ContainerFloatingActionButton

    使用Container,您可以设置屏幕大小。使用FloatingActionButton,您可以在Scaffold 中设置按钮的位置,在这种情况下,它位于整个屏幕中。 为了使按钮变平,我将属性 elevation 设置为 0.0,所以按钮看起来很平。

    appBar: AppBar(
          backgroundColor: Color(0xFF031e39),
          title: Text("MY APP"),
        ),
        body: new Container(
          width: double.infinity,
          child: FloatingActionButton(
            backgroundColor: Color(0xFF81A483),
            shape: new RoundedRectangleBorder(),
            elevation: 0.0,
            onPressed: () {
              print("entra");
            },
            child: Text(
              'Search',
              style: TextStyle(color: Colors.white),
            ),
          ),
        )
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      根据source。看起来 Flutter 填充的按钮小于目标抽头尺寸(48 x 48),您可以通过以下方式绕过它:

      1. 让你的按钮高度大于等于48

      1. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, 添加到您的FlatButton

      【讨论】:

      • 它为我节省了几个小时。
      • 它仍然留下一个很小的填充。另一种解决方法是使用 RichText 和 TapGestureRecognizer() 而不是 FlatButton。点击可能不方便,但它可能是适合您设计的唯一方式。
      猜你喜欢
      • 2019-02-17
      • 2020-10-13
      • 2022-01-23
      • 2017-05-09
      • 2020-05-26
      • 1970-01-01
      • 2012-08-24
      • 2021-10-12
      • 2019-09-19
      相关资源
      最近更新 更多