The article will show an example of webview in Flutter. I will create a simple application using Flutter embedded in webview.
First, we must create a project using Visual Studio Code software with the name "webview". Let's create a new project using Visual Studio Code.
  1. Invoke View > Command Palette.
  2. Type “flutter”, and select the Flutter: New Project.
  3. Enter a project name, example such as "webview", and press Enter.
  4. Create or select the parent directory for the new project folder.
  5. Wait for project creation progress to complete and the main.dart file to appear.
Below is an example using webview flutter plugin webview_flutter: ^0.3.3. Edit the file pubspec.yaml in your directory and it should look something like below, and then update the webview flutter package.
  1. name: webview
  2. description: A new Flutter project.
  3. # The following defines the version and build number for your application.
  4. # A version number is three numbers separated by dots, like 1.2.43
  5. # followed by an optional build number separated by a +.
  6. # Both the version and the builder number may be overridden in flutter
  7. # build by specifying --build-name and --build-number, respectively.
  8. # In Android, build-name is used as versionName while build-number used as versionCode.
  9. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning
  10. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
  11. # Read more about iOS versioning at
  12. # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
  13. version: 1.0.0+1
  14. environment:
  15. sdk: ">=2.1.0 <3.0.0"
  16. dependencies:
  17. flutter:
  18. sdk: flutter
  19. # The following adds the Cupertino Icons font to your application.
  20. # Use with the CupertinoIcons class for iOS style icons.
  21. cupertino_icons: ^0.1.2
  22. webview_flutter: ^0.3.3
  23. dev_dependencies:
  24. flutter_test:
  25. sdk: flutter
  26. # For information on the generic Dart part of this file, see the
  27. # following page: https://www.dartlang.org/tools/pub/pubspec
  28. # The following section is specific to Flutter.
  29. flutter:
  30. # The following line ensures that the Material Icons font is
  31. # included with your application, so that you can use the icons in
  32. # the material Icons class.
  33. uses-material-design: true
  34. # To add assets to your application, add an assets section, like this:
  35. # assets:
  36. # - images/a_dot_burr.jpeg
  37. # - images/a_dot_ham.jpeg
  38. # An image asset can refer to one or more resolution-specific "variants", see
  39. # https://flutter.dev/assets-and-images/#resolution-aware.
  40. # For details regarding adding assets from package dependencies, see
  41. # https://flutter.dev/assets-and-images/#from-packages
  42. # To add custom fonts to your application, add a fonts section here,
  43. # in this "flutter" section. Each entry in this list should have a
  44. # "family" key with the font family name, and a "fonts" key with a
  45. # list giving the asset and other descriptors for the font. For
  46. # example:
  47. # fonts:
  48. # - family: Schyler
  49. # fonts:
  50. # - asset: fonts/Schyler-Regular.ttf
  51. # - asset: fonts/Schyler-Italic.ttf
  52. # style: italic
  53. # - family: Trajan Pro
  54. # fonts:
  55. # - asset: fonts/TrajanPro.ttf
  56. # - asset: fonts/TrajanPro_Bold.ttf
  57. # weight: 700
  58. #
  59. # For details regarding fonts from package dependencies,
  60. # see https://flutter.dev/custom-fonts/#from-packages
You must use Flutter webview container for custom widget, so create a file with the name web_view_container.dart like below,
  1. import 'package:flutter/material.dart';
  2. import 'package:webview_flutter/webview_flutter.dart';
  3. class WebViewContainer extends StatefulWidget {
  4. final url;
  5. WebViewContainer(this.url);
  6. @override
  7. createState() => _WebViewContainerState(this.url);
  8. }
  9. class _WebViewContainerState extends State<WebViewContainer> {
  10. var _url;
  11. final _key = UniqueKey();
  12. _WebViewContainerState(this._url);
  13. @override
  14. Widget build(BuildContext context) {
  15. return Scaffold(
  16. appBar: AppBar(),
  17. body: Column(
  18. children: [
  19. Expanded(
  20. child: WebView(
  21. key: _key,
  22. javascriptMode: JavascriptMode.unrestricted,
  23. initialUrl: _url))
  24. ],
  25. ));
  26. }
  27. }
We need the home screen to show a single button that opens a URL. Create a file with the name Home.dart and you can put your link in this file.
  1. import 'package:flutter/material.dart';
  2. import 'web_view_container.dart';
  3. class Home extends StatelessWidget {
  4. final _links = ['https://camellabs.com'];
  5. @override
  6. Widget build(BuildContext context) {
  7. return Scaffold(
  8. body: SafeArea(
  9. child: SingleChildScrollView(
  10. child: Column(
  11. mainAxisAlignment: MainAxisAlignment.center,
  12. crossAxisAlignment: CrossAxisAlignment.stretch,
  13. children: _links.map((link) => _urlButton(context, link)).toList(),
  14. ))));
  15. }
  16. Widget _urlButton(BuildContext context, String url) {
  17. return Container(
  18. padding: EdgeInsets.all(20.0),
  19. child: FlatButton(
  20. color: Theme.of(context).primaryColor,
  21. padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),
  22. child: Text(url),
  23. onPressed: () => _handleURLButtonPress(context, url),
  24. ));
  25. }
  26. void _handleURLButtonPress(BuildContext context, String url) {
  27. Navigator.push(context,
  28. MaterialPageRoute(builder: (context) => WebViewContainer(url)));
  29. }
  30. }
And then modify file main.dart like below,
  1. import 'package:flutter/material.dart';
  2. import 'home.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. title: 'Flutter Web Views',
  9. theme: ThemeData(
  10. primarySwatch: Colors.blue,
  11. fontFamily: "Arial",
  12. textTheme: TextTheme(
  13. button: TextStyle(color: Colors.white, fontSize: 18.0),
  14. title: TextStyle(color: Colors.red))),
  15. home: Home(),
  16. );
  17. }
  18. }
After you finish copying the main.dart, running the application will show the below output,
You can see a webview in flutter example in Here.
Thank you for reading this article, I hope it is useful for you. Visit My Github about Flutter Here.