flutter - 值通知器#97fe7(去健身房)
问题描述
大家好:问题很简单,但我找不到解决方案
这是我的代码:
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)),
),
);
},
),
],
),
),
);
}
}
我试图在我的 todo 应用程序中添加一些任务。用户只需在文本字段中输入一些内容,然后将此值传递给我的 taskdata 类中的 addtask 函数,以通知侦听器。问题是 newTaskTitle 变量是 ValueNotifier 类型,而我的输入函数的值是一个字符串,我试图通过在此处添加 toString 方法来解决这个问题:
Provider.of<TaskData>(context, listen: false)
.addTask(newTaskTitle.toString());
但它返回:屏幕上的ValueNotifier#97fe7(去健身房)..我只想要来自用户的纯字符串。请帮我解决这个问题......我很感激它提前。
解决方案
您正在使用ValueNotifier
.
在您的情况下使用 ValueNotifier.value 来newTaskTitle.value
获取 String 值。
代码:
...
...
Provider.of<TaskData>(context, listen: false)
.addTask(newTaskTitle.value);
...
推荐阅读
- oracle - 使用 Oracle regexp_replace 捕获多个字符串或单词
- php - 由于新的 php 版本,我需要更新 php 文件吗?
- java - Hibernate 5.4.25 二级缓存和扩展 SingletonEhcacheRegionFactory 类
- python - 如何使用统计模型运行序数回归?
- ios - 如何在 iOS 应用程序的 settings.bundle 中创建检查选项
- elasticsearch - 如何从 ElasticSearch 的源中仅获取内部字段?
- python - 删除与python一起使用的文件时出错
- mysql - 识别别名列字段中一组不同数据的最后一行
- python - 为什么我的变量被函数覆盖,即使它是间接引用的?
- javascript - 如何在打字稿中按字母顺序排列字符串