我想獲得用戶在文本欄位中輸入的任何內容的輸出,我參考了flutter dev的官方檔案。我無法提出我錯的地方,如果你們能提供幫助,我會很高興。我希望使用控制器將文本欄位輸入作為輸出,但 alertdialog 不起作用。
這是我的代碼:
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Employee List')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: myController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Employee Name',
),
),
),
ElevatedButton(
// When the user presses the button, show an alert dialog containing
// the text that the user has entered into the text field.
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
// Retrieve the text that the user has entered by using the
// TextEditingController.
content: Text(myController.text),
);
},
);
},
child: const Icon(Icons.text_fields),
),
],
)),
);
}
}
uj5u.com熱心網友回復:
您可以創建另一個小部件(新的)或移動MaterialApp到頂層。
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Employee List')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: myController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Employee Name',
),
),
),
ElevatedButton(
// When the user presses the button, show an alert dialog containing
// the text that the user has entered into the text field.
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
// Retrieve the text that the user has entered by using the
// TextEditingController.
content: Text(myController.text),
);
},
);
},
child: const Icon(Icons.text_fields),
),
],
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/509927.html
下一篇:用彩色圓角框包裹一行文本
