link 1.1.0

  • Readme
  • Changelog
  • Installing
  • 86

Link #

Just a simple link-alike widget for Flutter #

Although simple, Flutter lacks a linkable widget out of the box, so I decided to build my own. No fancy code, but it does its job. That's all I asked.

It uses an InkWell widget to provide both some visual response to the tap event and a trigger to launch the URL (onTap).

It's not intended to open up all kinds of URLs; it basically depends on your device. But you can be certain http:// and https:// URL will be launched (every device ships a webkit). Instead of throwing an error in case the URL can't be opened, you can use the onError callback at the constructor and do whatever you want to do. I really wanted to keep it simple.

For the launching part, I took the url_launcher package available at Dart Pub. A nice, working library I've used in almost every project since I learned about it.

Almost forgot... no idea whether it works on iOS. For the time being, I haven't yet set up the environment ;P. Maybe someday.

Documentation #

url_launcher

You can find the url_launcher Flutter package at Dart Pub, where I plan to submit this very library.

Let's give it a try #

Installing

Include link in your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  link: ^1.0.0

If your IDE doesn't do it automatically, type:

flutter packages get

Using

Import the package in your dart file and use Link to get the actual icon widget:

import 'package:link/link.dart';
...
Link _link = Link(
  child: Text('A link'),
  url: 'http://www.google.com',
  onError: _onErrorCallback
);
...

OnError is not required and defaults to null, meaning that, in case URL can't be launched, nothing will happen. No error, no alert, nothing.

Sample

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Link Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  void _showErrorSnackBar() {
    Scaffold.of(context).showSnackBar(
      SnackBar(
        content: Text('Oops... the URL couldn\'t be opened!'),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Link(
          child: Text('This is a link to Flutter'),
          url: 'https://flutter.dev',
          onError: _showErrorSnackBar,
        )
      ),
    );
  }
}

GitHub source code #

If you're interested on the code (feel free to modify it anyway you want), you can find it here: https://github.com/galonsos/link

1.0.0 #

  • Initial version

1.1.0 #

  • Fixed a bug: URL didn't open within the app, but in browser. Solved already

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  link: ^1.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:link/link.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
82
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
77
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Oct 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/link_base.dart. (-0.50 points)

Analysis of lib/src/link_base.dart reported 1 hint:

line 13 col 66: Don't explicitly initialize variables to null.

Format lib/link.dart.

Run flutter format to format lib/link.dart.

Maintenance suggestions

The package description is too short. (-13 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and link.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
url_launcher ^5.0.2 5.2.3
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
test ^1.0.0