简体   繁体   English

从另一个 dart 文件调用 main.dart 中的 void function

[英]Calling a void function in main.dart from another dart file

Thanks in advance....提前致谢....

I have an App built with Flutter whereby the main.dart file has a void function.我有一个使用 Flutter 构建的应用程序,其中 main.dart 文件有一个无效 function。 I have another dart file with a raised button that i would like to call the void function from the main.dart file我有另一个带有凸起按钮的 dart 文件,我想从 main.dart 文件中调用 void function

is this possible??这可能吗??

//This is the void in the main.dart file


 void buildBottomSheet(double height, MedicineModel model) async {
   var medicineId = await showModalBottomSheet(
       shape: RoundedRectangleBorder(
           borderRadius: BorderRadius.only(
                topLeft: Radius.circular(45), topRight: Radius.circular(45))),
       context: context,
       isScrollControlled: true,
       builder: (context) {
         return FadeAnimation(
            .6,
           AddMedicine(height, model.getDatabase(), 
           model.notificationManager),
         );
       });

  if (medicineId != null) {
    Fluttertoast.showToast(
        msg: "Reminder Added!",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM_LEFT,
        timeInSecForIosWeb: 1,
        backgroundColor: Theme
            .of(context)
            .accentColor,
        textColor: Colors.white,
        fontSize: 20.0);

    setState(() {});
      }
     }   //and then a raised button in a different .dart file to call this void?

yes it is possible to call like MyApp.buildBottomSheet(); where MyApp is the class name.是的,可以call like MyApp.buildBottomSheet(); where MyApp is the class name. call like MyApp.buildBottomSheet(); where MyApp is the class name. But it is not good practice to call function from the main.dart file但是从 main.dart 文件中调用 function 并不是一个好习惯

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
 void buildBottomSheet(double height, MedicineModel model) async {
      //custom function
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

Many thanks, but now the issue is solved.非常感谢,但现在问题解决了。 I have started from scratch and now not calling function from main.dart file as @Muhammad Tameem Rafay advised.我从头开始,现在没有按照@Muhammad Tameem Rafay 的建议从 main.dart 文件中调用 function。 But thank you anyway不过还是谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM