前提
Flutterの初学者です。
class MyHomePage
の中にある
const MyHomePage({super.key, required this.title});
についてです。
この引数key
は、一体どこで使われているのでしょうか?
super.key
は親要素のプロパティkey
を参照しているのは分かるのですが、
home: const MyHomePage(title: 'Flutter Demo Home Page'),
の際に、
引数を渡していないですよね?
実現したいこと
super.key
が必要な理由の理解
該当のソースコード
main.dart
1import 'package:flutter/material.dart'; 2 3void main() { 4 runApp(const MyApp()); 5} 6 7class MyApp extends StatelessWidget { 8 const MyApp({super.key}); 9 10 @override 11 Widget build(BuildContext context) { 12 return MaterialApp( 13 title: 'Flutter Demo', 14 theme: ThemeData( 15 primarySwatch: Colors.blue, 16 ), 17 home: const MyHomePage(title: 'Flutter Demo Home Page'), 18 ); 19 } 20} 21 22class MyHomePage extends StatefulWidget { 23 24// ↓このコードです。 25 const MyHomePage({super.key, required this.title}); 26 27 final String title; 28 29 @override 30 State<MyHomePage> createState() => _MyHomePageState(); 31} 32 33class _MyHomePageState extends State<MyHomePage> { 34 int _counter = 0; 35 void _incrementCounter() { 36 setState(() { 37 _counter++; 38 }); 39 } 40 41 @override 42 Widget build(BuildContext context) { 43 return Scaffold( 44 appBar: AppBar( 45 title: Text(widget.title), 46 ), 47 body: Center( 48 child: Column( 49 mainAxisAlignment: MainAxisAlignment.center, 50 children: <Widget>[ 51 const Text( 52 'You have pushed the button this many times:', 53 ), 54 Text( 55 '$_counter', 56 style: Theme.of(context).textTheme.headline4, 57 ), 58 ], 59 ), 60 ), 61 floatingActionButton: FloatingActionButton( 62 onPressed: _incrementCounter, 63 tooltip: 'Increment', 64 child: const Icon(Icons.add), 65 ), 66 ); 67 } 68}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/22 23:48