Home > Enterprise >  How to do calculation using widget
How to do calculation using widget

Time:04-20

import 'package:flutter/material.dart';

class Screen2 extends StatefulWidget {
  late final String name;
  late final String email;
  late final String quantity;
  late final String movie;
  // late final String price;
  String? price;

  // late final String total;
  // String? movie;
  // late final int total;
  // late final int total;


  // late final String value;

  Screen2({required this.name,required this.email,required this.quantity, required this.movie , required this.price});


  @override
  State<Screen2> createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'eTicket',
          style: TextStyle(color: Colors.grey, fontSize: 18.0),
        ),
        // centerTitle: true,

      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: EdgeInsets.all(40.0),
              decoration: BoxDecoration(
                color: Colors.grey,
                border: Border.all(color: Colors.grey, width: 5.0),
              ),
              child: Column(
                children: <Widget> [
                  Text(
                    'Name : '   widget.name ,
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                  Divider(),
                  Text(
                    'Email : '  widget.email ,
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                  Divider(),
                  Text(
                    'Movie : '  widget.movie.toString(),
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                  Divider(),
                  // Text(
                  //   widget.val == null ? '' : widget.val.toString(),
                  //   style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  // ),
                  Text(
                   'Quantity : '   widget.quantity,
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    'Total Price :'   widget.price.toString() * int.parse(widget.quantity), // I want to make it like (price * quantity)


                  ),
                ],
              ),
            ),
            SizedBox(
              height: 20.0,
            ),
          ],
        ),
      ),
    );
  }
}

So basically I wanted to make a calculation at the "Total Price: " but I'm not sure how to do it using the widget. what I think was that when widget.price * widget.quantity means that price * quantity but the result shows for example price = 20 and quantity = 2 , the result will be 2020 , please help

CodePudding user response:

Encapsulate the calculation in parentheses, so that the operator uses the result of the calculation?

Text(
 'Total Price :' 
   ( widget.price.toString() * int.parse(widget.quantity ) )

), 
  

Because * is an operator on string. From the docs:

Creates a new string by concatenating this string with itself a number of times.

The result of str * n is equivalent to str str ...(n times)... str.

CodePudding user response:

you can do it inside $()

Text(
 "Total Price : 
 $( widget.price.toString() * int.parse(widget.quantity ) )"
),
  • Related