搜索插件、代码仓与 APP 分类

prompts 封面
插件详情v2.0.0

prompts

Rich,简单,同步的命令行提示库,适用于 Dart。

AndroidiOSmacOSWindowsLinux
近 30 天下载
116万
喜欢数
29
Pub 分数
140

安装插件

flutter pub add prompts

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个功能丰富、简单且同步的命令行提示库,专为 Dart 设计,旨在让构建交互式 CLI 应用变得轻松。它提供了一个简洁的 API,支持通过多种提示类型(如文本、密码、确认和选择型输入)向用户获取输入。该库非常适合需要在终端环境中进行用户交互的工具、脚本或开发实用程序。Prompts 支持所有主要平台,包括 Android、iOS、macOS、Windows 和 Linux,具有高度可移植性。其同步特性简化了流程控制,无需使用 async/await 的样板代码。

适合场景

  • 构建 CLI 工具
  • 交互式设置向导
  • 脚本中的用户输入收集
  • 开发实用程序应用程序
  • 基于终端的配置

主要特点

  • 同步 API
  • 多种提示类型
  • 跨平台支持
  • 简单、直观的语法
  • 无需 async/await

适合对象

  • Dart 开发人员构建 CLI 应用
  • 终端中需要用户交互的工具
  • 简单的命令行界面

使用建议

  • 不适用于异步工作流
  • 仅限基于控制台的交互
  • 可能在输入期间阻止执行
  • 需要终端访问
  • 不适用于图形用户界面应用程序

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

prompts

Pub

Rich, simple, synchronous command-line prompt library for Dart.

Now with pretty colors!

GIF Demo

Example

import 'package:io/ansi.dart';
import 'package:prompts/prompts.dart' as prompts;

void main() {
  // Easily get a single line.
  var name = prompts.get('Enter your name');
  print('Hello, $name!');

  // ... Or many lines.
  print('Tell me about yourself.');
  var bio = prompts.get(
    "Enter some lines, using '\\' to escape line breaks",
    allowMultiline: true,
    inputColor: resetAll,
  );
  print('About $name:\n$bio');

  // Supports default values.
  name = prompts.get('Enter your REAL name', defaultsTo: name);
  print('Hello, $name!');

  // "High-level" prompts are built upon [get].
  // For example, we can prompt for confirmation trivially.
  bool shouldDownload = prompts.getBool('Really download this package?');

  if (!shouldDownload)
    print('Not downloading.');
  else
    print('Downloading...!');

  // Or, get an integer, WITH validation.
  int age = prompts.getInt('How old are you?', defaultsTo: 23, chevron: false);
  print('$name, you\'re $age? Cool!');

  // We can choose from various values.
  // There are two methods - shorthand and regular.
  var rgb = [Color.red, Color.green, Color.blue];
  Color color = prompts.chooseShorthand('Tell me your favorite color', rgb);
  print('You chose: ${color.about}');

  // The standard chooser prints to multiple lines, but is often
  // clearer to read, and has more obvious semantics.
  color = prompts.choose('Choose another color', rgb, defaultsTo: Color.blue);
  print(color.about);
}

class Color {
  final String name, description;

  const Color(this.name, this.description);

  static const Color red = const Color('Red', 'The color of apples.'),
      blue = const Color('Blue', 'The color of the sky.'),
      green = const Color('Green', 'The color of leaves.');

  String get about => '$name - $description';

  @override
  String toString() => name;
}

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目