我怎样才能使四舍五入的文本字段在 Flutter ?

fnatzsnv  于 2023-06-30  发布在  Flutter
关注(0)|答案(7)|浏览(118)

iOS的Material Design看起来不太好,尤其是TextField。有没有办法创造自己的?或者有没有办法给TextField添加一些样式,这样它看起来会更圆?

ruarlubt

ruarlubt1#

您可以在TextField的装饰参数中为其添加圆角

TextField(
  decoration: InputDecoration(
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10.0),
    ),
    filled: true,
    hintStyle: TextStyle(color: Colors.grey[800]),
    hintText: "Type in your text",
    fillColor: Colors.white70,
  ),
)
9rbhqvlz

9rbhqvlz2#

@Rockvole的答案是正确的,但如果你不喜欢TextField周围的默认边框,你可以通过覆盖OutlineInputBorderborderSide属性来实现它,如下所示:

TextField(
    textAlign: TextAlign.center,
    controller: searchCtrl,
    keyboardType: TextInputType.text,
    decoration: InputDecoration(
        hintText: 'Enter a product name eg. pension',
        hintStyle: TextStyle(fontSize: 16),
        border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide(
                width: 0, 
                style: BorderStyle.none,
            ),
        ),
        filled: true,
        contentPadding: EdgeInsets.all(16),
        fillColor: colorSearchBg,
    ),
),
ssgvzors

ssgvzors3#

您可以在Container的帮助下获得所需的输出。

new Container(
  child: new Text (
      "Text with rounded edges.",
      style: new TextStyle(
          color: Colors.blue[500],
          fontWeight: FontWeight.w900
      )
  ),
  decoration: new BoxDecoration (
      borderRadius: new BorderRadius.all(new Radius.circular(10.0)),
      color: Colors.black
  ),
  padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
),
ltqd579y

ltqd579y4#

你可以试试下面的代码:

decoration:  InputDecoration(
         border: OutlineInputBorder(
             borderRadius: const BorderRadius.all(
                const Radius.circular(10.0), 
             ),
             borderSide: BorderSide(
              width: 0, 
              style: BorderStyle.none,
              ),
             ),
           )
vnzz0bqm

vnzz0bqm5#

我已经从互联网上的几种方法和解决方案中收集了这个解决方案,它对我很有效,所以它可以帮助那里的人:此解决方案将控制TextField的宽度和高度以及其他属性

Container(
    child: Theme(
      data: Theme.of(context).copyWith(splashColor: Colors.transparent),
      child: TextField(
        autofocus: false,
        style: TextStyle(fontSize: 15.0, color: Color(0xFFbdc6cf)),
        decoration: InputDecoration(
          filled: true,
          fillColor: Colors.white,
          hintText: 'Search',
          contentPadding:
          const EdgeInsets.only(left: 14.0, bottom: 12.0, top: 0.0),

          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(25.7),
          ),

          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(25.7),
          ),
        ),
      ),
    ),

    decoration: new BoxDecoration (
      borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
      color: Colors.white   ),   width: 250,   height: 50,   margin: new EdgeInsets.fromLTRB(20, 20, 20, 210),   padding: new EdgeInsets.fromLTRB(8, 8, 8, 8),

),
nr7wwzry

nr7wwzry6#

你可以通过几种不同的方式来达到这个目的:
第一种方法:

Container(
      child: new Text (
          "Some Text",
          style: TextStyle(
              color: Colors.black
          )
      ),
      decoration: BoxDecoration (
          borderRadius: BorderRadius.all( Radius.circular(15)),
          color: Colors.white
      ),
      padding: EdgeInsets.all(16.0),
    ),

第二种方法:

TextField(
      decoration: InputDecoration(
          border: OutlineInputBorder(
            borderRadius: const BorderRadius.all(
              Radius.circular(15.0),
            ),
          ),
          filled: true,
          hintStyle: new TextStyle(color: Colors.grey[600]),
          hintText: "Hint text",
          fillColor: Colors.white),
    )

第三种方法:

TextField(
    textAlign: TextAlign.center,
    controller: someTextXontroller,
    keyboardType: TextInputType.text,
    decoration: InputDecoration(
        hintText: 'Hint Text',
        border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(12),
            borderSide: BorderSide(
                width: 0, 
                style: BorderStyle.none,
            ),
        ),
        filled: true,
        contentPadding: EdgeInsets.all(16),
        fillColor: Colors.blue,
    ),
),
jexiocij

jexiocij7#

TextFormField中尝试以下操作:

border: OutlineInputBorder(
     borderRadius: BorderRadius.all(
         Radius.circular(20.0),
     ),
),

相关问题