【问题标题】:Flutter - multiply the values ​from different text fieldsFlutter - 将来自不同文本字段的值相乘
【发布时间】:2020-11-30 12:02:32
【问题描述】:

我需要你的帮助,

我想将不同文本字段的值相乘。我面临*不能与TexteditingController()一起使用的问题:

void _calculation() {
    _volume = lenCon * widCon * higCon;
    print(_volume);
  }

我是否需要将 lenCon 之类的变量转换为之前的两倍?我怎样才能做到这一点? var lenConInt = double.parse(lenCon); 不起作用。

RaisedButton 必须执行_calculation 并使用正确的值更改文本。我需要一个 StatlessWidget 还是 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,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _volume = 0;

//  var _lenght;
//  var _width;
//  var _hight;

  void _calculation() {
    _volume = lenCon * widCon * higCon;
    print(_volume);
  }

  final lenCon = new TextEditingController();
  final widCon = new TextEditingController();
  final higCon = new TextEditingController();

  // var lenConInt = double.parse(lenCon);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: lenCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Länge',
                ),
              ),
              TextField(
                controller: widCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Breite',
                ),
              ),
              TextField(
                controller: higCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Höhe',
                ),
              ),
              RaisedButton(
                onPressed: (_calculation),
                child: Text('Berechnen'),
              ),
              Text('Your Volume is: $_volume'),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • try var lenConDouble = double.parse(lenCon.text); .text 从 TextEditingController flutter.dev/docs/cookbook/forms/retrieve-input 检索输入的值
  • 我建议您阅读 Stateless Widget 和 Stateful Widget 之间的区别;)
  • @sonic 在这里使用有状态小部件是正确的,因为他想在计算后更新最后一个 Text 小部件。但是,状态未正确初始化。
  • @Heikkisorsa 我并不是说在这里使用有状态小部件是错误的。我只是指着这个问题“我需要一个 StatlessWidget 还是 SetState?”我即将回答:“回答自己你真正需要使用什么以及为什么”
  • @sonic 你说得对,我要学很多东西!

标签: flutter textfield


【解决方案1】:

试试这个以使其正常工作:

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,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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 _volume;

  @override
  initState(){
      _volume = 0;
  }

  void _calculation() {
    setState((){
        _volume = int.parse(lenCon.text) * int.parse(widCon.text) * int.parse(higCon.text);
        },
    );
    print(_volume);
  }

  final lenCon = TextEditingController();
  final widCon = TextEditingController();
  final higCon = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: lenCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Länge',
                ),
              ),
              TextField(
                controller: widCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Breite',
                ),
              ),
              TextField(
                controller: higCon,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: 'Höhe',
                ),
              ),
              RaisedButton(
                onPressed: (_calculation),
                child: Text('Berechnen'),
              ),
              Text('Your Volume is: $_volume'),
            ],
          ),
        ),
      ),
    );
  }
}

首先,您需要在字符串和整数之间进行某种类型的转换(如果需要,甚至可以加倍)。其次,您应该将生成的 _volume 置于一种状态,以便在您更新状态时 UI 会刷新(通过 setState)。尽量避免使用var 并改用具体类型。从长远来看,它将对您有所帮助。

【讨论】:

  • 这是一个“啊哈”的时刻。我需要将 SetState 放入 _calculation 函数中。非常感谢!
【解决方案2】:

变量lenConwidConhigCon 的类型为TextEditingConrollerTextEditingController 是一个类,可以帮助您控制TextField 中的信息。

在这种情况下,您可能希望访问用户输入到TextField 中的值,您可以使用TextEditorControllertext 属性访问该值。如果您将 calculation 方法更新为类似下面的方法,它应该可以工作:

void _calculation() {
  final length = double.parse(lenCon.text);
  final width = double.parse(widCon.text);
  final height = double.parse(higCon.text);

  _volume = length * width * height;
  print(_volume);
}

您仍然需要使用double.parse,因为.text 属性将返回一个String,需要将其转换为您进行计算的东西(数值类型)。

如前所述,TextEditingController 将使您能够访问有关用户与TextField 交互的更多信息。例如文本的选定部分(使用selection 属性)。如果您想阅读,可以查看:https://api.flutter.dev/flutter/widgets/TextEditingController-class.html

【讨论】:

  • 太棒了!谢谢!
【解决方案3】:

感谢 Maurits van Beusekom,我能够完成计算。

但我必须将它移到按钮上才能使 Text('Your Volume is: $_volume'), 正常工作。

      RaisedButton(
        onPressed: () {
          setState(() {
            final length = double.parse(lenCon.text);
            final width = double.parse(widCon.text);
            final height = double.parse(higCon.text);

            _volume = length * width * height;
            print(_volume);
          });
        },
        child: Text('Berechnen'),
      ),

编辑:

没关系,我只需要将 SetState 放入计算中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多