flutter - 如何将文本字段的标签转移到前缀图标的顶部而不是文本

标签 flutter dart material-design textfield flutter-animation

在 flutter 的 textField 中使用 prefixIcon 和 label 时,在焦点模式下 label 的动画转移到文本的顶部(而不是图标的顶部)。 我不想使用 prefix 而不是 prefixIcon,因为当 textField 未处于焦点模式时 prefix 会隐藏。我需要我的前缀始终可见。 喜欢这些来自 material designs text fields part 的图片: enter image description here 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> {
  FocusNode _focusNode = FocusNode();
  bool textEditHasFocus = false;
  @override
  void initState() {
    super.initState();
    _focusNode.addListener(() {
      setState(() {
        textEditHasFocus = _focusNode.hasFocus;
      });
    });
  }

  @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 Container(
      decoration: BoxDecoration(color: Colors.white),
      padding: new EdgeInsets.symmetric(
        vertical: 40.0,
        horizontal: 20,
      ),
      child: Stack(
        clipBehavior: Clip.none,
        children: <Widget>[
          Container(
            child: TextField(
              focusNode: _focusNode,
              decoration: InputDecoration(
                // hintText: textEditHasFocus ? '' : 'Label',
                // hintStyle: TextStyle(
                //   color: Colors.grey,
                // ),
                border: OutlineInputBorder(),
                focusedBorder: OutlineInputBorder(),
                contentPadding: EdgeInsets.only(left: 40),
              ),
            ),
          ),
          AnimatedPositioned(
            duration: Duration(milliseconds: 200),
            left: textEditHasFocus ? 10 : 40,
            top: textEditHasFocus ? -10 : 13,
            child: InkWell(
              onTap: () {
                _focusNode.requestFocus();
              },
              child: Container(
                padding: EdgeInsets.only(left: 3),
                color: Colors.white,
                child: Text('Label'),
              ),
            ),
          ),
          Positioned.fill(
            child: Align(
              alignment: Alignment.centerLeft,
              child: Container(
                padding: EdgeInsets.only(left: 10),
                color: Colors.transparent,
                child: Icon(Icons.favorite),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

关于flutter - 如何将文本字段的标签转移到前缀图标的顶部而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70332783/

相关文章:

flutter - 在提供者初始化时调用将来的方法

flutter - 是否可以将平台 View 从主机 iOS 应用程序添加到 flutter 模块 View 而不转换为插件?

html - 如何在 Angular Material Design 中使 md 内容可滚动

带帐户选择器的 Android 抽屉导航

flutter - 尝试从 dart mixin 调用类构造函数时出现错误 T 不是函数

flutter :- Keyboard causes layout to overflow on the bottom when on focus

unit-testing - Dart 测试大致相等

android - 如何在 flutter 中扩展卡片?

android - 如何创建指向苹果商店和Playstore的链接?

flutter - Auto_route无法使用生成的文件:名称为 'reportsRouteUpdateToEngine'的无名称参数