首页 > 解决方案 > 如何将按钮呈现到 main.dart 文件?另外,有没有什么方法可以在不使用任何按钮的按下()的情况下将参数传递给命名路由?

问题描述

这是 main.dart,目前参数在按钮的 press() 上传递,我请求建议一种方法(如果有)将参数从 main.dart(示例)传递到 plugin.dart,而不使用按钮!。

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
    routes: {
      ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
    },
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
  
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My test app'),
      ),
      body: Center(
        child: Text('This example app'),
      ),

      // A button that navigates to a named route. The named route extracts the arguments by itself.
      floatingActionButton: FloatingActionButton(
        child: Text("Boutton"), //button that is passing arguments to named route.
        onPressed: () {
          
          Navigator.pushNamed(
            context,
            ExtractArgumentsScreen.routeName,
            arguments: ScreenArguments(
              'Click me',                       //custom button name
              'abc',                            //api key
            ),
          );
        },
      )
    );
  }
}

下面是 plugin.dart 文件,我在其中发出 http 获取请求。我如何在 main.dart 中调用它,以便按钮以相同的方式呈现。

class ScreenArguments {
  final String buttonName;

  final String api;

  ScreenArguments(this.buttonName, this.api);
}

class ExtractArgumentsScreen extends StatefulWidget {
  _ExtractArgumentsScreen createState() => _ExtractArgumentsScreen();
  static const routeName = '/extractArguments';
}

// A widget that extracts the necessary arguments from the ModalRoute.
class _ExtractArgumentsScreen extends State<ExtractArgumentsScreen> {
  Future<String> myButton() async {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as ScreenArguments.

    final ScreenArguments args = ModalRoute.of(context).settings.arguments;
    String url = "example.com/project/details?key=" + args.api;
    final response = await http.get(url);
    // headers: {"Accept":"application/json"});

    if (response.statusCode == 200) {
      print(response.body);
      return args.buttonName;
    }
    return null;
  }

  Widget build(BuildContext context) {
    return FutureBuilder<String>(
        future: myButton(),
        builder: (context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            return FloatingActionButton(        //this button has to be rendered to main.dart file
              child: Text(snapshot.data),
              onPressed: () {
                MyForm();
              },
            );
          } else {
            return CircularProgressIndicator();
          }
        }
        );
  }
}

class MyForm extends State<ExtractArgumentsScreen> {
  @override
  Widget build(BuildContext context) {
    // code for form widget
  }
}

该方法在语法上是否正确?!

标签: javahttpflutterasync-awaitflutter-plugin

解决方案


推荐阅读