Flutter插件nian_dev_kit的使用方法详解

Flutter插件nian_dev_kit的使用方法详解

念安科技-开发工具

开始使用Flutter插件nian_dev_kit

本项目是一个用于Flutter的插件包起点,它包含针对Android和/或iOS平台的特定实现代码。

对于Flutter开发的帮助,可以查看在线文档,该文档提供了教程、示例、移动开发指南以及完整的API参考。

示例代码

以下是nian_dev_kit插件的基本使用示例。此示例展示了如何在Flutter应用中集成并使用该插件。

import 'dart:io';

import 'package:nian_dev_kit/dev_kit.dart';
import 'package:nian_dev_kit/page/channel_monitor/src/core/devkit_binary_messenger.dart';
import 'package:nian_dev_kit/page/custom_plug/custom_pluggable.dart';
import 'package:nian_dev_kit/utils/app_log_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await logInit();
  
  // 设置透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  runApp(const MyApp());
}

Future logInit() async {
  await KitAppLog.init();
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  // 这个小部件是你的应用的根组件

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this); // 注册监听器
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    return DevKit(
      enable: true,
      pluginsList: [
        CustomPluggable(),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: false,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  String methodChannel = "nian_an/methodChannel";
  
  Future<void> testMethodCall() async {
    // 1. 创建Flutter端的MethodChannel
    MethodChannel method = MethodChannel(
      methodChannel,
      const StandardMethodCodec(),
      DevKitBinaryMessenger.binaryMessenger,
    );
    
    // 2. 通过invokeMethod调用Native方法,拿到返回值
    String debugString = await method.invokeMethod(
        "give_$_counter",
        Platform.isAndroid
            ? "($_counter)give me debug info"
            : "ios_($_counter) give me debug info");
    debugPrint('test debugString=$debugString');
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
    testMethodCall();
    KitAppLog.i(_counter);
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              '你已经点击了按钮次数:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加',
        child: const Icon(Icons.add),
      ),
    );
  }
}

说明

  1. 初始化:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await logInit();
    
      if (Platform.isAndroid) {
        SystemUiOverlayStyle systemUiOverlayStyle =
            const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      }
      runApp(const MyApp());
    }
    
  2. 日志初始化:

    Future logInit() async {
      await KitAppLog.init();
    }
    
  3. 使用DevKit:

    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
      }
    
      @override
      void dispose() {
        super.dispose();
        WidgetsBinding.instance.removeObserver(this);
      }
    
      @override
      Widget build(BuildContext context) {
        return DevKit(
          enable: true,
          pluginsList: [
            CustomPluggable(),
          ],
          child: MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
              useMaterial3: false,
            ),
            home: const MyHomePage(title: 'Flutter Demo Home Page'),
          ),
        );
      }
    }
    
  4. 测试MethodChannel:

    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      String methodChannel = "nian_an/methodChannel";
    
      Future<void> testMethodCall() async {
        MethodChannel method = MethodChannel(
          methodChannel,
          const StandardMethodCodec(),
          DevKitBinaryMessenger.binaryMessenger,
        );
    
        String debugString = await method.invokeMethod(
            "give_$_counter",
            Platform.isAndroid
                ? "($_counter)give me debug info"
                : "ios_($_counter) give me debug info");
        debugPrint('test debugString=$debugString');
      }
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
        testMethodCall();
        KitAppLog.i(_counter);
      }

更多关于Flutter插件nian_dev_kit的使用方法详解的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter插件nian_dev_kit的使用方法详解的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


nian_dev_kit 是一个未知的 Flutter 插件,可能是一个实验性、个人开发或社区贡献的工具包。由于它的名称和功能并不广泛传播,以下是一些探索和使用未知插件的一般步骤:


1. 查找插件的官方文档或源代码

  • 访问 pub.dev 搜索 nian_dev_kit,查看是否有官方发布和文档。
  • 如果 pub.dev 上没有,可以尝试在 GitHub 或其他代码托管平台搜索其源代码。
  • 阅读插件的 README.md 文件,了解其功能、使用方法和示例。

2. 安装插件

pubspec.yaml 中添加插件依赖:

dependencies:
  nian_dev_kit: ^版本号

然后运行 flutter pub get 安装插件。


3. 导入插件

在 Dart 文件中导入插件:

import 'package:nian_dev_kit/nian_dev_kit.dart';

4. 尝试使用插件功能

  • 如果插件提供了示例代码,可以先运行示例,了解其功能。
  • 如果没有示例,可以通过插件暴露的 API 进行尝试。例如:
    // 假设插件提供了一个工具类或方法
    var result = NianDevKit.someFunction();
    print(result);
回到顶部