skip to Main Content

i am trying to create a single page app where I want to add and multiply some values, values are given below;

(currentTime.Day + currentTime.Month + 39) * currentTime.Year

and i want the value shown below and also get copied

this is the formula

enter image description here

2

Answers


  1. As far as i understood you want this fuzzy expression to be implemented, you can refer the below code:

    void main() {
      int day = DateTime.now().day;
      int month = DateTime.now().month;
      int year = DateTime.now().year;
    
      int result = (day + month + 39) * year;
    
      print(result);
    }
    

    For adding this data to your device clipboard you need a button, on it’s click it will add the value to the clipboard:

    import this import 'package:flutter/services.dart';

    And then Simply implement this into your onPressed of the button:

    onPressed: () async {
      await Clipboard.setData(ClipboardData(text: result));
      // copied successfully
    },
    

    Replace this below code with your main file code:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() {
      runApp(
        MaterialApp(
          home: const Home(),
          debugShowCheckedModeBanner: false,
          theme: ThemeData(primarySwatch: Colors.green),
        ),
      );
    }
    
    class Home extends StatefulWidget {
      const Home({super.key});
    
      @override
      State<Home> createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      late String buttonText;
    
      @override
      void initState() {
        super.initState();
        int day = DateTime.now().day;
        int month = DateTime.now().month;
        int year = DateTime.now().year;
    
        buttonText = ((day + month + 39) * year).toString();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Copy to Clipboard'),
          ),
          body: Center(
            child: ElevatedButton(
              child: Text(buttonText),
              onPressed: () async {
                await Clipboard.setData(ClipboardData(text: buttonText.toString()));
              },
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. You can implement the formula using dart’s date time class methods.
    Use the below code to get for the formula.

    void main() {
    DateTime currentTime = DateTime.now();
    print((currentTime.day + currentTime.month + 39) * 
    currentTime.year); 
    }
    

    Reading the above comments you also want to copy the result of the expression.

    You can use the SelectableText widget to solve that problem or you can use the above mentioned Clipboard service by importing the flutter/services.dart

    You can view the working example of my code on dartpad here

    To copy the value you just have to long press on the text and it will give you the option to copy the text.

    import 'package:flutter/material.dart';
    
    void main() {
    runApp(
    MaterialApp(
      home: const CalculatorExpression(),
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.blue),
      ),
     );
    }
    
    class CalculatorExpression extends StatefulWidget {
    const CalculatorExpression({super.key});
    
    @override
    State<CalculatorExpression> createState() => 
    _CalculatorExpressionState();
    }
    
    class _CalculatorExpressionState extends 
     State<CalculatorExpression> {
      String buttonText = '';
      int? day;
      int? month;
      int? year;
    @override
    void initState() {
     super.initState();
      day = DateTime.now().day;
      month = DateTime.now().month;
      year = DateTime.now().year;
    
      buttonText = ((day! + month! + 39) * year!).toString();
    }
    
    @override
    Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text('Date Time Expression'),
      ),
      body: Center(   
        child: Column(mainAxisAlignment: MainAxisAlignment.center, 
        children: [ Text("The expression 
          (($day + $month + 39) * $year) will result to give the 
          below value:"),SelectableText(buttonText),]) 
         ),
        );
       }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search