先看效果圖
代碼
復(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: "說點(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 鍵盤彈窗
Flutter 彈窗