【问题标题】:Generate GridView with flutter using index使用索引生成带有颤振的 GridView
【发布时间】:2019-10-09 16:07:03
【问题描述】:

我想要做的是生成一个带有上面定义的索引的 GridView,但由于某种原因它说“未定义的名称索引”,请你帮我解决这个问题。

代码如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp(items: List<String>.generate(1000, (index) => "Item $index")));

class MyApp extends StatelessWidget {

  final List<String> items;

  MyApp({Key key, @required this.items}) : super(key: key);


  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(

          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
            appBar:
            AppBar(title:
            Text('List View Vertical'),),
            body:
              GridView.count(
                  crossAxisCount: 2,
                  children: List.generate(100, index)(
                    return Center(child: Text('Items $index',
                  style: Theme.of(context).textTheme.headline,),);
                  ))
        )

    );
  }
}

我期望的结果是使用我已经定义的索引生成 1000 行 gridview。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    用这个替换你的build()

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: Text('List View Vertical'),
          ),
          body: GridView.count(
            crossAxisCount: 2,
            children: items.map((title) {
            return Center(
              child: Text(
                '$title',
                style: Theme.of(context).textTheme.headline,
              ),
            );
          }).toList(),
          ),
        ),
      );
    }
    

    【讨论】:

    • 感谢您的帮助,您的代码工作正常,但不是我想要的方式,如果您要查看第二行上方的代码,我已经在那里定义了索引,我想使用它。跨度>
    • 完美运行!谢谢。
    【解决方案2】:

    更改您的代码以使用您正在传递的属性items 中的值。

    例子:

    class MyApp extends StatelessWidget {
      final List<String> items;
    
      MyApp({Key key, @required this.items}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            appBar: AppBar(
              title: Text('List View Vertical'),
            ),
            body: GridView.count(
              crossAxisCount: 2,
              children: items.map((text) {
                return Center(
                  child: Text(
                    '$text',
                    style: Theme.of(context).textTheme.headline,
                  ),
                );
              }).toList(),
            ),
          ),
        );
      }
    }
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多