简体   繁体   English

Flutter TextField 不遵循 ReorderableListView 中的 ListTiles

[英]Flutter TextField does not follow ListTiles within ReorderableListView

I'm trying to use a TextFormField within a ReorderableListView and can't seem to get the text to follow the moving ListTile .我正在尝试在ReorderableListView中使用TextFormField ,但似乎无法让文本跟随移动的ListTile Below is a minimally modified copy of the ReorderableListView sample code that reproduces the issue.下面是重现该问题的ReorderableListView 示例代码的最小修改副本。 All that's added is a list of Strings and a TextFormField per ListTile .添加的只是一个Strings列表和一个TextFormField每个ListTile

The example includes a Text widget that tracks with the movement of the ListTile .该示例包括一个跟踪ListTile移动的Text小部件。 The difference I see with Text is that it is possible to update the Text constructor in the build method of the ListTile .我看到Text的不同之处在于可以在ListTile的 build 方法中更新Text构造函数。

Using an itemBuilder: instead of the static children: property seems to make no difference.使用itemBuilder:而不是静态children:属性似乎没有什么区别。

What am I missing here?我在这里想念什么? Thanks in advance!提前致谢!

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  final List<int> _items = List<int>.generate(50, (int index) => index);
  final List<String> _textValues = List<String>.generate(50, (int index) => '');

  @override
  Widget build(BuildContext context) {
    final ColorScheme colorScheme = Theme.of(context).colorScheme;
    final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
    final Color evenItemColor = colorScheme.primary.withOpacity(0.15);

    return ReorderableListView(
      padding: const EdgeInsets.symmetric(horizontal: 40),
      children: <Widget>[
        for (int index = 0; index < _items.length; index += 1)
          ListTile(
            key: Key('$index'),
            tileColor: _items[index].isOdd ? oddItemColor : evenItemColor,
            title: Text('Item ${_items[index]}: ${_textValues[index]}'),
            subtitle: TextFormField(
              initialValue: _textValues[index],
              onChanged: (String? value) {
                setState(() {
                  _textValues[index] = value!;
                });
              },
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
              ),
            ),
          ),
      ],
      onReorder: (int oldIndex, int newIndex) {
        setState(() {
          if (oldIndex < newIndex) {
            newIndex -= 1;
          }
          final int item = _items.removeAt(oldIndex);
          _items.insert(newIndex, item);
          final String textValue = _textValues.removeAt(oldIndex);
          _textValues.insert(newIndex, textValue);
        });
      },
    );
  }
}

在此处输入图像描述

It appears as though this works if I wrap the int and String in an object, and then use a single ObjectKey as the key for the ListTile .如果我将intString包装在一个对象中,然后使用单个 ObjectKey 作为ObjectKey的键,这ListTile I guess the lesson here is that even if multiple lists of properties are in sync, some widgets cache or something and aren't properly rebuilt, while others are.我想这里的教训是,即使多个属性列表是同步的,一些小部件缓存或其他东西并且没有正确重建,而另一些则是。

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

class Item {
  int? item;
  String? text;

  Item(this.item, this.text);
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  final List<Item> _listItems =
      List<Item>.generate(50, (int index) => Item(index, ''));

  @override
  Widget build(BuildContext context) {
    final ColorScheme colorScheme = Theme.of(context).colorScheme;
    final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
    final Color evenItemColor = colorScheme.primary.withOpacity(0.15);

    return ReorderableListView(
      padding: const EdgeInsets.symmetric(horizontal: 40),
      children: <Widget>[
        for (int index = 0; index < _listItems.length; index += 1)
          ListTile(
            key: ObjectKey(_listItems[index]),
            tileColor:
                _listItems[index].item!.isOdd ? oddItemColor : evenItemColor,
            title: Text(
                'Item ${_listItems[index].item}: ${_listItems[index].text}'),
            subtitle: TextFormField(
              initialValue: _listItems[index].text,
              onChanged: (String? value) {
                setState(() {
                  _listItems[index].text = value!;
                });
              },
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
              ),
            ),
          ),
      ],
      onReorder: (int oldIndex, int newIndex) {
        setState(() {
          if (oldIndex < newIndex) {
            newIndex -= 1;
          }
          final Item listItem = _listItems.removeAt(oldIndex);
          _listItems.insert(newIndex, listItem);
        });
      },
    );
  }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM