我正在开发一个应用程序与Flutter 动和Flutter 动_河脚.我想使用带有Riverpod的Listview.但我不知道怎么用.我有一个文本字段的onSubmit方法.此方法用于处理一些文本文本的DART代码.我的目的是将这些话保存到Listview中.我为这个目的使用了fltter_riverpod,但我不知道如何使用状态提供程序或任何其他提供程序的列表.

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter/material.dart';


void main() {
  runApp(ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: SingleChildScrollView(child: ana1()),
        ),
      ),
    );
  }
}

final List<String> myList= [];
final StateProvider<int> _lengthProvider = StateProvider((ref) => 0);

class ana1 extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Container(
        width: 1000,
        height: 1000,
        color: Colors.yellow,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                width: 500,
                child: TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Content Text',
                  ),
                  onSubmitted: (inText) {
                    print(inText);

                    String gir = inText;
                            String myText = "$gir a";
                            var a = 0;

                            List<String> myList = [];

                            for (a = 0; a < myText.length; a++) {
                              if (myText.substring(a, a + 1) == " ") {
                                myList.add(myText.substring(0, a));
                                myText = myText.substring(a + 1, myText.length);
                                a = 0;
                              }
                            }
                            
                            var b = 0;
                            String errorText = "?.; :-";

                            for (b = 0; b < myList.length; b++) {
                              String checkF = myList[b].substring(0, 1);
                              if (errorText.contains(checkF)) {
                                myList[b] =
                                    myList[b].substring(1, myList[b].length);
                              }

                              String checkL = myList[b].substring(
                                  myList[b].length - 1, myList[b].length);
                              if (errorText.contains(checkL)) {
                                myList[b] = myList[b]
                                    .substring(0, myList[b].length - 1);
                              }
                            }



 myList.add("$inText-${myList.length + 1}");
      ref.read(_lengthProvider.state).state = myList.length;




                  },
                )),
            Container(
              color: Colors.amberAccent,
              width: 500,
              child: ListView.builder(
                itemCount: ref.watch(_lengthProvider.state).state,
                itemBuilder: (BuildContext context, int index) {
                  return Text(myList[index]);
                },
              ),
            ),
          ],
        ));
  }
}

推荐答案

我正在将代码片段简化为仅与州提供程序一起使用


final StateProvider<List<String>> itemsProvider = StateProvider((ref) => []);

class ana1 extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Container(
        width: 400,
        height: 300,
        color: Colors.yellow,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
                child: TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Content Text',
              ),
                           onSubmitted: (inText) {
                final sliptIntoWords = inText.split(
                    ","); // Im separating words on , perform your logic here
                final oldItems = ref.read(itemsProvider);
                ref.read(itemsProvider.state).update(
                      (state) => [
                        ...oldItems,
                        ...sliptIntoWords,
                      ],
                    );
              },
            )),
            Expanded(
              child: Container(
                color: Colors.amberAccent,
                child: ListView.builder(
                  itemCount: ref.watch(itemsProvider).length,
                  itemBuilder: (BuildContext context, int index) {
                    return Text(ref.watch(itemsProvider)[index]);
                  },
                ),
              ),
            ),
          ],
        ));
  }
}

我想在ListView号上用化妆品会更好.

Flutter相关问答推荐

创建多个具有适当填充和对齐的按钮的最佳方法

如何让GridView.builder用另外两个小部件来占据屏幕的剩余部分

Flutter -如何停止块监听程序在后台堆栈中运行

如何在Ffltter Chrome应用程序中使用webview_fltter_web显示自定义html而不是外部uri?

GitHub Actions工作流secret中的特殊字符未被保留

Flutter 构建 Web 分段故障 - 无法启动事件处理程序线程

在 Flutter 中滑动关闭 PageView.builder 时出现问题

带有命名参数的回调函数不起作用

TextField 中的富文本并获取 RenderParagraph

更改下拉按钮的背景 colored颜色 - flutter

我想在文本结束后显示分隔线.它会在第一行、第二行或第三行结束

如何格式化网址?

在 Flutter 中使用 HardwareKeyboard 时所有键都没有响应

我无法从 Cloud firestore 访问文档字段并在 statefulwidget 类中显示它

使用 Chaquopy 插件后 Gradle Sync 无法正常工作

Flutter 上Denim blue colored颜色 的 colored颜色 矩阵代码是什么?

Navigator.push '!_debugLocked' 上的错误:不正确

为什么这个循环只发生一次?Flutter /dart

在 Flutter 应用程序中全局使用 assets 文件夹

Flutter - 如何调用此函数?