美文网首页
Flutter开发体验-新建项目到运行项目

Flutter开发体验-新建项目到运行项目

作者: shiyueZ | 来源:发表于2021-06-15 23:52 被阅读0次

    准备工作

    开发环境:

    设备:mac电脑
    开发工具:Visual Studio Code 版本1.57.0
    其他工具:Android Studio、Xcode、Chrome
    插件:Visual Studio Code安装Flutter插件,Flutter使用的是Dart语言,安装了Flutter插件,自带就下载了Dart插件

    接下来,开始体验Flutter的开发之旅吧!

    新建项目

    快捷键:shift+comman+P,新建flutter项目


    新建项目

    目录结构看下图:

    目录结构

    作为初学者,目前我们主要看这两个文件,main.dart和pubspec.yaml,main.dart是项目的初始化入口,根组件设置主要在这个文件里,体验flutter开发的代码主要在这里写;pubspec.yaml主要是flutter开发的配置文件,里面包含项目的信息、SDK版本、第三方package依赖,以后的开发过程中,我们肯定会用到很多第三方的组件,这里添加第三方组件也很简单,如下图:

    配置文件
    在dependencies下添加第三方组件,cupertino_icons就是新建flutter项目时自带的一个第三方组件,后面是它的版本号,这里版本号有不同的设置方法,后面另说。

    现在来说说main.dart文件的代码结构:

    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(
            // 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,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key? key, required this.title}) : super(key: key);
    
      // This widget is the home page of your application. It is stateful, meaning
      // that it has a State object (defined below) that contains fields that affect
      // how it looks.
    
      // This class is the configuration for the state. It holds the values (in this
      // case the title) provided by the parent (in this case the App widget) and
      // used by the build method of the State. Fields in a Widget subclass are
      // always marked "final".
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          // This call to setState tells the Flutter framework that something has
          // changed in this State, which causes it to rerun the build method below
          // so that the display can reflect the updated values. If we changed
          // _counter without calling setState(), then the build method would not be
          // called again, and so nothing would appear to happen.
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: Center(
            // Center is a layout widget. It takes a single child and positions it
            // in the middle of the parent.
            child: Column(
              // Column is also a layout widget. It takes a list of children and
              // arranges them vertically. By default, it sizes itself to fit its
              // children horizontally, and tries to be as tall as its parent.
              //
              // Invoke "debug painting" (press "p" in the console, choose the
              // "Toggle Debug Paint" action from the Flutter Inspector in Android
              // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
              // to see the wireframe for each widget.
              //
              // Column has various properties to control how it sizes itself and
              // how it positions its children. Here we use mainAxisAlignment to
              // center the children vertically; the main axis here is the vertical
              // axis because Columns are vertical (the cross axis would be
              // horizontal).
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:123',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
    

    void main() {}是项目初始化的主入口
    class MyApp extends StatelessWidget {}是自定义的一个无状态组件,默认会有一个build方法;
    class MyHomePage extends StatefulWidget {}是自定义的一个有状态的组件,默认会有class _MyHomePageState extends State<MyHomePage> {}State类。

    运行

    了解完上面的基本结构,现在我们看看如何运行代码,如下图:

    运行方法
    选择设备
    运行结果:
    运行结果
    现在,我们可以体验一下flutter的另一个高效开发的技能:热重载,随意修改代码中文本上的内容,保存就可以看到页面上已动态刷新的内容。

    结语

    好了,以上就是从新建项目到运行第一个项目的过程,如有问题,欢迎指正哦!

    题外话:虽然网上教程很多,写的比我全面以及好的多,但个人觉得有古话说的好:好记性不如烂笔头,以自己的理解记录下学习的过程是一个很好的学习方法,若有问题,欢迎指正,必认真修改!愿与大家共勉~

    参考:
    https://flutterchina.club/get-started/codelab/

    相关文章

      网友评论

          本文标题:Flutter开发体验-新建项目到运行项目

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