【问题标题】:How can I parse the scanned result data received from my barcode scanner to a new screen or page in flutter如何将从我的条码扫描仪接收到的扫描结果数据解析到新的屏幕或页面中
【发布时间】:2020-06-19 18:31:39
【问题描述】:

这是因为我正在尝试实现一个条码阅读器来扫描员工身份证。所以,我想要实现的是当分配给扫描卡的代理执行扫描时,它应该在另一个页面中显示扫描结果,而不是同一个屏幕或小部件。

简单来说,就像您拥有待办事项列表,当您单击待办事项时,它会找到待办事项 ID 并带您进入待办事项详细信息页面。这意味着指向员工详细信息页面的链接将被编码到条形码中,因此当您扫描时,它会将您带到员工详细信息页面。但我不知道该怎么做。

这就是我在下面所做的。这可以很好地执行扫描,但显示结果是问题。

import 'dart:async';
import 'package:erg_app/ScanPage.dart';
import 'package:erg_app/eops.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:erg_app/Animations/FadeAnimation.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: StartScanPage(),
    )
  );
}

class StartScanPage extends StatefulWidget {
  @override
  StartScanPageState createState() {
      return StartScanPageState();
  }
}

  class StartScanPageState extends State<StartScanPage>  {

  String result = "Bio Data:";

  Future _scanQR() async {
    try {
      String qrResult = await BarcodeScanner.scan();

      setState(() {
        result = qrResult; 
      });
    } on PlatformException catch (ex) {
      if (ex.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          result = "Camera permission was denied";
        });
      } else {
        setState(() {
          result = "Unknown Error $ex";
        });
      }
    } on FormatException {
      setState(() {
        result = "You pressed the back button before scanning anything";
      });
    } catch (ex) {
      setState(() {
        result = "Unknown Error $ex";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          width: double.infinity,
          height: MediaQuery.of(context).size.height,
          padding: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Column(
                children: <Widget>[
                  FadeAnimation(1, Text("Verify Farmer", 
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 30
                  ),)),
                  SizedBox(height: 20,),
                  FadeAnimation(1.2, Text("Automatic identity verification which enables you to verify each farmer applicant", 
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 15
                  ),)),
                ],
              ),
              FadeAnimation(1.4, Container(
                height: MediaQuery.of(context).size.height / 3,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/illustration1.png')
                  )
                ),
              )),
              Column(
                children: <Widget>[
                  FadeAnimation(1.5, MaterialButton(
                    color: Colors.green,
                    minWidth: double.infinity,
                    height: 60,
                    onPressed: _scanQR,
                    // () {
                    //   Navigator.push(context, MaterialPageRoute(builder: (context) => ScanPage()));
                    // },
                    shape: RoundedRectangleBorder(
                      side: BorderSide(
                        color: Colors.green
                      ),
                      borderRadius: BorderRadius.circular(50)
                    ),
                    child: Text("Start Scan", style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w600, 
                      fontSize: 18
                    ),),
                  )),
                  SizedBox(height: 20,),
                  FadeAnimation(1.6, Container(
                    // padding: EdgeInsets.only(top: 3, left: 3),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50),
                      border: Border(
                        bottom: BorderSide(color: Colors.green),
                        top: BorderSide(color: Colors.green),
                        left: BorderSide(color: Colors.green),
                        right: BorderSide(color: Colors.green),
                      )
                    ),
                    child: MaterialButton(
                      minWidth: double.infinity,
                      height: 60,
                      onPressed: () {
                        Navigator.push(context, MaterialPageRoute(builder: (context) => EopPage()));
                      },
                      color: Colors.white,
                      elevation: 0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50)
                      ),
                      child: Text("Cancel", style: TextStyle(
                        // color: Colors.white,
                        fontWeight: FontWeight.w600, 
                        fontSize: 18
                       ), ),
                    ),
                  ))
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

我想在这里显示结果:

import 'package:flutter/material.dart';
import 'package:erg_app/CaptureInputsPage.dart';

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

class ProfilePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
       debugShowCheckedModeBanner: false,
      home: Scaffold(
         appBar: AppBar(
          title: new Center(child: Text('Farmers Data', textAlign: TextAlign.left)),
          iconTheme: IconThemeData(color: Colors.white),
          backgroundColor: Colors.green, 
          leading: IconButton(
          icon: Icon(Icons.assignment_ind),
          onPressed: () {},
        ),

        ),
        body: Container(
          child: ListView(
              children: <Widget>[
                Container(margin: EdgeInsets.only(top: 20),),
                CircleAvatar(
                  radius: 80,
                  backgroundColor: Colors.grey,
                ),
                Text(
                  'Yusuf Danladi',
                  style: TextStyle(
                    fontFamily: 'SourceSansPro',
                    fontSize: 25,
                  ),
                  textAlign: TextAlign.center,
                ),
                Text(
                  'Welcome',
                  style: TextStyle(
                    fontSize: 20,
                    fontFamily: 'SourceSansPro',
                    color: Colors.green[400],
                    letterSpacing: 2.5,
                  ),
                 textAlign: TextAlign.center,
                ),
                Container(margin: EdgeInsets.only(top: 20),),
                SizedBox(
                  height: 20.0,
                  width: 200,
                  child: Divider(
                    color: Colors.teal[100],
                  ),
                ),
                Text(
                  'Farmer Details',
                  textAlign: TextAlign.center,
                ),
                Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'Phone:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '+234 801 000 4504',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'BVN:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '22108691911',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'Appicant ID:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          'ERG-108691911',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'State/LGA:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          'Taraba/Ussa',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'Farm Size:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '5000 meter sq',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Card(
                      color: Colors.white,
                      margin:
                          EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                            'Geo Coord.:',
                            style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'SourceSansPro',
                              color: Colors.green[700],
                              letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '012350007, 038245543',
                          style:
                              TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
                        ), 
                      ), 
                    ),

                    Container(
                      margin: EdgeInsets.only(top: 20, bottom: 30),
                      child: Center(
                        child: RaisedButton(
                          padding: EdgeInsets.fromLTRB(80, 10, 80, 10),
                          color: Colors.green,
                          child: Text("Complete", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14), ),
                          onPressed: () {
                            Navigator.of(context).push(MaterialPageRoute(builder: (context) => CaptureInputsPage()));
                          },
                          shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(50),
                        ),
                        ),
                      ),
                  ),
              ],
          ),
        ), 
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies


    【解决方案1】:

    从二维码得到结果后,你可以简单地使用MaterialPageRoute。

      Future _scanQR() async {
        try {
          String qrResult = await BarcodeScanner.scan();
    
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => ProfilePage(qrResult),
            ),
          );
        } on PlatformException catch (ex) {
          ....
        }
      }
    

    将 ProfilePage 创建为

    class ProfilePage extends StatelessWidget {
      final String result;
    
      ProfilePage(this.result);
    
      ....
    }
    

    【讨论】:

    • 非常非常非常非常非常非常感谢@prasad banker 和 Federick Jonathan。我唯一添加的是 final String 结果; ProfilePage({this.result}); 但它的工作谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2020-12-03
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多