main.dart
import "package:flutter/material.dart";import 'package:app1/demos/increase/Increase.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'app', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: '计数器 demo'), ); }}
demos/increse/Increse.dart
import "package:flutter/material.dart";class MyHomePage extends StatefulWidget { final String title; MyHomePage({key, @required this.title}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> { // 定义一个状态值,用于记录按钮点击的总次数。 int _counter = 0; // _counter 为保存屏幕右下角带“+”号按钮点击次数的状态。 // 数字自增函数 void _increaseCounter() { // 数字自增完成后,需要一个setState函数。如果不使用,那么我们的视图不会刷新。 setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('${widget.title}'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text('You have pushed the button this many times:'), Text( '$_counter', style: TextStyle( color: Theme.of(context).primaryColor, // 使用主题色 fontSize: 30, ), ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _increaseCounter, //传入自增函数 tooltip: 'increse button', child: Icon(Icons.add), ), ); }}