简体   繁体   English

小部件内部的颤动循环

[英]flutter loop inside a widget

I'm new to flutter and was wondering if anyone would be able to guide me. 我是新来的人,想知道是否有人能指导我。

I would like to irritate through a string that I retrieved from a json file. 我想通过从json文件中检索到的字符串来激怒。 I would like to display each letter separately. 我想分别显示每个字母。 Example below. 以下示例。

Output Complete Word: Hi Letter pos 0: H Letter pos 1: I 输出完整单词:嗨信pos 0:H Letter pos 1:I

What I tried so far is adding a for loop below the itemBuilder but can't retrieve the variable inside the card. 到目前为止我尝试的是在itemBuilder下面添加一个for循环但是无法检索卡内的变量。 I tried adding a for loop inside the Widget and it doesn't allow me. 我尝试在Widget中添加一个for循环,它不允许我。

Any input would be greatly appreciated! 任何投入将不胜感激!

import 'dart:convert';

import 'package:flutter/material.dart';


void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  List data;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Load local JSON file"),
        ),
        body: new Container(
          child: new Center(
            // Use future builder and DefaultAssetBundle to load the local JSON file
            child: new FutureBuilder(
                future: DefaultAssetBundle
                    .of(context)
                    .loadString('data_repo/starwars_data.json'),
                builder: (context, snapshot) {
                  // Decode the JSON
                  var new_data = JSON.decode(snapshot.data.toString());

                  return new ListView.builder(
                    // Build the ListView

                    itemBuilder: (BuildContext context, int index) { 
                      return new Card(
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            new Text("Complete Word: " + new_data[index]['complete_word'])
                          ],
                        ),
                      );
                    },
                    itemCount: new_data == null ? 0 : new_data.length,
                  );
                }),
          ),
        ));
  }
}

Create a method and within it create a list of widgets. 创建一个方法,并在其中创建一个小部件列表。 Include the widgets and then return the list. 包括小部件,然后返回列表。

Example

child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: ListMyWidgets()),

List<Widget> ListMyWidgets() {
List<Widget> list = new List();
list.add(new Text("hi"));
list.add(new Text("hi2"));
list.add(new Text("hi3"));
return list;
}

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

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