【问题标题】:Transform Flutter Barcode Result single line into list将 Flutter Barcode Result 单行转换为列表
【发布时间】:2021-01-17 18:27:45
【问题描述】:

我有这样的代码

return DefaultTabController(
  length: 4,
  child: Scaffold(
      appBar: AppBar(
        title: Text("Halaman Dashboard"),
        actions: <Widget>[
          IconButton(
            onPressed: () {
              signOut();
            },
            icon: Icon(Icons.lock_open),
          )
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Scan'),
              onPressed: () async {
                try {
                  String barcode = await BarcodeScanner.scan();
                  setState(() {
                    this.barcode = barcode;
                  });
                } on PlatformException catch (error) {
                  if (error.code == BarcodeScanner.CameraAccessDenied) {
                    setState(() {
                      this.barcode =
                          'Izin kamera tidak diizinkan oleh si pengguna';
                    });
                  } else {
                    setState(() {
                      this.barcode = 'Error: $error';
                    });
                  }
                }
              },
            ),
            Text(
              'Result: $barcode', //THIS 
              textAlign: TextAlign.center,
            ),
          ],
        ),
      )),
);

带注释的变量$barcode //THIS有一个类似“abc; def; ghi; ..”的值,该值显示在一行中,我如何在列表中显示该值

名称:ABC

地址:def

电话:ghi ?

【问题讨论】:

  • 谢谢你的回答,对不起,你能举个例子吗?

标签: flutter dart


【解决方案1】:

使用拆分。

了解更多详情 https://stackoverflow.com/a/55358328/11794336

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String example = 'abc; def; ghi;';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ListView(
          children: example
              .split(';')                       // split the text into an array
              .map((String text) => Text(text)) // put the text inside a widget
              .toList(),                        // convert the iterable to a list
        )
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2019-08-07
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2021-12-31
    • 2021-04-11
    相关资源
    最近更新 更多