使用 widget.[variable_name] 是引用 StatefulWidget 变量的最佳方式吗?

如何解决使用 widget.[variable_name] 是引用 StatefulWidget 变量的最佳方式吗?

我有一个传递给 FriendsFeed 类的变量 friendsList。我用 friendsList 引用 FriendsFeed 状态中的 widget.friendsList。使用 widget.[my_variable_name] 来引用 StatefulWidget 变量是一种专业的方式吗?我不禁觉得有一种更简洁的方法可以做到这一点。

import 'package:Flutter/material.dart';

class FriendsFeed extends StatefulWidget {
  FriendsFeed(this.friendsList);

  final List<dynamic> friendsList;

  @override
  _FriendsFeedState createState() => _FriendsFeedState();
}

class _FriendsFeedState extends State<FriendsFeed> {
  @override
  void initState() {
    super.initState();
  }

  @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 < widget.friendsList.length; index++)
          ListTile(
            key: Key('$index'),tileColor:
                widget.friendsList[index].isOdd ? oddItemColor : evenItemColor,title: Text('Item ${widget.friendsList[index]}'),),],onReorder: (int oldindex,int newIndex) {
        setState(() {
          if (oldindex < newIndex) {
            newIndex -= 1;
          }
          final int item = widget.friendsList.removeAt(oldindex);
          widget.friendsList.insert(newIndex,item);
        });
      },);
  }
}

解决方法

是的,是的。但您仍然可以通过一些方法使其更干净。

一种方法是在 build 方法中创建一个 final 变量,或者作为应用程序中的 final 字段,比如“friendList”,然后将其赋值为“widget.friendsList”。您现在可以继续在构建函数或应用中的任何位置使用 var "friendList"(取决于您声明它的位置)。

因此,从您的代码看来,您似乎希望能够重新排序/重新排列列表中的项目,这意味着能够修改列表,我建议您像这样在状态类中创建一个字段:

    class FriendsFeed extends StatefulWidget {
      FriendsFeed(this.friendsList);

      final List<dynamic> friendsList;

      @override
      _FriendsFeedState createState() => _FriendsFeedState();
    }

    class _FriendsFeedState extends State<FriendsFeed> {
      // declare it here if you wish to make any modification to the list 
      // outside the build function.
      late List<dynamic> _friendsList;

      @override
      void initState() {
      _friendsList = widget.friendsList;

      super.initState();
    }

      @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 < _friendsList.length; index++)
          ListTile(
            key: Key('$index'),tileColor: _friendsList[index].isOdd ? oddItemColor : evenItemColor,title: Text('Item ${_friendsList[index]}'),),],onReorder: (int oldIndex,int newIndex) {
        setState(() {
          if (oldIndex < newIndex) {
            newIndex -= 1;
          }
          final int item = _friendsList.removeAt(oldIndex);
          _friendsList.insert(newIndex,item);
        });
      },);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?