Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

flutter - 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. I have another dart file with a raised button that i would like to call the void function from the main.dart file

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?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

yes it is possible to call like MyApp.buildBottomSheet(); where MyApp is the class name. But it is not good practice to call function from the main.dart file

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'),
        ),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...