颤音:如何使前缀以标记为中心

发布于 01-23 21:49 字数 1180 浏览 4 评论 0原文

如何制作prefixiconlabelText为中心。

    return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            color: Colors.red,
            child: RichText(
              textAlign: TextAlign.justify,
              text: TextSpan(
                children: <InlineSpan>[
                  TextSpan(
                    text: "+49 ",
                    style: textMontserrat16SpNormal(
                        ColorPalettes.grey9, FontWeight.w500),
                  ),
                  WidgetSpan(
                    alignment: ui.PlaceholderAlignment.middle,
                    child: SizedBox(
                      child: SvgPicture.asset(ImageAssets.icArrowDown),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

How to make prefixIcon centered with labelText.

    return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            color: Colors.red,
            child: RichText(
              textAlign: TextAlign.justify,
              text: TextSpan(
                children: <InlineSpan>[
                  TextSpan(
                    text: "+49 ",
                    style: textMontserrat16SpNormal(
                        ColorPalettes.grey9, FontWeight.w500),
                  ),
                  WidgetSpan(
                    alignment: ui.PlaceholderAlignment.middle,
                    child: SizedBox(
                      child: SvgPicture.asset(ImageAssets.icArrowDown),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2025-01-30 21:49:04

我试图用“ Row”小部件包装“ RichText”小部件。
由于资源泄漏,我更改了一些代码。

”在此处输入图像描述“

=“ https://i.sstatic.net/6zj52.png” alt =“在此处输入图像说明”>

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            color: Colors.red,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                RichText(
                  textAlign: TextAlign.justify,
                  text: TextSpan(
                    children: <InlineSpan>[
                      TextSpan(
                        text: "+49 ",
                      ),
                      WidgetSpan(
                        alignment: PlaceholderAlignment.middle,
                        child: SizedBox(
                          child: Icon(Icons.favorite),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

I tried to wrap 'RichText' widget by 'Row' widget.
I changed some code because of leak of resources.

enter image description here

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            color: Colors.red,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                RichText(
                  textAlign: TextAlign.justify,
                  text: TextSpan(
                    children: <InlineSpan>[
                      TextSpan(
                        text: "+49 ",
                      ),
                      WidgetSpan(
                        alignment: PlaceholderAlignment.middle,
                        child: SizedBox(
                          child: Icon(Icons.favorite),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

风追烟花雨 2025-01-30 21:49:04

尝试以下代码:如果您想要在该文本场中的phone_number,则将引用 intl_phone_phone_phone_field_field a> packages

   TextField(
          decoration: InputDecoration(
             labelText: "Test",
            prefixIcon: GestureDetector(
              onTap: () {},
              child: Container(
                  color: Colors.red,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text('+'),
                      Text('42'),
                      Icon(
                        Icons.arrow_downward,
                      ),
                    ],
                  )),
            ),
          ),
        ),

result-&gt;

Try below code: If you want phone_number in that textfield then you will refer intl_phone_field and intl_phone_number_input packages

   TextField(
          decoration: InputDecoration(
             labelText: "Test",
            prefixIcon: GestureDetector(
              onTap: () {},
              child: Container(
                  color: Colors.red,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text('+'),
                      Text('42'),
                      Icon(
                        Icons.arrow_downward,
                      ),
                    ],
                  )),
            ),
          ),
        ),

Result-> image

空名 2025-01-30 21:49:04

在容器上使用对齐:

return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            alignment:Alignment.center,
            color: Colors.red,
            child: RichText(
              textAlign: TextAlign.justify,
              text: TextSpan(
                children: <InlineSpan>[
                  TextSpan(
                    text: "+49 ",
                    style: textMontserrat16SpNormal(
                        ColorPalettes.grey9, FontWeight.w500),
                  ),
                  WidgetSpan(
                    alignment: ui.PlaceholderAlignment.middle,
                    child: SizedBox(
                      child: SvgPicture.asset(ImageAssets.icArrowDown),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

Use Alignment at Container:

return TextField(
      decoration: InputDecoration(
        labelText: "Test",
        prefixIcon: GestureDetector(
          onTap: () {},
          child: Container(
            alignment:Alignment.center,
            color: Colors.red,
            child: RichText(
              textAlign: TextAlign.justify,
              text: TextSpan(
                children: <InlineSpan>[
                  TextSpan(
                    text: "+49 ",
                    style: textMontserrat16SpNormal(
                        ColorPalettes.grey9, FontWeight.w500),
                  ),
                  WidgetSpan(
                    alignment: ui.PlaceholderAlignment.middle,
                    child: SizedBox(
                      child: SvgPicture.asset(ImageAssets.icArrowDown),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文