Flutter---两种带输入框的对话框

第一种风格

Cupertino方案

CuptinoAlertDialog+CupertinoTextField+CupertinoDialogAction

效果图

具体代码

Dart 复制代码
//创建文本编辑控制器
  //作用:管理TextField的文本内容,监听文本变化,获取和设置文本值,清空文本内容
  final _nameController = TextEditingController();


  //统一使用 Cupertino 风格
  // 带输入框的对话框
  void showChangeName() {
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: const Text("修改名字"),
          content: Card(
            color: Colors.transparent,
            elevation: 0,
            child: SizedBox(
              height: 45,
              child: CupertinoTextField( // 使用 Cupertino 输入框
                controller: _nameController,
                placeholder: "请输入要修改的名字",
                padding: const EdgeInsets.all(10),
              ),
            ),
          ),
          actions: [
            // 取消按钮
            CupertinoDialogAction(
              child: const Text("取消"),
              onPressed: () => Navigator.pop(context),
            ),
            // 确定按钮
            CupertinoDialogAction(
              isDefaultAction: true, // 强调样式
              child: const Text("确定"),
              onPressed: () {
                // 这里写具体的修改名字的逻辑
                print(_nameController.text);
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    );
  }

第二种风格

Material方案

AlterDialog+TextField+TextButton

效果图

具体代码

Dart 复制代码
//创建文本编辑控制器
  //作用:管理TextField的文本内容,监听文本变化,获取和设置文本值,清空文本内容
  final _nameController = TextEditingController();


  // 带输入框的对话框
  void showChangeName() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("修改名字"),
          content: TextField(
            controller: _nameController,
            decoration: const InputDecoration(
              hintText: "请输入要修改的名字",
              border: OutlineInputBorder(),
            ),
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text("取消"),
            ),
            TextButton(
              onPressed: () {
                print(_nameController.text);
                Navigator.pop(context);
              },
              child: const Text("确定"),
            ),
          ],
        );
      },
    );
  }
相关推荐
大龄秃头程序员12 小时前
一次 Flutter 动画 Demo 的整理与复盘
flutter
AlexMaybeBot15 小时前
躺在沙发上开发 Openclaw 的移动端APP
前端·flutter
天空之城--16 小时前
Android Flutter行业动态与学习参考(2026年8月)
android·flutter
FungLeo17 小时前
Flutter Web token 存储陷阱:crypto.subtle 在非安全上下文失效排查实录
前端·安全·flutter
FungLeo17 小时前
Flutter Web 中文字体困境与渲染器选择:CanvasKit vs HTML renderer 实战
前端·flutter·html
杉氧18 小时前
原生交互:如何在 Flutter 中嵌入 Android/iOS 原生组件并解决手势冲突
android·flutter·dart
恋猫de小郭19 小时前
Flutter iOS Deep Link 为什么会突然失效:一系列难以言喻的问题
android·前端·flutter
FungLeo1 天前
Flutter fl_chart 0.70 破坏性 API 迁移实录:duration/getTooltipColor/withValues 全解
flutter·fl_chart
FungLeo2 天前
Flutter 吸顶分组列表实战:语义桶分组 + 点击头平滑滚动
android·flutter
FungLeo2 天前
Flutter 超长 StatefulWidget 拆分术:part of + extension on State 实战
android·flutter·dart