Discuz!NT|BBS|论坛

注册

 

发新话题 回复该主题

例子程序 [复制链接]

1#
import 'packagelutter/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(
      debugShowCheckedModeBanner: false,
      title: '计数器程序',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyCounterWidget(),
    );
  }
}

class MyCounterWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // 将创建的State返回
    return MyCounterState();
  }
}

class MyCounterState extends State<MyCounterWidget> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar:AppBar(titleext("计数器程序")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  color: Colors.redAccent,
                  child: Text("+1", style: TextStyle(fontSize: 18, color: Colors.white),),
                  onPressed: () {
                    setState(() {
                      counter++;
                    });
                  },
                ),
                RaisedButton(
                  color: Colors.orangeAccent,
                  child: Text("-1", style: TextStyle(fontSize: 18, color: Colors.white),),
                  onPressed: () {
                    setState(() {
                      counter--;

                      if(counter < 0){counter = 0;}
                    });
                  },
                )
              ],
            ),
            Text("$counter", style: TextStyle(fontSize: 30),)
          ],
        ),
      )
    );
  }
}
分享 转发
TOP
发新话题 回复该主题