【发布时间】:2021-06-17 17:46:22
【问题描述】:
大家好:问题很简单,但我就是找不到解决办法
这是我的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoye_app_angela/models/task_data.dart';
class AddTaskScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
ValueNotifier<String> newTaskTitle = ValueNotifier("");
return Container(
color: Color(0xFF757575),
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.limeAccent[400],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(60),
topRight: Radius.circular(60),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30, color: Colors.brown[900]),
),
SizedBox(
height: 12,
),
TextField(
onChanged: (newText) {
newTaskTitle.value = newText;
},
autocorrect: false,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.brown[800]!,
width: 2,
),
borderRadius: BorderRadius.circular(30),
),
hintText: 'Type Your Task ...',
labelStyle: TextStyle(
color: Colors.green[900],
),
helperStyle: TextStyle(
color: Colors.brown[900],
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(60),
borderSide: BorderSide(
color: Colors.brown[900]!,
width: 2,
),
),
),
),
SizedBox(
height: 12,
),
ValueListenableBuilder<String>(
valueListenable: newTaskTitle,
builder: (context, taskt, child) {
return ElevatedButton(
onPressed: taskt.isEmpty
? null
: () {
Provider.of<TaskData>(context, listen: false)
.addTask(newTaskTitle.toString());
print(taskt);
Navigator.pop(context);
// Provider.of<TaskData>(context, listen: false)
// .addTask(newTaskTitle);
// Navigator.pop(context);
},
child: Text(
'Add',
style: TextStyle(color: Colors.brown[900]),
),
style: ButtonStyle(
backgroundColor: MaterialStateColor.resolveWith(
(states) => Colors.lightGreen),
elevation: MaterialStateProperty.resolveWith((states) => 6),
shadowColor: MaterialStateColor.resolveWith(
(states) => Colors.green),
minimumSize: MaterialStateProperty.resolveWith(
(states) => Size.square(40.67)),
),
);
},
),
],
),
),
);
}
}
我正在尝试在我的待办事项应用程序中添加一些任务。用户只需在文本字段中输入一些内容,然后将此值传递给我的 taskdata 类中的 addtask 函数,以通知侦听器。问题是 newTaskTitle 变量的类型是 ValueNotifier 并且我的输入函数的值是一个字符串我试图通过在此处添加 toString 方法来解决这个问题:
Provider.of<TaskData>(context, listen: false)
.addTask(newTaskTitle.toString());
但它在屏幕上返回:ValueNotifier#97fe7(去健身房)..我只想要来自用户的纯字符串。 请帮我解决这个问题... 我很感激它提前。
【问题讨论】: