꿈꾸는 시스템 디자이너

Flutter Example - Launch Web Browser | url_launcher plugin 본문

Tutorial/Flutter with App

Flutter Example - Launch Web Browser | url_launcher plugin

독행소년 2019. 10. 23. 13:12

* About plugin

https://pub.dev/packages/url_launcher

 

url_launcher | Flutter Package

Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes.

pub.dev

 

1. Add this to pubspec.yaml file

dependencies:
  url_launcher: ^5.1.3

 

2. Source code

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

OpenWebBrowserState pageState;

class OpenWebBrowser extends StatefulWidget {
  @override
  OpenWebBrowserState createState() {
    pageState = OpenWebBrowserState();
    return pageState;
  }
}

class OpenWebBrowserState extends State<OpenWebBrowser> {
  final scaffoldKey = GlobalKey<ScaffoldState>();

  TextEditingController teCon =
      TextEditingController(text: "http://google.com");

  FocusNode myFocusNode = FocusNode();

  @override
  void dispose() {
    super.dispose();
    myFocusNode.dispose();
    teCon.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(title: Text("Open Web Browser")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            customButton("Google", "https://google.com"),
            customButton("Flutter", "https://flutter.dev"),
            customButton("Facebook", "https://facebook.com"),
            Divider(color: Colors.grey, height: 50, indent: 50, endIndent: 50),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 30),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Text("URL: "),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 10),
                      child: TextField(
                        decoration: InputDecoration(),
                        controller: teCon,
                        focusNode: myFocusNode,
                      ),
                    ),
                  ),
                  Container(
                    width: 60,
                    child: RaisedButton(
                      padding: const EdgeInsets.all(0),
                      color: Colors.blueGrey,
                      textColor: Colors.white,
                      child: Text("lauch"),
                      onPressed: () {
                        _launchUrl(teCon.text.toString());
                      },
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.only(left: 5),
                    width: 65,
                    child: RaisedButton(
                      padding: const EdgeInsets.all(0),
                      color: Colors.grey,
                      textColor: Colors.white,
                      child: Text("clear"),
                      onPressed: () {
                        FocusScope.of(context).requestFocus(myFocusNode);
                        teCon.text = "http://";
                      },
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  customButton(String text, String url) {
    return Container(
      width: 250,
      child: RaisedButton(
        color: Colors.blue,
        textColor: Colors.white,
        child: Text(text),
        onPressed: () {
          teCon.text = url;
        },
      ),
    );
  }

  _launchUrl(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      scaffoldKey.currentState
        ..hideCurrentSnackBar()
        ..showSnackBar(
          SnackBar(
            content: Text("'$url' is invalid a URL"),
            backgroundColor: Colors.deepOrange,
            action: SnackBarAction(
              label: "Done",
              textColor: Colors.white,
              onPressed: () {},
            ),
          ),
        );
    }
  }
}

 

 

▶ Go to Table of Contents | 강의 목차로 이동


※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.

 

Flutter Code Examples - Google Play 앱

Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.

play.google.com

Comments