如何从flutter的子类访问webviewcontroller

时间:2019-08-12 13:41:15

标签: flutter dart

我正在使用flutter webview_flutter软件包。

现在我在主_controller中有一个webview _MyAppState变量。

“ _ MyAppState”类具有子窗口小部件QRCodeScannerPage()

QRCodeScannerPage()内,我有一个onTap函数。通过此功能,我想更改状态并调用_controller.load()。但是如何访问_controller

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedTab = 0;
  bool _webView = false;

  WebViewController _controller;
@override
Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: Colors.black,
          accentColor: Color(0xFF4054b2),
          // primarySwatch: Colors.purple,
          primaryTextTheme: TextTheme(
            title: TextStyle(color: Colors.white),
          )),
      title: _title,
      home: Scaffold(
        key: _scaffoldKey,
        appBar: new AppBar(
          title: const Text('Clubs & Classes'),
        ),
        body: IndexedStack(
          index: _stackToView,
          children: [
            Column(
              children: <Widget>[
                Expanded(
                    child: WebView(
                  key: _key,
                  javascriptMode: JavascriptMode.unrestricted,
                  initialUrl: _url,
                  onPageFinished: _handleLoad,
                  onWebViewCreated: (WebViewController webViewController) {
                    _controller = webViewController;
                  },
                )),
              ],
            ),
            Container(
              color: Colors.white,
              child: Center(
                child: CircularProgressIndicator(),
              ),
            ),
            QRCodeScannerPage()
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
          // code...
        ),
    );
  }

0 个答案:

没有答案