Flutter 有状态Hello world程序

Flutter有状态Hello world程序:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyHomeState();
  }
}

class MyHomeState extends State<MyHomePage> {
  var count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('AppBar title')),
      body: Center(
        child: Column(
          children: [
            Text('hello $count'),
            MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                child: Text('点我'),
                onPressed: () {
                  setState(() {
                    this.count += 1;
                  });
                })
          ],
        ),
      ),
    );
  }
}



标签: 、面试
  • 回复
隐藏