• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In the next few moments I will share two daily tips for flutter development

How do I make text in Expanded scrollable in a Flutter

I was busy building an application with flash cards that contained information about them, but I ran into a stupid problem THAT I couldn’t seem to figure out.

To give context, the card widget currently looks like this:

As you can see, the text is cut off. I tried changing Expanded to SingleChildScrollView, but eventually I ran into this situation:

I know the solution may be simple, but I’ve been working on it for hours

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      padding: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
      decoration: BoxDecoration(
        color: Colors.indigo,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Image.asset(
                getCategoryImage(),
                width: 50
              ),
              Container(width: 10),
              Text(
                title,
                style: TextStyle(
                  fontFamily: "Estherilla",
                  fontSize: 30,
                  color: Colors.white
                ),
              )
            ],
          ),
          SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 25),
              child: Text(
                content,
                style: TextStyle(
                  fontSize: 25,
                  color: Colors.white
                ),
              ),
            )
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Text(
                "#$author",
                style: TextStyle(
                  fontFamily: "Estherilla",
                  color: Colors.white,
                  fontSize: 20() [() [() [() [() }Copy the code

The best answer

Wrap your SingleChildScrollView inside Expanded widgets like this one

Column( ... Expanded( child: SingleChildScrollView(...) ),...).Copy the code

Scan the bar code to open the link

I have a quick question about qr code scanning in Flutter. How to open the WEBSITE after the two-dimensional code reads the data successfully?

I use this package to use the QR code, which is used to open a URL, which is my function to check if the data value from the QR code is a URL, and if it is a URL, run the function to open the website.

checkingValue() {
    if(_result ! =null|| _result ! ="") {
      if (_result.contains("https") || _result.contains("http")) {
        return _launchURL(_result);
      } else {
        Toast.show("Invalide URL", context, duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM); }}else {
      return null;
    }
  }

  _launchURL(String urlQRCode) async {
    String url = urlQRCode;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url'; }}Copy the code

The _result variable is a string rid of the value of the QR code data.

Here’s my entire code:

class _ScannerPageState extends State<ScannerPage> {

  String _password;
  String _result = "";

  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";
          Toast.show(_result, context,
              duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
        });
      } else {
        setState(() {
          _result = "Unknown Error $ex";
          Toast.show(_result, context, duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM); }); }}on FormatException {
      setState(() {
        _result = "You pressed the back button before scanning anything";
        Toast.show(_result, context,
            duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
      });
    } catch (ex) {
      setState(() {
        _result = "Unknown Error $ex";
        Toast.show(_result, context, duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM); }); }}@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        return showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return PopUp(
                content: "Are you sure want to exit?",
                cancelText: "No",
                acceptText: "Yes",
                onTapCancel: () => Navigator.of(context).pop(),
                onTapAccept: () async {
                  await SessionManager().removeSession();//
                  awaitSystemNavigator.pop(); }); }); }, child: Scaffold( appBar: AppBar( title: Text(widget.title), actions: <Widget>[ IconButton( icon: Icon(Icons.lock), onPressed: () { Navigator.pushNamed(context,'/login');
              },
            ),
          ],
        ),
        body: Column(
          children: <Widget>[
            Text(_result.contains("https") || _result.contains("http")? _result :"Invalid URL"),
          ],
        ),
        floatingActionButton: FloatingActionButton.extended(
          icon: Icon(Icons.camera_alt),
          label: Text("Scan"),
          onPressed: () => _scanQR(),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      ),
    );
  }

  checkingValue() {
    if(_result ! =null|| _result ! ="") {
      if (_result.contains("https") || _result.contains("http")) {
        return _launchURL(_result);
      } else {
        Toast.show("Invalide URL", context, duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM); }}else {
      return null;
    }
  }

  _launchURL(String urlQRCode) async {
    String url = urlQRCode;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url'; }}}Copy the code

So, where do I put the ability to run checkingValue() after scanning the QR code?

Pass qrResult to the checkingValue method

Future _scanQR() async {
    try {
      String qrResult = await BarcodeScanner.scan();
      checkingValue(qrResult);
     //....
    }
Copy the code

CheckingValue method

checkingValue(String url) {
    / /...
}
Copy the code

And checkingValue (after)

setState(() {_result = qrResult; }); checkingValue();/ /...
Copy the code