0

I'm learning how to use dart.I created a simple application that scans the qrCodes, and displays the text. My question is: How do I open the text as a link? Since the url is displayed to me as text.

Currently the application works without errors, but I would like to create a tap to open the links in the browser. This is my main.dart

 void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _MyAppState();
  }

  class _MyAppState extends State<MyApp> {
  GlobalKey qrKey =GlobalKey();
  var qrText = "";
  QRViewController controller;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(body: Column(children: [

      Expanded(
      flex:5,
      child: QRView(key: qrKey,
      overlay: QrScannerOverlayShape(
        borderRadius:10,
        borderColor: Colors.red,
        borderLength: 30,
        borderWidth: 10,
        cutOutSize:300

      ),
      onQRViewCreated: _onQRViewCreate,)
          ),
    Expanded(
    flex: 1,
    child: Center(
    child: Text('Scan result: $qrText'),



     ),
    )
       ],
      ),
    );
  }

  @override
    void dispose() {
    controller?.dispose();
    super.dispose();
  }



  void _onQRViewCreate(QRViewController controller ) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        qrText = scanData;
      });
    });
  }

}

3 Answers3

0

use url_launcher to open the link

eg code

class _MyAppState extends State<MyApp> {
  GlobalKey qrKey = GlobalKey();
  var qrText = "";
  QRViewController controller;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Column(
        children: [
          Expanded(
              flex: 5,
              child: QRView(
                key: qrKey,
                overlay: QrScannerOverlayShape(
                    borderRadius: 10,
                    borderColor: Colors.red,
                    borderLength: 30,
                    borderWidth: 10,
                    cutOutSize: 300),
                onQRViewCreated: _onQRViewCreate,
              )),
          Expanded(
            flex: 1,
            child: Center(
              child: InkWell(
                child: Text(
                  'Scan result: $qrText',
                ),
                onTap: () {
                  if (qrText.isNotEmpty) {
                    launch(qrText);
                  }
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  void _onQRViewCreate(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        qrText = scanData;
      });
    });
  }
}
Mr Random
  • 1,992
  • 7
  • 20
0

You should split the Text into two TextComponents within a row and add a GestureDetector to handle the tap on the link. Add blue styling and underline if you want it to look like a link.

Row(children:[Text('Scan result: '), GestureDetector(onPressed: () async { await launch(qrText); }, child:Text(qrText, style=...))])

"launch" is explained here: How do I open a web browser (URL) from my Flutter code?

Seth Kitchen
  • 1,526
  • 19
  • 53
-1
import 'dart:html';
window.open('https://merchant.razorpay.com/policy/LazhrlAH8gPfi9/refund',"new tab");
Anks
  • 11
  • 2