ios 如何将图标放在textFormField/ textfield的左上角?

crcmnpdw  于 2022-11-26  发布在  iOS
关注(0)|答案(1)|浏览(151)

我有一个文本字段,但问题是图标仍然在中间,而文本字段是展开的。我怎样才能使图标去在左边的网站提示文本(左上)?

TextFormField reusableTextField2(String text, IconData icon,TextEditingController controller) {
  return TextFormField(

    controller: controller,
    cursorColor: Colors.white,
    style: TextStyle(color: Colors.white.withOpacity(0.9)),

    decoration: InputDecoration(
      
      prefixIcon: Icon(

        icon,
        color: Colors.white70,

      ),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(30.0),
          borderSide: const BorderSide(width: 0, style: BorderStyle.none)),
      contentPadding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),

      labelText: text,
      labelStyle: TextStyle(color: Colors.white.withOpacity(0.9)),
      filled: true,
      floatingLabelBehavior: FloatingLabelBehavior.never,
      fillColor: Colors.white.withOpacity(0.3),
      alignLabelWithHint: true,
    ),
    maxLines: null,
    minLines: 7,
  );
}

这是解决方案的外观

p8h8hvxi

p8h8hvxi1#

你可以用Stack小部件 Package 它,然后单独设置图标,但它会像TextFormField小部件一样垂直对齐,给予它一个垂直的padding,就像TextFormFieldcontentPadding一样。

Widget reusableTextField2(
    String text, IconData icon, TextEditingController controller) {
  final verticalPadding = EdgeInsets.symmetric(vertical: 20);
  return Stack(
    children: <Widget>[
      TextFormField(
        controller: controller,
        cursorColor: Colors.white,
        style: TextStyle(color: Colors.white.withOpacity(0.9)),
        decoration: InputDecoration(
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(30.0),
              borderSide: const BorderSide(width: 0, style: BorderStyle.none)),
          contentPadding:
              verticalPadding + EdgeInsets.symmetric(horizontal: 20),
          labelText: text,
          labelStyle: TextStyle(color: Colors.white.withOpacity(0.9)),
          filled: true,
          floatingLabelBehavior: FloatingLabelBehavior.never,
          fillColor: Colors.black.withOpacity(0.3),
          alignLabelWithHint: true,
        ),
        maxLines: null,
        minLines: 7,
      ),
      Container(
        padding: verticalPadding,
        child: Icon(icon),
      ),
    ],
  );
}

相关问题