Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振-如何在列表视图中仅显示选定项_Android_Ios_Variables_Dart_Flutter - Fatal编程技术网

Android 颤振-如何在列表视图中仅显示选定项

Android 颤振-如何在列表视图中仅显示选定项,android,ios,variables,dart,flutter,Android,Ios,Variables,Dart,Flutter,我一直在寻找一种方法,将所选项目从列表视图永久存储到另一个列表视图中,用于flatter/Dart。 此问题不包含完整代码,项目包含更多内容,可在以下网址查看: 如果您对此有任何想法,我们将不胜感激 主列表视图位于主页。dart 保存项目的第二个列表视图将位于favorites\u页面。dart 这是所讨论的主要列表视图: Widget _cryptoWidget() {     return new Container(         child: new Column(          

我一直在寻找一种方法,将所选项目从列表视图永久存储到另一个列表视图中,用于flatter/Dart。

此问题不包含完整代码,项目包含更多内容,可在以下网址查看:

如果您对此有任何想法,我们将不胜感激

主列表视图位于
主页。dart

保存项目的第二个列表视图将位于
favorites\u页面。dart

这是所讨论的主要列表视图:

Widget _cryptoWidget() {
    return new Container(
        child: new Column(
          children: <Widget>[
            new Flexible(
              child: new ListView.builder(
                itemCount: _currencies.length,
                itemBuilder: (BuildContext context, int index) {
                  final int i = index ~/ 2;
                  final Crypto currency = _currencies[i];
                  final MaterialColor color = _colors[i % _colors.length];
                  if (index.isOdd) {
                    return new Divider();
                  }
                  return _getListItemUi(currency, color);
                },
              ),
            ),
          ],
        )
      );
  }

  ListTile _getListItemUi(Crypto currency, MaterialColor color) {
    return new ListTile(
      leading: new Image.network("http://cryptoicons.co/32@2x/color/"+currency.symbol.toLowerCase()+"@2x.png"),
      title: new Text(currency.name,
          style: new TextStyle(fontWeight: FontWeight.bold)),
      subtitle:
      _getSubtitleText(currency.price_usd, currency.percent_change_1h),
      isThreeLine: true,
      trailing: new IconButton(
        icon: new Icon(Icons.add),
        onPressed: () async { Directory appDocDir = await         getApplicationDocumentsDirectory();
        String appDocPath = appDocDir.path;
        var myFile = new File('$appDocPath/my_file.txt')
        ..writeAsStringSync('myVar: $_currencies');  
        print(myFile.absolute.path);
        }
      ),
    );
  }
Widget\u cryptoWidget(){
退回新货柜(
子:新列(
儿童:[
新柔性(
子项:新建ListView.builder(
itemCount:_.length,
itemBuilder:(构建上下文,int索引){
最终int i=指数~/2;
最终加密货币=_货币[i];
最终材料颜色=_颜色[i%_颜色.length];
if(索引isOdd){
返回新的分隔符();
                  }
返回_getListItemUi(货币、颜色);
                },
              ),
            ),
          ],
        )
      );
  }
ListTile\u getListItemUi(加密货币、材质颜色){
返回新的ListTile(
前导:新映像。网络(“http://cryptoicons.co/32@2x/color/“+currency.symbol.toLowerCase()+”@2x.png”),
标题:新文本(currency.name,
样式:新文本样式(fontWeight:fontWeight.bold)),
副标题:
_getSubtitleText(货币.价格_美元,货币.百分比_变化_1h),
伊斯特里琳:是的,
拖尾:新图标按钮(
图标:新图标(Icons.add),
onPressed:()异步{Directory appDocDir=wait getApplicationDocumentsDirectory();
字符串appDocPath=appDocDir.path;
var myFile=新文件(“$appDocPath/my_File.txt”)
..writeAsStringSync('myVar:$_货币');
打印(myFile.absolute.path);
        }
      ),
    );
  }

我很抱歉-这已经变成了一堵text=D的墙。希望它能有所帮助

在flatter中,数据几乎总是沿着所谓的“小部件树”传递。这部分是为了性能,但似乎也是开发人员喜欢的范例

这意味着,如果您有两个不同的“页面”,每个页面都显示有导航器,那么在它们之间共享状态通常并不简单

但是,有一个例外-如果您总是从“主页”进入“收藏夹”页面,则可以推送包含数据的MaterialPage路径。这类似于以下内容-直接从以下内容复制:

通过使用InheritedWidget,您的小部件将在继承的小部件发生更改时重建。然而,这并不是当InheritedWidget的数据发生变化时,而是当它实际被替换时。InheritedData的问题是它是不可变的(即所有成员都必须是final),并且它仍然不能帮助将状态传递回树

因此,您仍然需要一个StatefulWidget,以便可以向上传递您的收藏夹列表。这归结为由StatefulWidget构建一个InheritedWidget,使用
InheritedWidget.of
读取数据,并使用
StatefulWidget.of
设置状态。有关如何将其连接起来的示例,请参见

缺点是有相当多的样板文件

2.5:让我们把它简化一点=)

您不必每次都为继承的小部件处理所有的连接,您可以使用一些帮助,例如。它允许您简单地编写模型(从作用域模型继承)。但它仍然使用基本相同的底层结构

下面是一个直接来自作用域_模型插件的示例:

// Start by creating a class that holds some view the app's state. In
// our example, we'll have a simple counter that starts at 0 can be 
// incremented.
//
// Note: It must extend from Model.  
class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    // First, increment the counter
    _counter++;

    // Then notify all the listeners.
    notifyListeners();
  }
}

// Create our App, which will provide the `CounterModel` to 
// all children that require it! 
class CounterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // First, create a `ScopedModel` widget. This will provide 
    // the `model` to the children that request it. 
    return new ScopedModel<CounterModel>(
      model: new CounterModel(),
      child: new Column(children: [
        // Create a ScopedModelDescendant. This widget will get the
        // CounterModel from the nearest ScopedModel<CounterModel>. 
        // It will hand that model to our builder method, and rebuild 
        // any time the CounterModel changes (i.e. after we 
        // `notifyListeners` in the Model). 
        new ScopedModelDescendant<CounterModel>(
                builder: (context, child, model) => new Text(
                    model.counter.toString()),
              ),
        new Text("Another widget that doesn't depend on the CounterModel")
      ])
    );
  }
}
//首先创建一个类,该类保存应用程序状态的某些视图。在里面
//在我们的示例中,我们将有一个简单的计数器,它可以从0开始
//增加。
//
//注意:它必须从模型扩展。
类反模型扩展模型{
int _计数器=0;
int get counter=>\u计数器;
无效增量(){
//首先,递增计数器
_计数器++;
//然后通知所有的听众。
notifyListeners();
}
}
//创建我们的应用程序,它将为
//所有需要它的孩子!
类CounterApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
//首先,创建一个“ScopedModel”小部件
//“模特”是给那些要求它的孩子们的。
返回新的ScopedModel(
型号:新的计数器型号(),
子项:新列(子项:[
//创建ScopedModelSecondant。此小部件将获取
//来自最近ScopedModel的反模型。
//它将把该模型交给我们的构建器方法,然后重新构建
//当反模型发生变化时(即在我们
//模型中的“notifyListeners”)。
新ScopedModelDescentant(
生成器:(上下文、子对象、模型)=>新文本(
model.counter.toString()),
),
新文本(“另一个不依赖于反模型的小部件”)
])
);
}
}
他们在ScopedModel代码中做了一些骇人的事情,因此可以进行改进。但这可能是代码方面做您想要的事情的最简单方法

3:全面设计模式-集团

继续我们一直遵循的轨迹
class FrogColor extends InheritedWidget {
  const FrogColor({
    Key key,
    @required this.color,
    @required Widget child,
  }) : assert(color != null),
       assert(child != null),
       super(key: key, child: child);

  final Color color;

  static FrogColor of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(FrogColor);
  }

  @override
  bool updateShouldNotify(FrogColor old) => color != old.color;
}
// Start by creating a class that holds some view the app's state. In
// our example, we'll have a simple counter that starts at 0 can be 
// incremented.
//
// Note: It must extend from Model.  
class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    // First, increment the counter
    _counter++;

    // Then notify all the listeners.
    notifyListeners();
  }
}

// Create our App, which will provide the `CounterModel` to 
// all children that require it! 
class CounterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // First, create a `ScopedModel` widget. This will provide 
    // the `model` to the children that request it. 
    return new ScopedModel<CounterModel>(
      model: new CounterModel(),
      child: new Column(children: [
        // Create a ScopedModelDescendant. This widget will get the
        // CounterModel from the nearest ScopedModel<CounterModel>. 
        // It will hand that model to our builder method, and rebuild 
        // any time the CounterModel changes (i.e. after we 
        // `notifyListeners` in the Model). 
        new ScopedModelDescendant<CounterModel>(
                builder: (context, child, model) => new Text(
                    model.counter.toString()),
              ),
        new Text("Another widget that doesn't depend on the CounterModel")
      ])
    );
  }
}