main.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. // This is the theme of your application.
  14. //
  15. // TRY THIS: Try running your application with "flutter run". You'll see
  16. // the application has a purple toolbar. Then, without quitting the app,
  17. // try changing the seedColor in the colorScheme below to Colors.green
  18. // and then invoke "hot reload" (save your changes or press the "hot
  19. // reload" button in a Flutter-supported IDE, or press "r" if you used
  20. // the command line to start the app).
  21. //
  22. // Notice that the counter didn't reset back to zero; the application
  23. // state is not lost during the reload. To reset the state, use hot
  24. // restart instead.
  25. //
  26. // This works for code too, not just values: Most code changes can be
  27. // tested with just a hot reload.
  28. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  29. ),
  30. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  31. );
  32. }
  33. }
  34. class MyHomePage extends StatefulWidget {
  35. const MyHomePage({super.key, required this.title});
  36. // This widget is the home page of your application. It is stateful, meaning
  37. // that it has a State object (defined below) that contains fields that affect
  38. // how it looks.
  39. // This class is the configuration for the state. It holds the values (in this
  40. // case the title) provided by the parent (in this case the App widget) and
  41. // used by the build method of the State. Fields in a Widget subclass are
  42. // always marked "final".
  43. final String title;
  44. @override
  45. State<MyHomePage> createState() => _MyHomePageState();
  46. }
  47. class _MyHomePageState extends State<MyHomePage> {
  48. int _counter = 0;
  49. void _incrementCounter() {
  50. setState(() {
  51. // This call to setState tells the Flutter framework that something has
  52. // changed in this State, which causes it to rerun the build method below
  53. // so that the display can reflect the updated values. If we changed
  54. // _counter without calling setState(), then the build method would not be
  55. // called again, and so nothing would appear to happen.
  56. _counter++;
  57. });
  58. }
  59. @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. return Scaffold(
  68. appBar: AppBar(
  69. // TRY THIS: Try changing the color here to a specific color (to
  70. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
  71. // change color while the other colors stay the same.
  72. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  73. // Here we take the value from the MyHomePage object that was created by
  74. // the App.build method, and use it to set our appbar title.
  75. title: Text(widget.title),
  76. ),
  77. body: Center(
  78. // Center is a layout widget. It takes a single child and positions it
  79. // in the middle of the parent.
  80. child: Column(
  81. // Column is also a layout widget. It takes a list of children and
  82. // arranges them vertically. By default, it sizes itself to fit its
  83. // children horizontally, and tries to be as tall as its parent.
  84. //
  85. // Column has various properties to control how it sizes itself and
  86. // how it positions its children. Here we use mainAxisAlignment to
  87. // center the children vertically; the main axis here is the vertical
  88. // axis because Columns are vertical (the cross axis would be
  89. // horizontal).
  90. //
  91. // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
  92. // action in the IDE, or press "p" in the console), to see the
  93. // wireframe for each widget.
  94. mainAxisAlignment: MainAxisAlignment.center,
  95. children: <Widget>[
  96. const Text('You have pushed the button this many times:'),
  97. Text(
  98. '$_counter',
  99. style: Theme.of(context).textTheme.headlineMedium,
  100. ),
  101. ],
  102. ),
  103. ),
  104. floatingActionButton: FloatingActionButton(
  105. onPressed: _incrementCounter,
  106. tooltip: 'Increment',
  107. child: const Icon(Icons.add),
  108. ), // This trailing comma makes auto-formatting nicer for build methods.
  109. );
  110. }
  111. }