【问题标题】:Is using widget.[variable_name] the best way to reference StatefulWidget variables?使用 widget.[variable_name] 是引用 StatefulWidget 变量的最佳方式吗?
【发布时间】:2021-08-29 06:13:39
【问题描述】:

我有一个变量friendsList,它被传递给 FriendsFeed 类。我用widget.friendsList 在FriendsFeed 状态中引用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);
        });
      },
    );
  }
}

【问题讨论】:

    标签: flutter dart variables widget statefulwidget


    【解决方案1】:

    是的,是的。但仍有一些方法可以让它更干净。

    一种方法是在构建方法中创建一个最终变量,或者作为应用程序中的最终字段,例如“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);
            });
          },
        );
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2022-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多