Я новичок в Дарт и Флаттер. Теперь у меня проблема с вызовом метода из другого класса.
Я пытался сделать метод статическим, но метод содержит метод setState (), поэтому это невозможно.
Поэтому мне нужно вызвать main.dart
>>> showDialogWith()
изwallet.dart
main.dart
import 'package:flutter/material.dart';
import 'dialog/operation.dart';
import 'pages/wallet.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future showDialogWith(String dialogName) async {
Widget dialog;
switch (dialogName) {
case 'operations':
setState(() {
dialog = OperationsDialog();
});
break;
// another cases and default...
}
await showDialog(
context: context,
child: dialog,
);
}
@override
Widget build(BuildContext context) {
body: WalletContent();
}
}
wallet.dart
class WalletContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialButton(
onPressed: () {
// here I have to call the 'showDialogWith()' method
},
);
}
}
operation.dart
class OperationsDialog extends StatefulWidget{
OperationsDialog({Key key}) : super(key: key);
@override
_OperationDialogState createState() => new _OperationDialogState();
}
class _OperationDialogState extends State<OperationsDialog> {
@override
Widget build(BuildContext context) {
return new SimpleDialog(
title: new Text('Операции', textAlign: TextAlign.center),
);
}
}