美文网首页Flutter
Flutter Example 有状态组件

Flutter Example 有状态组件

作者: 三只仓鼠 | 来源:发表于2018-11-14 17:31 被阅读0次
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return new MaterialApp(
          home: MyButton(),
        );
      }
    }
    
    class MyButton extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => MyButtonState();
    }
    
    class MyButtonState extends State<MyButton> {
      int counter = 0;
      List<String> strs = ['测试', '的', '描述', '文字'];
      String displayedString = "初始化文字...";
      //更改属性
      void onPressOfButton() {
        setState(() {
          displayedString = strs[counter];
          counter = counter < 3 ? counter + 1 : 0;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Center(child: new Text("Stateful Widget")),
            backgroundColor: Colors.green,
          ),
          body: new Container(
            child: new Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(displayedString, style: new TextStyle(fontSize: 40.0)),
                  new Padding(padding: new EdgeInsets.all(10.0)),
                  new RaisedButton(
                    child: new Text(
                      "Press",
                      style: TextStyle(color: Colors.white),
                    ),
                    color: Colors.red,
                    onPressed: onPressOfButton,
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    
    

    相关文章

      网友评论

        本文标题:Flutter Example 有状态组件

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