main.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 blue 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. useMaterial3: true,
  30. ),
  31. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  32. );
  33. }
  34. }
  35. class MyHomePage extends StatefulWidget {
  36. const MyHomePage({super.key, required this.title});
  37. // This widget is the home page of your application. It is stateful, meaning
  38. // that it has a State object (defined below) that contains fields that affect
  39. // how it looks.
  40. // This class is the configuration for the state. It holds the values (in this
  41. // case the title) provided by the parent (in this case the App widget) and
  42. // used by the build method of the State. Fields in a Widget subclass are
  43. // always marked "final".
  44. final String title;
  45. @override
  46. State<MyHomePage> createState() => _MyHomePageState();
  47. }
  48. class _MyHomePageState extends State<MyHomePage> {
  49. int _counter = 0;
  50. void _incrementCounter() {
  51. setState(() {
  52. // This call to setState tells the Flutter framework that something has
  53. // changed in this State, which causes it to rerun the build method below
  54. // so that the display can reflect the updated values. If we changed
  55. // _counter without calling setState(), then the build method would not be
  56. // called again, and so nothing would appear to happen.
  57. _counter++;
  58. });
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. // This method is rerun every time setState is called, for instance as done
  63. // by the _incrementCounter method above.
  64. //
  65. // The Flutter framework has been optimized to make rerunning build methods
  66. // fast, so that you can just rebuild anything that needs updating rather
  67. // than having to individually change instances of widgets.
  68. return Scaffold(
  69. appBar: AppBar(
  70. // TRY THIS: Try changing the color here to a specific color (to
  71. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
  72. // change color while the other colors stay the same.
  73. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  74. // Here we take the value from the MyHomePage object that was created by
  75. // the App.build method, and use it to set our appbar title.
  76. title: Text(widget.title),
  77. ),
  78. body: Center(
  79. // Center is a layout widget. It takes a single child and positions it
  80. // in the middle of the parent.
  81. child: Column(
  82. // Column is also a layout widget. It takes a list of children and
  83. // arranges them vertically. By default, it sizes itself to fit its
  84. // children horizontally, and tries to be as tall as its parent.
  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. //
  92. // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
  93. // action in the IDE, or press "p" in the console), to see the
  94. // wireframe for each widget.
  95. mainAxisAlignment: MainAxisAlignment.center,
  96. children: <Widget>[
  97. const Text(
  98. 'You have pushed the button this many times:',
  99. ),
  100. Text(
  101. '$_counter',
  102. style: Theme.of(context).textTheme.headlineMedium,
  103. ),
  104. ],
  105. ),
  106. ),
  107. floatingActionButton: FloatingActionButton(
  108. onPressed: _incrementCounter,
  109. tooltip: 'Increment',
  110. child: const Icon(Icons.add),
  111. ), // This trailing comma makes auto-formatting nicer for build methods.
  112. );
  113. }
  114. }