Line data Source code
1 : import 'package:flutter/material.dart'; 2 : 3 0 : void main() { 4 0 : runApp(const MyApp()); 5 : } 6 : 7 : class MyApp extends StatelessWidget { 8 : final MaterialColor primarySwatch; 9 5 : const MyApp({ 10 : Key? key, 11 : this.primarySwatch = Colors.blue, 12 1 : }) : super(key: key); 13 : 14 : // This widget is the root of your application. 15 2 : @override 16 : Widget build(BuildContext context) { 17 2 : return MaterialApp( 18 : title: 'Flutter Demo', 19 2 : theme: ThemeData( 20 2 : primarySwatch: primarySwatch, 21 : ), 22 : home: const MyHomePage(title: 'Flutter Demo Home Page'), 23 : ); 24 : } 25 : } 26 : 27 : class MyHomePage extends StatefulWidget { 28 2 : const MyHomePage({Key? key, required this.title}) : super(key: key); 29 : 30 : // This widget is the home page of your application. It is stateful, meaning 31 : // that it has a State object (defined below) that contains fields that affect 32 : // how it looks. 33 : 34 : // This class is the configuration for the state. It holds the values (in this 35 : // case the title) provided by the parent (in this case the App widget) and 36 : // used by the build method of the State. Fields in a Widget subclass are 37 : // always marked "final". 38 : 39 : final String title; 40 : 41 2 : @override 42 2 : State<MyHomePage> createState() => _MyHomePageState(); 43 : } 44 : 45 : class _MyHomePageState extends State<MyHomePage> { 46 : int _counter = 0; 47 : 48 1 : void _incrementCounter() { 49 2 : setState(() { 50 : // This call to setState tells the Flutter framework that something has 51 : // changed in this State, which causes it to rerun the build method below 52 : // so that the display can reflect the updated values. If we changed 53 : // _counter without calling setState(), then the build method would not be 54 : // called again, and so nothing would appear to happen. 55 2 : _counter++; 56 : }); 57 : } 58 : 59 2 : @override 60 : Widget build(BuildContext context) { 61 : // This method is rerun every time setState is called, for instance as done 62 : // by the _incrementCounter method above. 63 : // 64 : // The Flutter framework has been optimized to make rerunning build methods 65 : // fast, so that you can just rebuild anything that needs updating rather 66 : // than having to individually change instances of widgets. 67 2 : return Scaffold( 68 2 : appBar: AppBar( 69 : // Here we take the value from the MyHomePage object that was created by 70 : // the App.build method, and use it to set our appbar title. 71 6 : title: Text(widget.title), 72 : ), 73 2 : body: Center( 74 : // Center is a layout widget. It takes a single child and positions it 75 : // in the middle of the parent. 76 2 : child: Column( 77 : // Column is also a layout widget. It takes a list of children and 78 : // arranges them vertically. By default, it sizes itself to fit its 79 : // children horizontally, and tries to be as tall as its parent. 80 : // 81 : // Invoke "debug painting" (press "p" in the console, choose the 82 : // "Toggle Debug Paint" action from the Flutter Inspector in Android 83 : // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) 84 : // to see the wireframe for each widget. 85 : // 86 : // Column has various properties to control how it sizes itself and 87 : // how it positions its children. Here we use mainAxisAlignment to 88 : // center the children vertically; the main axis here is the vertical 89 : // axis because Columns are vertical (the cross axis would be 90 : // horizontal). 91 : mainAxisAlignment: MainAxisAlignment.center, 92 2 : children: <Widget>[ 93 : const Text( 94 : 'You have pushed the button this many times:', 95 : ), 96 2 : Text( 97 4 : '$_counter', 98 6 : style: Theme.of(context).textTheme.headline4, 99 : ), 100 : ], 101 : ), 102 : ), 103 2 : floatingActionButton: FloatingActionButton( 104 2 : onPressed: _incrementCounter, 105 : tooltip: 'Increment', 106 : child: const Icon(Icons.add), 107 : ), // This trailing comma makes auto-formatting nicer for build methods. 108 : ); 109 : } 110 : }