Flutter:如何使 prefixIcon 以 labelText 为中心

Flutter: How to make prefixIcon centered with labelText

如何使 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),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

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

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),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

试试下面的代码:如果你想在那个文本字段中 phone_number 那么你将参考 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,
                      ),
                    ],
                  )),
            ),
          ),
        ),

结果->

在容器处使用对齐方式:

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),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );