当前位置:   article > 正文

使用Flutter创建带有图标提示的TextField

使用Flutter创建带有图标提示的TextField

移动应用开发中,TextField是一种常用的用户输入小部件。然而,有时向用户提供有关他们应该输入什么的提示或说明是很有帮助的。在本教程中,我们将创建一个Flutter应用程序,演示如何在TextField旁边包含一个图标提示。

编写代码

现在,让我们深入代码。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('TextField with Icon Hint'),
        ),
        body: const Center(
          child: MyTextField(),
        ),
      ),
    );
  }
}

class MyTextField extends StatefulWidget {
  const MyTextField({super.key});

  
  _MyTextFieldState createState() => _MyTextFieldState();
}

class _MyTextFieldState extends State<MyTextField> {
  final TextEditingController _controller = TextEditingController();
  bool _isHintVisible = true;

  
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 50,
      margin: const EdgeInsets.symmetric(horizontal: 50),
      decoration: BoxDecoration(
        color: Colors.grey.withOpacity(0.3),
        borderRadius: BorderRadius.circular(30),
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Visibility(
            visible: _isHintVisible,
            child: const Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(Icons.info),
                SizedBox(width: 5),
                Text('提示文本'),
              ],
            ),
          ),
          TextField(
            controller: _controller,
            textAlign: TextAlign.center,
            decoration: const InputDecoration(
              contentPadding: EdgeInsets.symmetric(horizontal: 25),
              border: InputBorder.none,
            ),
            onChanged: (value) {
              setState(() {
                _isHintVisible = value.isEmpty;
              });
            },
          ),
        ],
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

运行示例图:

在这里插入图片描述

代码解释

  • 我们创建了一个名为MyApp的StatelessWidget作为应用程序的入口。
  • MyApp类返回一个MaterialApp,其中包含一个Scaffold,其中包含一个标题栏和一个居中的MyTextField小部件。
  • MyTextField是一个StatefulWidget,它返回一个包含TextField和提示文本的容器。
  • _MyTextFieldState类是MyTextField的状态类,它包含一个TextEditingController来控制TextField的文本,以及一个布尔值_isHintVisible,用于控制提示文本的可见性。
  • 在MyTextField的build方法中,我们使用Stack和Visibility小部件来叠加TextField和提示文本,根据TextField中的文本是否为空来控制提示文本的可见性。

结论

通过本教程,您学会了如何在Flutter应用程序中创建一个带有图标提示的TextField。您可以根据自己的需要进一步定制该TextField,以使其符合您的应用程序设计和用户体验需求。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/402102
推荐阅读
相关标签
  

闽ICP备14008679号