Webview In Flutter Example

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.   
  4. # The following defines the version and build number for your application.    
  5. # A version number is three numbers separated by dots, like 1.2.43    
  6. # followed by an optional build number separated by a +.    
  7. # Both the version and the builder number may be overridden in flutter    
  8. # build by specifying --build-name and --build-number, respectively.    
  9. # In Android, build-name is used as versionName while build-number used as versionCode.    
  10. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning    
  11. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.    
  12. # Read more about iOS versioning at    
  13. # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html    
  14. version: 1.0.0+1    
  15.     
  16. environment:    
  17.   sdk: ">=2.1.0 <3.0.0"    
  18.     
  19. dependencies:    
  20.   flutter:    
  21.     sdk: flutter    
  22.   
  23.   # The following adds the Cupertino Icons font to your application.    
  24.   # Use with the CupertinoIcons class for iOS style icons.    
  25.   cupertino_icons: ^0.1.2    
  26.   webview_flutter: ^0.3.3    
  27.     
  28. dev_dependencies:    
  29.   flutter_test:    
  30.     sdk: flutter    
  31.   
  32.   
  33. # For information on the generic Dart part of this file, see the    
  34. # following page: https://www.dartlang.org/tools/pub/pubspec    
  35.   
  36. # The following section is specific to Flutter.    
  37. flutter:    
  38.   
  39.   # The following line ensures that the Material Icons font is    
  40.   # included with your application, so that you can use the icons in    
  41.   # the material Icons class.    
  42.   uses-material-design: true    
  43.   
  44.   # To add assets to your application, add an assets section, like this:    
  45.   # assets:    
  46.   #  - images/a_dot_burr.jpeg    
  47.   #  - images/a_dot_ham.jpeg    
  48.   
  49.   # An image asset can refer to one or more resolution-specific "variants", see    
  50.   # https://flutter.dev/assets-and-images/#resolution-aware.    
  51.   
  52.   # For details regarding adding assets from package dependencies, see    
  53.   # https://flutter.dev/assets-and-images/#from-packages    
  54.   
  55.   # To add custom fonts to your application, add a fonts section here,    
  56.   # in this "flutter" section. Each entry in this list should have a    
  57.   # "family" key with the font family name, and a "fonts" key with a    
  58.   # list giving the asset and other descriptors for the font. For    
  59.   # example:    
  60.   # fonts:    
  61.   #   - family: Schyler    
  62.   #     fonts:    
  63.   #       - asset: fonts/Schyler-Regular.ttf    
  64.   #       - asset: fonts/Schyler-Italic.ttf    
  65.   #         style: italic    
  66.   #   - family: Trajan Pro    
  67.   #     fonts:    
  68.   #       - asset: fonts/TrajanPro.ttf    
  69.   #       - asset: fonts/TrajanPro_Bold.ttf    
  70.   #         weight: 700    
  71.   #    
  72.   # For details regarding fonts from package dependencies,    
  73.   # 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.   
  4. class WebViewContainer extends StatefulWidget {  
  5.   final url;  
  6.   
  7.   WebViewContainer(this.url);  
  8.   
  9.   @override  
  10.   createState() => _WebViewContainerState(this.url);  
  11. }  
  12.   
  13. class _WebViewContainerState extends State<WebViewContainer> {  
  14.   var _url;  
  15.   final _key = UniqueKey();  
  16.   
  17.   _WebViewContainerState(this._url);  
  18.   
  19.   @override  
  20.   Widget build(BuildContext context) {  
  21.     return Scaffold(  
  22.         appBar: AppBar(),  
  23.         body: Column(  
  24.           children: [  
  25.             Expanded(  
  26.                 child: WebView(  
  27.                     key: _key,  
  28.                     javascriptMode: JavascriptMode.unrestricted,  
  29.                     initialUrl: _url))  
  30.           ],  
  31.         ));  
  32.   }  

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.   
  4. class Home extends StatelessWidget {  
  5.   final _links = ['https://camellabs.com'];  
  6.   
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return Scaffold(  
  10.         body: SafeArea(  
  11.             child: SingleChildScrollView(  
  12.                 child: Column(  
  13.       mainAxisAlignment: MainAxisAlignment.center,  
  14.       crossAxisAlignment: CrossAxisAlignment.stretch,  
  15.       children: _links.map((link) => _urlButton(context, link)).toList(),  
  16.     ))));  
  17.   }  
  18.   
  19.   Widget _urlButton(BuildContext context, String url) {  
  20.     return Container(  
  21.         padding: EdgeInsets.all(20.0),  
  22.         child: FlatButton(  
  23.           color: Theme.of(context).primaryColor,  
  24.           padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),  
  25.           child: Text(url),  
  26.           onPressed: () => _handleURLButtonPress(context, url),  
  27.         ));  
  28.   }  
  29.   
  30.   void _handleURLButtonPress(BuildContext context, String url) {  
  31.     Navigator.push(context,  
  32.         MaterialPageRoute(builder: (context) => WebViewContainer(url)));  
  33.   }  

And then modify file main.dart like below,
  1. import 'package:flutter/material.dart';  
  2. import 'home.dart';  
  3.   
  4. void main() => runApp(MyApp());  
  5.   
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       title: 'Flutter Web Views',  
  11.       theme: ThemeData(  
  12.           primarySwatch: Colors.blue,  
  13.           fontFamily: "Arial",  
  14.           textTheme: TextTheme(  
  15.               button: TextStyle(color: Colors.white, fontSize: 18.0),  
  16.               title: TextStyle(color: Colors.red))),  
  17.       home: Home(),  
  18.     );  
  19.   }  

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.


Similar Articles