美文网首页工作生活
flutter-StatelessWidget/Stateful

flutter-StatelessWidget/Stateful

作者: iPhone | 来源:发表于2019-07-01 10:46 被阅读0次
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyText(),
        );
      }
    }
    
    class MyText extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return MyState();
      }
    }
    
    class MyState extends State<MyText>{
      var _count = 0;
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Scaffold(
          appBar: AppBar(title: Text('state'),),
          body: Stack(
            children: <Widget>[
              Align(
                child: Text('这是---$_count'),
                alignment: FractionalOffset(0.1, 0.1),
              ),
              Align(
                child: RaisedButton(onPressed: (){
                  setState(() {
                    _count++;
                  });
                },
                child: Text('累计'),
                ),
              )
            ],
          ),
        );
      }
    }
    

    相关文章

      网友评论

        本文标题:flutter-StatelessWidget/Stateful

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