如何解决Flutter:如何比较Material Button内部的两个字符串?
其他更新
由于Shri Hari,我能够在begin
@product = Product.find(params[:id])
rescue => e
redirect_to root_path
end
中调用void
函数,因此不必再调用class
函数。再次感谢大家到目前为止对我的帮助。
其他更新的结尾
更新
谢谢大家的有用评论!不幸的是,我仍然有以下问题:
我想知道此问题是否与下面所述的void
函数有关:
void
再次感谢您的时间和帮助。我真的很感激。
(顺便说一句,我确保包含 void _takePhoto() async {
// final _picker = ImagePicker();
// PickedFile pickedFile = await _picker.getimage(source: ImageSource.camera);
// final File file = File(pickedFile.path);
ImagePicker.pickImage(source: ImageSource.camera)
.then((File recordedImage) {
if (recordedImage != null && recordedImage.path != null) {
gallerySaver.saveImage(recordedImage.path,albumName: albumName)
.then((bool success) {});
}
});
}
的括号,但由于MaterialButton
内的内容太多而无法全部显示,因此它并未显示在屏幕上在一起。)
更新结束
我有一个名为MaterialButton
的{{1}}函数和其他类。如果我的void
等于_takePhoto()
,我想访问String iconCode
,而其他String 'CAMERA'
则访问其他班级。但是,我很难用字符串编写功能性的if-else语句。这是我写的:
_takePhoto()
任何帮助将不胜感激。
解决方法
步骤1:您需要==
来比较希望具有条件的字符串。
步骤2:of(context
,还需要一个右括号)
。
代码段供您参考
MaterialButton(onPressed: () {
iconCode == 'CAMERA'
? _takePhoto()
: Navigator.of(context).pushNamed('/$iconNAme');
setState(() {});
}),
完整的测试代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),initialRoute: '/',routes: {
'/': (context) => MyHomePage(title: "test",'/second': (context) => iconName(),},);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
String iconCode = 'CAMERA1';
String iconNAme = "second";
void _takePhoto() {
print("take photo");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
MaterialButton(
color: Colors.blue,onPressed: () {
iconCode == 'CAMERA'
? _takePhoto()
: Navigator.of(context).pushNamed('/$iconNAme');
setState(() {});
}),Text(
'You have pushed the button this many times:',Text(
'$_counter',style: Theme.of(context).textTheme.headline4,],floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),);
}
}
class iconName extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("iconName");
}
}
,
-
= 是分配运算符,而 == 检查两个对象是否相等,即在您的情况下两个字符串是否相等。只需使用iconCode == 'CAMERA'
-
(上下文)未正确关闭。 使用:
Navigator.of(context).pushNamed()
代码
MaterialButton(onPressed: () {
iconCode == 'CAMERA'
? _takePhoto()
: Navigator.of(context).pushNamed('/$iconNAme');
setState(() {});
}),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。