main.dart

  1. import "package:flutter/material.dart";
  2. import 'package:app1/demos/increase/Increase.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. title: 'app',
  9. theme: ThemeData(
  10. primarySwatch: Colors.blue,
  11. ),
  12. home: MyHomePage(title: '计数器 demo'),
  13. );
  14. }
  15. }

demos/increse/Increse.dart

  1. import "package:flutter/material.dart";
  2. class MyHomePage extends StatefulWidget {
  3. final String title;
  4. MyHomePage({key, @required this.title}) : super(key: key);
  5. @override
  6. _MyHomePageState createState() => _MyHomePageState();
  7. }
  8. class _MyHomePageState extends State<MyHomePage> {
  9. // 定义一个状态值,用于记录按钮点击的总次数。
  10. int _counter = 0; // _counter 为保存屏幕右下角带“+”号按钮点击次数的状态。
  11. // 数字自增函数
  12. void _increaseCounter() {
  13. // 数字自增完成后,需要一个setState函数。如果不使用,那么我们的视图不会刷新。
  14. setState(() {
  15. _counter++;
  16. });
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. appBar: AppBar(
  22. title: Text('${widget.title}'),
  23. ),
  24. body: Center(
  25. child: Column(
  26. mainAxisAlignment: MainAxisAlignment.center,
  27. children: <Widget>[
  28. Text('You have pushed the button this many times:'),
  29. Text(
  30. '$_counter',
  31. style: TextStyle(
  32. color: Theme.of(context).primaryColor, // 使用主题色
  33. fontSize: 30,
  34. ),
  35. ),
  36. ],
  37. ),
  38. ),
  39. floatingActionButton: FloatingActionButton(
  40. onPressed: _increaseCounter, //传入自增函数
  41. tooltip: 'increse button',
  42. child: Icon(Icons.add),
  43. ),
  44. );
  45. }
  46. }