计数器

作者: Jean_Lina | 来源:发表于2021-09-03 11:07 被阅读0次
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    main() {
      runApp(DBJApp());
    }
    
    class DBJApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(primaryColor: Colors.green),
          home: Scaffold(
            appBar: AppBar(
              title: Text('计数器'),
            ),
            body: DBJHomeBody('传递一个message'),
          ),
        );
      }
    }
    
    class DBJHomeBody extends StatefulWidget {
      final String message;
      DBJHomeBody(this.message);
    
      @override
      _DBJHomeBodyState createState() {
        return _DBJHomeBodyState();
      }
    }
    
    class _DBJHomeBodyState extends State<DBJHomeBody> {
      int _counter = 0;
    
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          // crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            _getRaiseButton(),
            SizedBox(
              height: 10,
            ),
            Text(
              '开始计数: $_counter,${this.widget.message}',
              style: TextStyle(fontSize: 15, color: Colors.green),
            ),
          ],
        );
      }
    
      Widget _getRaiseButton() {
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
              onPressed: () {
                print('点击加号');
                setState(() {
                  _counter++;
                });
              },
              child: Text(
                '点击+',
                style: TextStyle(color: Colors.white),
              ),
              style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.red)),
            ),
            TextButton(
              onPressed: () {
                print('点击减号');
                setState(() {
                  _counter--;
                });
              },
              child: Text(
                '点击-',
                style: TextStyle(color: Colors.white),
              ),
              style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.green)),
            ),
          ],
        );
      }
    }
    
    
    Simulator Screen Shot - iPhone 12 - 2021-09-02 at 17.13.33.png

    相关文章

      网友评论

          本文标题:计数器

          本文链接:https://www.haomeiwen.com/subject/qyrcwltx.html