Flutter生物识别插件bio_flutter的使用

Flutter生物识别插件bio_flutter的使用

特性

小部件

  • 投影数据可视化器

投影数据可视化器

生物数据类

  • 蛋白质
  • 序列:氨基酸序列、核苷酸序列
  • 蛋白质-蛋白质相互作用
  • 分类学

蛋白质表示和数据分析

  • 嵌入:每个序列、每个残基
  • UMAP

文件处理

名称 fasta csv json
蛋白质
蛋白质-蛋白质相互作用
嵌入
投影数据
自定义属性

使用

加载和写入蛋白质fasta文件

import 'package:flutter_test/flutter_test.dart';

Future<List<Protein>> readFastaFileProtein(String pathToFile) async {
  BioFileHandlerContext<Protein>? handler = BioFileHandler<Protein>().create(pathToFile);
  Map<String, Protein> proteins = await handler.read();
  return proteins.values.toList();
}

Future<void> saveProteins(List<Protein> proteins, String pathToFile) async {
  // 在桌面/移动设备上保存文件,在web上下载
  BioFileHandlerContext<Protein>? handler = BioFileHandler<Protein>().create(pathToFile);
  await handler.write(proteins.asMap().map((_, value) => MapEntry(value.id, value)));
}

添加自定义属性到蛋白质

import 'package:flutter_test/flutter_test.dart';

Future<List<Protein>> addCustomAttributes(List<Protein> proteins, String pathToFile) async {
  BioFileHandlerContext<CustomAttributes>? handler = BioFileHandler<CustomAttributes>().create(pathToFile);
  Map<String, CustomAttributes> attributes = await handler.read();
  List<Protein> updatedProteins = [];
  for (Protein protein in proteins) {
    if (attributes.containsKey(protein.id)) {
      Protein updated = protein.updateFromCustomAttributes(attributes[protein.id]!);
      updatedProteins.add(updated);
    }
  }
  return updatedProteins;
}

创建一个带有随机坐标的UMAP可视化小部件

Widget createProjectionWidget(List<ProteinProteinInteraction> interactionData) {
  return ProjectionVisualizer(
      projectionData: ProjectionData.random(interactionData.length),
      pointIdentifierKey: "id",
      pointData: interactionData.map((interaction) => interaction.toMap()).toList()); // 也适用于蛋白质数据
}

额外信息

该包的重点在于提供的操作的正确性,以便对科学家和研究人员在研究和发展中有所帮助。我们非常感谢在GitHub上报告的每一个错误和对代码库的贡献!

当前路线图

  • 提高测试覆盖率
  • 改进文档
  • 支持更多文件格式用于所有数据类型
  • 添加三维蛋白质可视化

完整示例代码

以下是一个完整的示例代码,展示了如何使用bio_flutter插件。

import 'package:bio_flutter/bio_flutter.dart';
import 'package:flutter/material.dart';

List<Protein> sampleData() {
  Taxonomy human = const Taxonomy(id: 9606, name: "Homo sapiens", family: "Hominidae");
  Taxonomy lyssaVirus = const Taxonomy(id: 11286, name: "Lyssavirus", family: "Rhabdoviridae");
  return [
    Protein("Seq1", sequence: Sequence.buildVerifiedFromString("SEQWENCE")!, taxonomy: human),
    Protein("Seq2", sequence: Sequence.buildVerifiedFromString("PRTEIN")!, taxonomy: lyssaVirus),
    Protein("Seq3", sequence: Sequence.buildVerifiedFromString("SEQVENCEPRTEI")!, taxonomy: human),
    Protein("Seq4", sequence: Sequence.buildVerifiedFromString("SEQ")!, taxonomy: lyssaVirus),
    Protein("Seq5", sequence: Sequence.buildVerifiedFromString("PRTEINSEQWENCE")!, taxonomy: lyssaVirus)
  ];
}

void main() {
  final List<Protein> proteinExampleData = sampleData();
  runApp(BioFlutterUMAPExample(
    proteinExampleData: proteinExampleData,
  ));
}

class BioFlutterUMAPExample extends StatelessWidget {
  final List<Protein> proteinExampleData;

  const BioFlutterUMAPExample({super.key, required this.proteinExampleData});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'bio_flutter UMAP Example',
      theme: ThemeData(
        useMaterial3: false,
        primaryColor: const Color(0xFF321F5D),
        fontFamily: 'Georgia',
      ),
      home: UMAPViewerPage(proteinExampleData: proteinExampleData),
    );
  }
}

class UMAPViewerPage extends StatelessWidget {
  final List<Protein> proteinExampleData;

  const UMAPViewerPage({super.key, required this.proteinExampleData});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Center(
        child: ProjectionVisualizer2D(
      projectionData: ProjectionData.random(proteinExampleData.length, 2),
      pointData: proteinExampleData
          .map((protein) =>
              Map.fromEntries(protein.toMap().entries.map((entry) => MapEntry(entry.key, entry.value.toString()))))
          .toList(),
      pointIdentifierKey: "id",
    ));
  }
}

更多关于Flutter生物识别插件bio_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter生物识别插件bio_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用bio_flutter插件进行生物识别(如指纹或面部识别)的示例代码。bio_flutter插件允许你访问设备的生物识别功能,但请注意,具体实现可能会因设备和操作系统版本的不同而有所差异。

首先,你需要在pubspec.yaml文件中添加bio_flutter依赖:

dependencies:
  flutter:
    sdk: flutter
  bio_flutter: ^x.y.z  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来,是一个简单的示例,展示了如何使用bio_flutter插件进行生物识别认证:

import 'package:flutter/material.dart';
import 'package:bio_flutter/bio_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bio Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BioAuthenticationScreen(),
    );
  }
}

class BioAuthenticationScreen extends StatefulWidget {
  @override
  _BioAuthenticationScreenState createState() => _BioAuthenticationScreenState();
}

class _BioAuthenticationScreenState extends State<BioAuthenticationScreen> {
  late BioAuth _bioAuth;
  bool _isAuthenticated = false;

  @override
  void initState() {
    super.initState();
    _bioAuth = BioAuth();
    _checkBioAuthAvailability();
  }

  Future<void> _checkBioAuthAvailability() async {
    bool isAvailable = await _bioAuth.canAuthenticate;
    if (isAvailable) {
      print("Biometric authentication is available.");
    } else {
      print("Biometric authentication is NOT available.");
    }
  }

  Future<void> _authenticateUser() async {
    try {
      bool isAuthenticated = await _bioAuth.authenticate(
        localizedReason: 'Please authenticate to continue',
        stickyAuth: true,
      );
      setState(() {
        _isAuthenticated = isAuthenticated;
      });
      if (isAuthenticated) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Authenticated successfully!')),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Authentication failed!')),
        );
      }
    } catch (e) {
      print("Authentication error: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('An error occurred during authentication.')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bio Flutter Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              _isAuthenticated ? 'Authenticated' : 'Not Authenticated',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _authenticateUser,
              child: Text('Authenticate'),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们首先导入了bio_flutter包。
  2. 创建了一个简单的Flutter应用,其中包含一个BioAuthenticationScreen,它检查生物识别功能的可用性并尝试进行用户认证。
  3. _checkBioAuthAvailability方法中,我们检查设备是否支持生物识别。
  4. _authenticateUser方法中,我们尝试使用生物识别进行用户认证,并根据认证结果更新UI。

请注意,localizedReason参数用于在生物识别对话框中显示给用户的信息,而stickyAuth参数决定了认证成功后是否保持认证状态(即用户不需要在短时间内重复认证)。

此外,请确保在Android和iOS项目中正确配置生物识别权限和设置。这通常涉及在AndroidManifest.xmlInfo.plist文件中添加必要的权限和配置。具体配置请参考bio_flutter的官方文档。

回到顶部