-1

Thing is my application is very complicated and I have an entire algorithm written on python ready, but before I implement it I want to develop a relation between Python and Dart.

I have created virtual Python environment inside utils directory because obviously I will be using lots of external third party libraries for my project, and of course my target platforms are Android and IOS so will have to keep that in mind that whatever solution you provide it does not get effected by any platform dependency.

This is my folder structure:

enter image description here

For now let's make application simple, let's say my script.py has the following code:

def hello():
    return "Hello World"
import 'package:flutter/material.dart';
import 'package:image_background_remover/routes/routes.dart';
import 'package:image_background_remover/src/pages/home_page/home_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      initialRoute: MyRoutes.homeRoute,
      routes: {
        MyRoutes.homeRoute: (context) => const HomePage(
              title: 'Image Background Remover',
            ),
      },
    );
  }
}

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, required this.title});

  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(255, 70, 71, 134),
        appBar: AppBar(
          backgroundColor: const Color.fromARGB(255, 37, 39, 107),
          title: Center(
            child: Text(
              widget.title,
              style: const TextStyle(color: Colors.white),
            ),
          ),
        ),
        body: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                'assets/images/img_1.jpg',
                width: 400, // Set a fixed width
                height: 400, // Set a fixed height
              ),
              Container(
                margin:
                    const EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        // Add your button logic here
                      },
                      child: const Text('Upload'),
                    ),
                    ElevatedButton(
                      onPressed: () {},
                      child: const Text('Remove BG'),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        // Add your button logic here
                      },
                      child: const Text('Export'),
                    ),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}

I want to call hello() method from dart when clicked on this button:

ElevatedButton(
      onPressed: () {
           // Add your button logic here
      },
      child: const Text('Upload'),
),

For now all I am expecting is to print "hello world" in Flutter Dart console using print. And yes dartpy and star flute cannon handle the libraries I am planning to use, I have to rely virtual Python environment. Also if I decide to use dart as frontend and python-flet which I think is also used for frontend, do you think it can do everything possible with Python, I mean communication between Flutter Dart and Python-Flet you know something like that, this is something that just came in my mind.

halfer
  • 19,824
  • 17
  • 99
  • 186

0 Answers0