https://flutter.dev/docs/development/ui/layout#nesting-rows-and-columns
上記のページをやっていて、
import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart' show debugPaintSizeEnabled; void main() { debugPaintSizeEnabled = true; // Remove to suppress visual layout runApp(MyApp()); } class MyApp extends StatelessWidget { Widget stars=Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.star, color: Colors.green[500]), Icon(Icons.star, color: Colors.green[500]), Icon(Icons.star, color: Colors.black), Icon(Icons.star, color: Colors.black), Icon(Icons.star, color: Colors.black), ], ); Widget ratings = Container( padding: EdgeInsets.all(20), child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ stars, Text( '130 Reviews', style: TextStyle( color: Colors.black, fontWeight: FontWeight.w800, fontFamily: 'Roboto', letterSpacing: 0.5, fontSize: 20, ), ), ], ), ); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter layout demo', home: Scaffold( appBar: AppBar( title: Text('Flutter layout demo'), ), // Change to buildColumn() for the other column example body: ratings, ), ); } }
を実行すると、
The instance member 'stars' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expressio
というエラーが出たのですが、何がまずいのかよくわかりません。
とりあえずratingsとstarsを関数にしたらエラーは消えたのですが、
上記コードでエラーが出る原因は何でしょうか?
2020/11/1/12:13追記
サンプルコード
import 'package:flutter/material.dart'; //import 'package:flutter/rendering.dart' show debugPaintSizeEnabled; void main() { //debugPaintSizeEnabled = true; // Remove to suppress visual layout //↓(1)→結果: x=10,y=10 //runApp(MyApp._internal(x:10,y:10)); //(2)→結果: x=20,y=0 //runApp(MyApp.red(20)); //(3)→結果: x=30,y=0 runApp(MyApp(30)); } class MyApp extends StatelessWidget { MyApp._internal({this.x, this.y}); //生成的コンストラクタ MyApp.red(int x) : this._internal(x: x, y: 0); //リダイレクトコンストラクタ factory MyApp(x) { //factoryコンストラクタ MyApp _ma = MyApp._internal(x: x, y: 0); return _ma; } final int x; final int y; @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter layout demo', home: Scaffold( appBar: AppBar( title: Text('Flutter layout demo'), ), // Change to buildColumn() for the other column example body: Row(children: [Text("x=$x"), Text("y=$y")]), ), ); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/31 01:13
2020/10/31 03:37
2020/11/01 03:12
2020/11/01 03:12