我想做的是使用上面定义的索引生成带有抖动的GridView,但是由于某些原因它说“ Undefined name index”,请您帮我。
这里是代码:
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。
更改代码以使用传递的属性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(),
),
),
);
}
}
希望有帮助!
用此替换您的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(),
),
),
);
}