1

我正在尝试创建一个带有前缀文本的 textformfield,如下图所示。这是在输入任何文本之前

文本表单字段 1

这是在输入文本之后

2

这是我写的代码

                TextFormField(
                  decoration: InputDecoration(
                    prefixIcon: Text(
                      '+254',
                      textAlign: TextAlign.center,
                      style: theme.textTheme.bodyText2!.copyWith(
                          color: Color(0xff000000),
                          fontWeight: FontWeight.w500),
                    ),
                    hintText: getTranslated(context, "label_hint")!,
                    hintStyle: theme.textTheme.bodyText2!.copyWith(
                      color: primaryLight.withOpacity(0.75),
                      fontWeight: FontWeight.w400,
                    ),
                    fillColor: const Color(0xffF5F5F5),
                    enabledBorder: OutlineInputBorder(
                      borderSide:
                          const BorderSide(width: 1, color: Color(0xffCCCCCC)),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: const BorderSide(width: 1, color: Color(0xffF38E30)),
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ),

这就是我的尝试的样子 3

我想实现接近前两个图像的东西,我可以在我的代码中改变什么

4

2 回答 2

0

尝试prefixIcon: Text('+254',用一列包裹文本小部件,并为其主轴对齐提供一个中心。或者用一个小部件包装你的文本小Center部件。

于 2022-03-01T15:39:26.030 回答
0

将小部件包装TextRow小部件中并将其mainAxisSize属性分配给MainAxisSize.minmainAxisAlignment属性mainAxisAlignment.center以实现带有提示文本的 prefixText 内联

试试这个代码 -

   TextFormField(
      decoration: InputDecoration(
        prefixIcon: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              '+254',
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.w400,
              ),
            ),
          ],
        ),
        hintText: 'mobile number',
        hintStyle: const TextStyle(
          color: Colors.grey,
          fontWeight: FontWeight.w400,
        ),
        fillColor: const Color(0xffF5F5F5),
        enabledBorder: OutlineInputBorder(
          borderSide:
              const BorderSide(width: 1, color: Color(0xffCCCCCC)),
          borderRadius: BorderRadius.circular(5),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide:
              const BorderSide(width: 1, color: Color(0xffF38E30)),
          borderRadius: BorderRadius.circular(5),
        ),
      ),
    ),
于 2022-03-01T20:25:05.317 回答