flutter 计数器改进版

对着背影说爱祢 提交于 2020-02-08 20:33:12

程序截图

在这里插入图片描述

源码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Demo',
      theme: new ThemeData(
        primaryColor: Colors.white,
      ),
      home: new HomePage(title: 'Demo'),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage>{
  int _counter = 0;
  void _increaseCounter(){
    setState(() {
      _counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            new IconButton(
              icon: new Icon(
                _counter != 0 ? Icons.favorite: Icons.favorite_border,
                color: Colors.red,
              ),
              iconSize: 100.0,
              onPressed: (){
                setState(() {
                  _counter = 0;
                });
              },

            ),
          ],
        )
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _increaseCounter,
        tooltip: 'press',
        child: new Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),

    );
  }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!