For web view in flutter, we use webview_flutter, a Flutter package that provides a WebView widget on Android and iOS.
To get started, first add webview_flutter as a dependency in your pubspec.yaml file, like this: webview_flutter: ^0.3.19+9 Incase you get this error Cannot run with sound null safety, because the following dependencies do IDE run arguments/configuration To set this up in your IDE of choice, you can use: In IntelliJ/Android Studio: "Edit Configurations" (in your run configurations) → "Additional run args". In Visual Studio Code: search for "Flutter run additional args" in your user settings. In both cases, add --no-sound-null-safety. |
After adding webview_flutter package, you can use WebView() widget. This widget has three basic arguments, thye're key, javascriptMode & initialUrl. See the implementation below: |
Code Snippet: Web View Open Website URL in Flutter |
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Web View URL Example', home: Scaffold( appBar: AppBar( title: Text('Web View URL Example'), centerTitle: true, ), body: WebViewExample('https://www.google.com/'), ), ); } } class WebViewExample extends StatefulWidget { final String url; WebViewExample(this.url); @override _WebViewExampleState createState() => _WebViewExampleState(); } class _WebViewExampleState extends State<WebViewExample> { final _key = UniqueKey(); @override Widget build(BuildContext context) { return Container( child: Column( children: <Widget>[ Expanded( child: WebView( key: _key, javascriptMode: JavascriptMode.unrestricted, initialUrl: widget.url, ), ), ], ), ); } } |
Output: Web View Open Website URL in Flutter |
For more details please see WebView for Flutter which has documentation for this package. |
Wednesday, September 21, 2022
Web Views Open Website URL in Flutter Example & Output
Flutter Multiple Styles in Single Line: Use RichText & TextSpan
In Flutter, if you need to design single line with multiple styles then it can be done by using RichText widget with TextSpan.
RichText widget displays text with different styles. Different text to display is represented using a tree of TextSpan widgts. Each of these TextSpan widgets are associated with different style. In this flutter example, we are going to implement simple application that displays multiple styles on same line. |
Code Snippet: Text with Multiple Styles |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'RichText + TextSpan Example', home: Scaffold( appBar: AppBar( title: Text('RichText+ TextSpan Example'), centerTitle: true, ), body: Center( child: RichText( text: TextSpan( text: 'Style 1 ', style: TextStyle( color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 24, ), children: remainingItems() ), ), ), ), ); } List<InlineSpan> remainingItems() { return [ TextSpan( text: 'Style 2 ', style: TextStyle( color: Colors.white, backgroundColor: Colors.teal, fontSize: 20, ), ), TextSpan( text: 'Style 3 ', style: TextStyle( color: Colors.orange, fontFamily: 'courier', fontSize: 24, ), ), TextSpan( text: 'Style 4 ', style: TextStyle( fontStyle: FontStyle.italic, color: Colors.pink, fontSize: 18, ), ), ]; } } |
Output: Multiple Styles for Text |
Based on layout constraints the text might break across multiple lines or might all be displayed on the same line. For more details please see RichText for Flutter which has documentation for this widget. |
Sunday, October 31, 2021
MySQL Drop All Tables At a Glance - For Speed Up Development
There may come a time when you need to drop all of your tables in a MySQL database but don't want to delete database. Consider you have 100+ tables in your database. So it will be big problem for all of us to delete all tables in short time.
If you have foreign keys in your database, then you may encounter errors if you drop a table that is related to another table using foreign keys. The quicker way to do this is to disable the foreign key checks when these statements are run, so you can drop the tables and avoid the error. Add this line above all of your Drop Table statements to disable the foreign key checks: SET FOREIGN_KEY_CHECKS = 0; Then, add this line at the end of your script to enable them: SET FOREIGN_KEY_CHECKS = 1; |
So what we will do is Drop All Tables In One Script to speed up our whole process. |
SET FOREIGN_KEY_CHECKS = 0; SET GROUP_CONCAT_MAX_LEN=32768; SET @tables = NULL; SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables FROM information_schema.tables WHERE table_schema = (SELECT DATABASE()); SELECT IFNULL(@tables,'dummy') INTO @tables; SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables); PREPARE stmt FROM @tables; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET FOREIGN_KEY_CHECKS = 1; |
Thursday, October 21, 2021
Grails 4: How to add Java JAR files to Grails project | How to add an external library or JAR file that is not a grails plugin to the Grails project
Putting the jar in the lib folder should do the trick.
The default lib folder is gone as of Grails 3.0. grails.github.io/grails-doc/3.0.x/guide/single.html#upgrading --> "Dependency resolution should be used to resolve JAR files" |
If Grails by default don't take the local .jar libraries located in <GRAILS-APP-DIR>/lib (seems that with Grails >= 3.X the /lib folder default configuration is removed) the easy way to enforce it is modifying build.gradle to add a local directory dependency for it. |
For almost all cases is of course better to use the maven repos, but in some cases it's possible to have some other libraries which aren't in a public repo. To do so we have to add these libraries in some lib folder then modify the <GRAILS-APP-DIR>/build.gradle and add something like: |
dependencies { ... // lib folder or any other name one can use compile fileTree(dir: './lib', include: ['*.jar']) ... } |
If you want you can use another directory (not /lib) since you're specifying it. Of course use the correct scope (for example for jars which probably already are in your application container class path the scope will be runtime instead of compile) |
You can download and drop the jar file into the grails-app/lib directory directly. This should be carefully maintained by someone time to time. Other developers working on the same project might not be aware of its presence. Plus you cannot easily upgrade versions in a transparent manner. |
Get Android API level of phone currently running my application
How do I get the Api level of the phone curently running my application |
Check android.os.Build.VERSION, which is a static class that holds various pieces of information about the Android OS a system is running.
If you care about all versions possible (back to original Android version), as in minSdkVersion is set to anything less than 4, then you will have to use android.os.Build.VERSION.SDK, which is a String that can be converted to the integer of the release. If you are on at least API version 4 (Android 1.6 Donut), the current suggested way of getting the API level would be to check the value of android.os.Build.VERSION.SDK_INT, which is an integer. |
In either case, the integer you get maps to an enum value from all those defined in android.os.Build.VERSION_CODES: |
SDK_INT value Build.VERSION_CODES Human Version Name 1 BASE Android 1.0 (no codename) 2 BASE_1_1 Android 1.1 Petit Four 3 CUPCAKE Android 1.5 Cupcake 4 DONUT Android 1.6 Donut 5 ECLAIR Android 2.0 Eclair 6 ECLAIR_0_1 Android 2.0.1 Eclair 7 ECLAIR_MR1 Android 2.1 Eclair 8 FROYO Android 2.2 Froyo 9 GINGERBREAD Android 2.3 Gingerbread 10 GINGERBREAD_MR1 Android 2.3.3 Gingerbread 11 HONEYCOMB Android 3.0 Honeycomb 12 HONEYCOMB_MR1 Android 3.1 Honeycomb 13 HONEYCOMB_MR2 Android 3.2 Honeycomb 14 ICE_CREAM_SANDWICH Android 4.0 Ice Cream Sandwich 15 ICE_CREAM_SANDWICH_MR1 Android 4.0.3 Ice Cream Sandwich 16 JELLY_BEAN Android 4.1 Jellybean 17 JELLY_BEAN_MR1 Android 4.2 Jellybean 18 JELLY_BEAN_MR2 Android 4.3 Jellybean 19 KITKAT Android 4.4 KitKat 20 KITKAT_WATCH Android 4.4 KitKat Watch 21 LOLLIPOP Android 5.0 Lollipop 22 LOLLIPOP_MR1 Android 5.1 Lollipop 23 M Android 6.0 Marshmallow in October 2015 24 N Android 7.0 Nougat in August 2016 25 N_MR1 Android 7.1.1 Nougat in October 2016 26 O Android 8.0 Oreo in August 2017 27 O_MR1 Android 8.1 Oreo MR1 in December 2017 28 P Android 9 Pie in August 2018 29 Q Android 10 in September 2019 30 R Android 11 in September 2020 10000 CUR_DEVELOPMENT Current Development Version |
All informatio collected from https://developer.android.com/reference/android/os/Build.VERSION_CODES |
Tuesday, October 12, 2021
Grails 4: How to load datasource configuration from external file in grails 4 In Grails, how do I put my DB username and password in an external property file
I'm trying to take certain database configurations from variables and put them into an external properties file.
I am writing a grails 4.0.11 application. My datasource written in application.groovy file. I want to load datasource configuration like username,password,DB from an external file. Is there any way to do it in grails 4+ versions. Here is my datasource configuration in application.groovy using static database name and other properties like username, password etc:- |
hibernate { cache { queries = false use_second_level_cache = true use_query_cache = true } } dataSource { pooled = true jmxExport = true dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" driverClassName = "org.mariadb.jdbc.Driver" username = 'root' password = '' dbCreate = "update" url = "jdbc:mysql://localhost/db2?useUnicode=yes" + "&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true" + "&useLegacyDatetimeCode=false&serverTimezone=UTC" properties = { jmxEnabled = true initialSize = 5 maxActive = 50 minIdle = 5 maxIdle = 25 maxWait = 10000 maxAge = 10 * 60000 timeBetweenEvictionRunsMillis = 5000 minEvictableIdleTimeMillis = 60000 validationQuery = "SELECT 1" validationQueryTimeout = 3 validationInterval = 15000 testOnBorrow = true testWhileIdle = true testOnReturn = false ignoreExceptionOnPreLoad = true jdbcInterceptors = "ConnectionState;StatementCache(max=200)" defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED // safe default abandonWhenPercentageFull = 100 // settings are active only when pool is full removeAbandonedTimeout = 120 removeAbandoned = true logAbandoned = false // causes stacktrace recording overhead, use only for debugging } } |
Yes, what we can do that is to put database configurations to a file named db_name.properties under [src/main/webapp] directory with following contents:
db_name=some_data_base_name db_user=root_user db_password=some_password Keeping these information will not load automatically. We have to do something magic to load these information into system. We can define database configuration for grails 4 in 3 different ways - 1. grails-app/conf/config/application.yml 2. grails-app/conf/application 3. grails-app/conf/application.groovy So from above list we can easily set our target file to load grails 4 application datasource information because we can write code inside groovy files. First of all remove any datasource related block from above 2 files and add configuration to grails-app/conf/application.groovy file as early statement with some modification. Now we will load database information from some properties file. We sill use Properties to load database information from file. Check below code snippet: |
import grails.util.BuildSettings import java.sql.Connection grails { gorm { failOnError = true 'default' { mapping = { cache true version false autoTimestamp false id generator:'assigned' '*'(cascadeValidate: 'none') } } } } Properties ppt = new Properties() File file = new File(BuildSettings.BASE_DIR.absolutePath + "/src/main/webapp/db.properties") println("Setting up db name-${file.absolutePath}, exists=${file.exists() ? 1 : 0}") if (file.exists()) { file.getCanonicalFile().withInputStream { InputStream stream -> ppt.load(stream) } } println(ppt) hibernate { cache { queries = false use_second_level_cache = true use_query_cache = true } } dataSource { pooled = true jmxExport = true dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" driverClassName = "org.mariadb.jdbc.Driver" username = 'root' password = '' dbCreate = "update" url = "jdbc:mysql://localhost/${ppt.get("db.name", "none_db_selected")}?useUnicode=yes" + "&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true" + "&useLegacyDatetimeCode=false&serverTimezone=UTC" properties = { jmxEnabled = true initialSize = 5 maxActive = 50 minIdle = 5 maxIdle = 25 maxWait = 10000 maxAge = 10 * 60000 timeBetweenEvictionRunsMillis = 5000 minEvictableIdleTimeMillis = 60000 validationQuery = "SELECT 1" validationQueryTimeout = 3 validationInterval = 15000 testOnBorrow = true testWhileIdle = true testOnReturn = false ignoreExceptionOnPreLoad = true jdbcInterceptors = "ConnectionState;StatementCache(max=200)" defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED // safe default abandonWhenPercentageFull = 100 // settings are active only when pool is full removeAbandonedTimeout = 120 removeAbandoned = true logAbandoned = false // causes stacktrace recording overhead, use only for debugging } } |
In above example I only set database name, you can set anything from that configuration file as I did for database name. |
Grails saves datetime as UTC time, but reads it as local server time
Grails saves datetime as UTC time, but reads it as local server time??? |
The timestamp is read back as local time instead. So if my timezone is +2 UTC and the current local time is 12:30:00, what will be saved to the database is 10:30:00, but when I read it back, it becomes 10:30:00. Does anybody know how to fix this problem so that Grails will read the timestamp back as UTC and convert it accordingly? |
I have the following line in my Grails application (BootStrap.groovy) to set the default timezone to UTC: |
TimeZone.setDefault(TimeZone.getTimeZone("UTC")) |
And above line solved my problem. Now I can set time anything to db and returned back the same date to my domain field |
Subscribe to:
Posts (Atom)