Flutter:如何比较Material Button内部的两个字符串?

问题描述

其他更新

由于Shri Hari,我能够在begin @product = Product.find(params[:id]) rescue => e redirect_to root_path end 调用void函数,因此不必再调用class函数。再次感谢大家到目前为止对我的帮助。

其他更新的结尾

更新

谢谢大家的有用评论!不幸的是,我仍然有以下问题:

enter image description here

我想知道此问题是否与下面所述的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()

这就是我看到的:

enter image description here

任何帮助将不胜感激。

解决方法

步骤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");
  }
}
,
  1. = 是分配运算符,而 == 检查两个对象是否相等,即在您的情况下两个字符串是否相等。只需使用iconCode == 'CAMERA'

  2. (上下文)未正确关闭。 使用:Navigator.of(context).pushNamed()

代码

MaterialButton(onPressed: () {
          iconCode == 'CAMERA'
              ? _takePhoto()
              : Navigator.of(context).pushNamed('/$iconNAme');
          setState(() {});
        }),