如何在Flutter中使Expanded中的文本可滚动
我正在忙着用闪存卡构建一个应用程序,其中包含有关它们的信息,但我遇到了一个我似乎无法弄清楚的愚蠢问题。
为了给出上下文,卡片小部件目前如下所示:
如您所见,文本被切断了。我试过将 Expanded 更改为 SingleChildScrollView,但最终我遇到了这种情况:
我知道解决方案可能很简单,但我已经摆弄了几个小时
@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 ), ) ], ) ], ), ); } 复制代码
最佳答案
包裹您的 SingleChildScrollView
里面Expanded
像这样的小部件
Column( ... Expanded( child: SingleChildScrollView(...) ), ... ) 复制代码
扫描条形码打开链接
我有一个关于 Flutter 中二维码扫描的小问题。二维码读取数据成功后如何打开网址?
我使用这个包来使用 QR 码,这用于打开一个 URL,这是我检查来自 QR 码的数据值是否为 URL 的功能,如果它是 URL,则运行该功能以打开网站。
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'; } } 复制代码
该_result
变量是一个字符串摆脱QR码数据的值。
这是我的全部代码:
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();// await SystemNavigator.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'; } } } 复制代码
那么,我把checkingValue()
扫描二维码后运行它的功能放在哪里?
递qrResult
给checkingValue
方法
Future _scanQR() async { try { String qrResult = await BarcodeScanner.scan(); checkingValue(qrResult); //.... } 复制代码
checkingValue
方法
checkingValue(String url) { //... } 复制代码
或checkingValue()
后
setState(() {_result = qrResult;}); checkingValue(); //...