Flutter 輸入框彈窗

先看效果圖

效果圖

代碼

復(fù)制后 貼在 main.dart 可直接操作
如果有使用 Get 可以看看注釋掉的代碼

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

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Material App',
      home: Home(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Material App Bar'),
      ),
      body: Column(
        children: [
          TextButton(
            onPressed: () async {
              var text = await SendCommentPage.show2<String>(context);
              print(text);
            },
            child: const Text('彈窗'),
          )
        ],
      ),
    );
  }
}

class SendCommentPage extends StatelessWidget {
  // 如果使用 Get
  // static Future<T?>? show<T>() {
  //   return Get.to(() => SendCommentPage(),
  //       opaque: false,
  //       preventDuplicates: false,
  //       duration: const Duration(microseconds: 0),
  //       fullscreenDialog: true);
  // }

  static Future<T?> show2<T>(BuildContext context) {
    return Navigator.of(context).push(
      PageRouteBuilder(
        // 關(guān)鍵
        opaque: false,
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
          return SendCommentPage();
        },
      ),
    );
  }

  SendCommentPage({super.key});

  final focusNode = FocusNode();
  final controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // 加個(gè)小延遲
    Timer(const Duration(milliseconds: 50), (() {
      focusNode.requestFocus();
    }));

    return Scaffold(
        // 關(guān)鍵
        backgroundColor: Colors.black.withAlpha((255 * 0.4).toInt()),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            //撐起上部分
            Expanded(child: GestureDetector(
              onTap: () {
                Navigator.pop(context);
                // Get.back();
              },
            )),
            Container(
                padding: const EdgeInsets.all(8),
                color: Colors.white,
                child: TextField(
                  controller: controller,
                  cursorColor: Colors.black,
                  decoration: const InputDecoration(
                    hintText: "說(shuō)點(diǎn)什么吧~",
                  ),
                  focusNode: focusNode,
                  minLines: 3,
                  maxLines: 6,
                )),
            Container(
                color: Colors.white,
                padding: const EdgeInsets.fromLTRB(40, 10, 40, 8),
                width: double.infinity,
                child: TextButton(
                    onPressed: () {
                      Navigator.pop(context, controller.text);
                      // Get.back(result: controller.text);
                    },
                    child: const Text("發(fā)送, 看 log"))),
          ],
        ));
  }
}

Flutter 輸入彈窗
Flutter 鍵盤(pán)彈窗
Flutter 彈窗

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容