Flutter动画提示插件animated_hint的使用

Flutter动画提示插件animated_hint的使用

License Version

Screen-Recording-2024-10-03-at-5 26 49 AM

目录

描述

Animated Hint 包允许你轻松地为TextView添加动画提示。此包通过为输入字段添加动画提示来增强用户体验,使您的文本输入字段更具交互性和视觉吸引力。

开始使用

安装

在你的Flutter项目中添加该包,使用以下命令:

flutter pub add animated_hint

使用

要使用动画提示功能,只需将传统的TextView替换为AnimatedHintTextField小部件。

import 'package:flutter/material.dart';
import 'package:animated_hint/animated_hint.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Animated Hint TextField Example')),
        body: Center(
          child: AnimatedHintTextField(
            fixedHint: "Search",
            hints: const ['\"Flights\"', '\"Accounts\"'],
            animationType: AnimationType.slide,
          ),
        ),
      ),
    );
  }
}

API参考

AnimatedHintTextField

属性

  • fixedHint: 此部分提示将保持固定且不会进行动画。
  • hints: 要进行动画的提示列表。
  • animationType: 用于提示的动画类型,默认使用 [AnimationType.slide]
  • fixedHintTextStyle: 固定提示文本样式,如果为空,则使用默认样式。
  • hintTextStyle: 动画提示文本样式,如果为空,则使用默认样式。

贡献

我们欢迎贡献!如果你有任何想法或改进,请创建一个issue或提交一个pull request。

贡献步骤

  1. Fork仓库。
  2. 创建一个新的分支 (git checkout -b feature-branch)。
  3. 进行更改并提交 (git commit -m 'Add new feature')。
  4. 将更改推送到分支 (git push origin feature-branch)。
  5. 打开一个pull request。

许可证

该项目在MIT许可证下发布,请参阅LICENSE文件以获取详细信息。

联系

如果有任何问题或需要支持,请在GitHub上创建一个issue。


示例代码

import 'package:animated_hint/animated_hint.dart';
import 'package:flutter/material.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  AnimationType _animationType = AnimationType.slide;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                AnimatedHintTextField(
                  fixedHint: "Search",
                  hints: const ['\"Flights\"', '\"Accounts\"'],
                  animationType: _animationType,
                  onTapOutside: (_) {
                    FocusScope.of(context).unfocus();
                  },
                ),
                const SizedBox(height: 20),
                DropdownMenu<AnimationType>(
                  initialSelection: _animationType,
                  dropdownMenuEntries: AnimationType.values
                      .map(
                        (item) => DropdownMenuEntry(
                          label: item.name,
                          value: item,
                        ),
                      )
                      .toList(),
                  onSelected: (value) {
                    if (value != null) {
                      setState(() {
                        _animationType = value;
                      });
                    }
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter动画提示插件animated_hint的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动画提示插件animated_hint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


animated_hint 是一个 Flutter 插件,用于在文本字段中显示动画提示。它可以帮助你在用户输入时提供动态的提示信息,增强用户体验。以下是如何使用 animated_hint 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 animated_hint 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  animated_hint: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 以获取依赖。

2. 导入包

在你的 Dart 文件中导入 animated_hint 包:

import 'package:animated_hint/animated_hint.dart';

3. 使用 AnimatedHintTextField

AnimatedHintTextFieldanimated_hint 提供的一个小部件,它可以在用户输入时显示动画提示。

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animated Hint Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            AnimatedHintTextField(
              hintText: 'Enter your name',
              hintStyle: TextStyle(color: Colors.grey),
              textStyle: TextStyle(color: Colors.black),
              onChanged: (value) {
                print('User input: $value');
              },
            ),
            SizedBox(height: 20),
            AnimatedHintTextField(
              hintText: 'Enter your email',
              hintStyle: TextStyle(color: Colors.grey),
              textStyle: TextStyle(color: Colors.black),
              onChanged: (value) {
                print('User input: $value');
              },
            ),
          ],
        ),
      ),
    );
  }
}

4. 自定义动画提示

你可以通过设置 hintStyletextStyle 来自定义提示文本和输入文本的样式。此外,onChanged 回调可以用于监听用户输入的变化。

5. 运行应用

现在你可以运行你的 Flutter 应用,并查看 AnimatedHintTextField 的效果。当用户点击输入框时,提示文本将以动画的形式移动到输入框的上方。

6. 其他配置

AnimatedHintTextField 还提供了其他一些配置选项,例如 controllerdecorationkeyboardType 等,你可以根据需要进行配置。

AnimatedHintTextField(
  controller: _controller,
  hintText: 'Enter your password',
  hintStyle: TextStyle(color: Colors.grey),
  textStyle: TextStyle(color: Colors.black),
  obscureText: true,
  onChanged: (value) {
    print('User input: $value');
  },
);

7. 处理输入

你可以使用 controller 来获取或设置输入框中的文本:

TextEditingController _controller = TextEditingController();

[@override](/user/override)
void dispose() {
  _controller.dispose();
  super.dispose();
}

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Animated Hint Example'),
    ),
    body: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          AnimatedHintTextField(
            controller: _controller,
            hintText: 'Enter your name',
            hintStyle: TextStyle(color: Colors.grey),
            textStyle: TextStyle(color: Colors.black),
            onChanged: (value) {
              print('User input: $value');
            },
          ),
          ElevatedButton(
            onPressed: () {
              print('Current input: ${_controller.text}');
            },
            child: Text('Submit'),
          ),
        ],
      ),
    ),
  );
}
回到顶部