我有一個模態底片,它有一個文本欄位和一個按鈕:
我有一個模態底片,它有一個文本欄位和一個按鈕。
import 'package:flutter/material.dart'。
class AddTaskScreen extends StatelessWidget{
Function addTaskCallback;
String newTaskTitle = "";
AddTaskScreen(this.addTaskCallback)。
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff7575)。
孩子。容器(
padding: EdgeInsets.all(30)。
孩子。列(
crossAxisAlignment: CrossAxisAlignment.stretch,
兒童。[
文本(
"Add Task"。
風格。TextStyle(
color: Colors.blueAccent,
字體大小。20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
文本欄位(
autofocus: true。
onChanged: (newText) {
newTaskTitle = newText。
},
),
SizedBox(
高度。30。
),
RaisedButton(
onPressed: (){
addTaskCallback(newTaskTitle)。
},
color: Colors.blueAccent,
孩子。文本(
"Add"。
style: TextStyle(
color: Colors.white
),
),
)
],
),
裝飾。BoxDecoration(
color: Colors.white,
borderRadius: borderRadius.only(
topRight: Radius.circular(20)。
topLeft: Radius.circular(20)
),
),
),
);
}
}
我是這樣使用回呼函式的:
floatingActionButton: FloatingActionButton(
onPressed: (){
showModalBottomSheet(
builder: (context) => AddTaskScreen(
(newTaskTitle) {
print(newTaskTitle)。
}
),
語境:語境
);
},
孩子。圖示(
Icons.add,
color: Colors.white,
),
)
但是每當我列印從模版底頁中獲得的文本時,它總是為空。
怎樣做才能使文本不為空呢?
我甚至將變數newTaskTitle分配給了textfield的onChanged屬性,它在本地并不是空的,但是當我在其他類中使用這個值時,它就是空的。
uj5u.com熱心網友回復:
你使用了一個無狀態的widget,這就是為什么當你改變值的時候,它沒有更新值。試著用TextEditingController。
import 'package:flutter/material.dart';
class AddTaskScreen extends StatelessWidget{
Function addTaskCallback;
String newTaskTitle = "";
TextEditingController _controller = TextEditingController()。
AddTaskScreen(this.addTaskCallback)。
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff7575)。
孩子。容器(
padding: EdgeInsets.all(30)。
孩子。列(
crossAxisAlignment: CrossAxisAlignment.stretch,
兒童。[
文本(
"Add Task"。
風格。TextStyle(
color: Colors.blueAccent,
字體大小。20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
文本欄位(
控制器。_controller,
autofocus: true。
onChanged: (newText) {
newTaskTitle = newText。
},
),
SizedBox(
高度。30。
),
RaisedButton(
onPressed: (){
addTaskCallback(_controller.text); //添加textController值。
},
color: Colors.blueAccent,
孩子。文本(
"Add",
style: TextStyle(
color: Colors.white
),
),
)
],
),
裝飾。BoxDecoration(
color: Colors.white,
borderRadius: borderRadius.only(
topRight: Radius.circular(20)。
topLeft: Radius.circular(20)
),
),
),
);
}
}
uj5u.com熱心網友回復:
/declare new function callback with text as parameter
typedef OnTextChanged = Function(String newText) 。
class AddTaskScreen extends StatelessWidget{
//在此使用該自定義函式。
OnTextChanged addTaskCallback;
String newTaskTitle = ""/span>;
AddTaskScreen(@required this.addTaskCallback)。
@override @override
Widget build(BuildContext context) {
return Container(
color: Color(0xff7575)。
孩子。容器(
padding: EdgeInsets.all(30)。
孩子。列(
crossAxisAlignment: CrossAxisAlignment.stretch,
兒童。[
文本(
"Add Task"。
風格。TextStyle(
color: Colors.blueAccent,
字體大小。20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
文本欄位(
autofocus: true。
onChanged: (newText) {
newTaskTitle = newText。
},
),
SizedBox(
高度。30。
),
RaisedButton(
onPressed: (){
//看這里,我們需要呼叫那個函式。
addTaskCallback.call(newTaskTitle)。
},
color: Colors.blueAccent,
孩子。文本(
"Add"。
style: TextStyle(
color: Colors.white
),
),
)
],
),
裝飾。BoxDecoration(
color: Colors.white,
borderRadius: borderRadius.only(
topRight: Radius.circular(20)。
topLeft: Radius.circular(20)
),
),
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/309236.html
標籤:

