组件

如果你不用get,可以注释掉,吧get.back 换成 Navigator.pop(context) 即可;

//组件文件
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class InputDialog extends StatefulWidget {
  const InputDialog({Key? key, this.hintText = "请输入...", this.title}) : super(key: key);

  final Widget? title; // Text('New nickname'.tr)
  final String? hintText;

  @override
  State<InputDialog> createState() => _InputDialogState(title: this.title, hintText: this.hintText);
}

class _InputDialogState extends State<InputDialog> {
  final TextEditingController _textEditingController = TextEditingController();

  final Widget? title; // Text('New nickname'.tr)
  final String? hintText;
  _InputDialogState({required this.title, required this.hintText});


  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: title,
      content: TextField(
          controller: _textEditingController,
          maxLength:12,
          decoration: InputDecoration(hintText: hintText),
          autofocus: true
      ),
      actions: [
        ElevatedButton(
          style:ButtonStyle(backgroundColor:MaterialStateProperty.all(Colors.green)),
          onPressed: () {
            Get.back(result: _textEditingController.text);
          },
          child: Text('ok'.tr,style: TextStyle(color: Colors.white)),
        ),
        ElevatedButton(
          style:ButtonStyle(backgroundColor:MaterialStateProperty.all(Colors.transparent),elevation:MaterialStateProperty.all(0)),
          onPressed: () {
            Get.back();
          },
          child: Text('cancel'.tr,style: TextStyle(color: Colors.grey)),
        ),
      ],
    );
  }
}

使用

//order code 

IconButton(
          icon: Icon(
            Icons.settings_input_antenna_outlined,
          ),
          onPressed: () => {
             _showInputDialog(context).then((value) => {
               Logger().i(value)
             })
          },
        ),

//order code 



Future<String> _showInputDialog(BuildContext context) async {
  String inputText = await showDialog(
    context: context,
    builder: (BuildContext context) => InputDialog(title: Text("New nickname".tr)),
  );
  return inputText;
}

tip : 这个示例我还用到了i18n,如果你没使用到,把所有的  .tr  删掉即可

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐