工作生活

flutter-StatelessWidget/Stateful

2019-07-01  本文已影响0人  iPhone
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('累计'),
            ),
          )
        ],
      ),
    );
  }
}
上一篇下一篇

猜你喜欢

热点阅读