1 ВАРИАНТ разумеется работает
children: <Widget>[
MyContainer('1', 75.0, 75.0, 0.0),
MyContainer('2', 75.0, 75.0, 0.0),
MyContainer('3', 75.0, 75.0, 0.0),
MyContainer('4', 75.0, 75.0, 0.0),
MyContainer('5', 75.0, 75.0, 0.0),
],
2 ВАРИАНТ работает
children: f1(),
List f1() {
var ara = List<Widget>();
for(int i=0; i<5;) {
i++;
ara.add(MyContainer(i.toString(), 75.0, 75.0, 0.0));
}
return ara;
}
3 ВАРИАНТ разумеется не работает. Как в 3 варианте вставить код и возможно ли такое? Спасибо.
children:
for(int i=0; i<5;) {
i++;
MyContainer(i.toString(), 75.0, 75.0, 0.0);
},
полный пример кода
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Name App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
//title: Text('Name Page'),
),
body: MyHomePage(),
),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: null,
color: Colors.amber,
child: Wrap(
direction: Axis.horizontal,
spacing: 10.0,
runSpacing: 10.0,
//1 ВАРИАНТ
/*children: <Widget>[
MyContainer('1', 75.0, 75.0, 0.0),
MyContainer('2', 75.0, 75.0, 0.0),
MyContainer('3', 75.0, 75.0, 0.0),
MyContainer('4', 75.0, 75.0, 0.0),
MyContainer('5', 75.0, 75.0, 0.0),
],*/
children: f1(), //2 ВАРИАНТ
//3 ВАРИАНТ не работает
/*children:
for(int i=0; i<5;) {
i++;
ara.add(MyContainer(i.toString(), 75.0, 75.0, 0.0));
},*/
),
);
}
}
class MyContainer extends StatelessWidget {
final String _str;
final double _width;
final double _height;
final double _margin;
MyContainer(this._str, this._width, this._height, this._margin);
@override
Widget build(BuildContext context) {
return Container(
width: _width,
height: _height,
margin: EdgeInsets.all(_margin),
color: Colors.pink,
alignment: Alignment.center,
child: Text(
_str,
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
),
),
);
}
}
List f1() {
var ara = List<Widget>();
for(int i=0; i<5;) {
i++;
ara.add(MyContainer(i.toString(), 75.0, 75.0, 0.0));
}
return ara;
}