【问题标题】:Flutter - how to add a widget to an Android viewFlutter - 如何将小部件添加到 Android 视图
【发布时间】:2020-06-29 10:34:59
【问题描述】:

是否可以将 Flutter 小部件或 UI 元素添加到 Android 视图?

我正在开发一个 Android SDK,我只使用视图而不是活动或片段

我在 View.OnAttachStateChangeListener 中获取视图实例:

override fun onViewAttachedToWindow(view: View) {
   
}

override fun onViewDetachedFromWindow(view: View) {
   
}

视图布局是简单的LinearLeayoutFrameLayout

我想在项目中添加一个 Flutter .arr 并在视图中显示一个小部件。此外,我想要小部件附带的所有路线和 UI。

例子,在视图中而不是在活动中显示Flutter的基本示例,可以吗?

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 press Run > Flutter 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, 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:',
            ),
            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.
    );
  }
}

【问题讨论】:

    标签: android flutter flutter-layout android-view


    【解决方案1】:

    我认为你不能只将一个 Widget 或任何来自 Flutter SDK 的 UI 元素添加到 Android 视图中。 Flutter Widget 需要 Flutter 上下文和 Flutter 引擎才能工作。

    但是,你可以在安卓原生应用https://flutter.dev/docs/development/add-to-app中启动 Flutter View

    【讨论】:

    • 所以你是说我可以触发一个颤动视图以从不在活动或片段内的本机代码显示?
    • 阅读手册和一些博客,看来你可以: - 打开一个新的颤动活动 - 打开/集成颤动片段 - 执行 dart 代码以运行一些功能/方法 你不能在没有 Flutter UI 的情况下使用特定的活动或片段。
    • 就像我在问题中所说的那样,不幸的是,我没有活动实例。我确实有一个服务上下文。 FlutterView 可以像覆盖窗口(例如 Facebook 气泡)一样启动/触发吗?
    • 我认为这不可能
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    相关资源
    最近更新 更多