首页 > 解决方案 > 值通知器#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(去健身房)..我只想要来自用户的纯字符串。请帮我解决这个问题......我很感激它提前。

标签: flutterdart

解决方案


您正在使用ValueNotifier. 在您的情况下使用 ValueNotifier.value newTaskTitle.value获取 String 值。 代码:

...
...
Provider.of<TaskData>(context, listen: false)
                              .addTask(newTaskTitle.value);

...


推荐阅读