android studio 的flutter_view例子

1.下载代码

2.代码说明

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
//代码入口
void main() {
  runApp(FlutterView());
}

//定义个FlutterView 继承StateLessWidget类
class FlutterView extends StatelessWidget {
  //重写Build
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter View',
      theme: ThemeData(
        primarySwatch: Colors.grey,
      ),
      home: MyHomePage(),
    );
  }
}

//定义一个MyHomePage 继承状态类的Widget
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

//定义一个_MyHomePageState记录我的页数
class _MyHomePageState extends State {
  static const String _channel = 'increment';
  static const String _pong = 'pong';
  static const String _emptyMessage = '';
  static const BasicMessageChannel platform =
      BasicMessageChannel(_channel, StringCodec());
  //初始化都是0
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    platform.setMessageHandler(_handlePlatformIncrement);
  }
  //每次点击异步方式 _counter+1
  Future _handlePlatformIncrement(String message) async {
    setState(() {
      _counter++;
    });
    return _emptyMessage;
  }

  void _sendFlutterIncrement() {
    platform.send(_pong);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Center(
              child: Text(
                'Platform button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
                style: const TextStyle(fontSize: 17.0)),
            ),
          ),
          Container(
            padding: const EdgeInsets.only(bottom: 15.0, left: 5.0),
            child: Row(
              children: [
                Image.asset('assets/flutter-mark-square-64.png', scale: 1.5),
                const Text('Flutter', style: TextStyle(fontSize: 30.0)),
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _sendFlutterIncrement,
        child: const Icon(Icons.add),
      ),
    );
  }
}

3.运行效果

这里用命令行运行,可以看到相关信息,也可以通过点击绿色三角运行

 

 flutter run
Using hardware rendering with device AOSP on IA Emulator. If you get
graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib/main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...                                 Running Gradle task 'assembleDebug'... Done                        41.1s
✓ Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...                         2.7s
W/FlutterEngine( 5467): Tried to automatically register plugins with FlutterEngine (io.flutter.embedding.engine.FlutterEngine@9d1d33d) but could not find and invoke the GeneratedPluginRegistrant.
D/FlutterView( 5467): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@9d1d33d
D/EGL_emulation( 5467): eglMakeCurrent: 0xe6e853c0: ver 3 0 (tinfo 0xe6e836d0)
D/eglCodecCommon( 5467): setVertexArrayObject: set vao to 0 (0) 1 0    Syncing files to device AOSP on IA Emulator...                          
 6,754ms (!)                                       

 To hot reload changes while running, press "r". To hot restart (and
rebuild state), press "R".
An Observatory debugger and profiler on AOSP on IA Emulator is
available at: http://127.0.0.1:53729/zb2WBjk1kis=/
For a more detailed help message, press "h". To detach, press "d"; to
quit, press "q".

 

android studio 的flutter_view例子_第1张图片


 

你可能感兴趣的:(Flutter实践大全)