Tuesday, October 4, 2022

How to use static content / resource files in app root directory in grails

In Grails latest version like 4 and 5, it provides static assets management mechanism more standard way. We can use different files under the corresponding folders.
All the images will go under the images directory, js files go under the javascripts directory and CSS goes under the stylesheets directory. These files will be accessed by the URL path start with "/assets/".
Now, what if you don't want to use this structure or need to use the root path for some static file content.
For this, grails provide another option to manage static resources. So, what you can do is create the /resources/public directory under /src/main/ and use static resources there.
For example, if you had a file under /src/main/resources/public/images/example.jpg path then you can access that file using https://example.com/static/images/example.jpg.
This means, your file can be accessed with "/static/" URL by default. If you want to access the file in the app root directory, setup the following configuration under application.groovy

grails.resources.pattern = '/**'
Now, for the above example the file can be accessed with https://example.com/images/example.jpg URL and if the file is in /public/ directory that file can be accessed with root path https://example.com/file.js

Thursday, September 29, 2022

Tracking of Stackoverflow Exception from Grails / Gradle Project - Caused by: java.lang.StackOverflowError: null

In many cases we may face Stackoverflow exception and in my many cases I didn't find the exact point where this exception occurred.
Then I start checked my debug output and found that from UrlMappingUtils.java class in package org.grails.web.mapping is the reason of StackoverflowException rather than throwing the main Exception.
This is because of handling of Exception is not handled properly.
Then I set a breakpoint inside method public static String forwardRequestForUrlMappingInfo and found the original Exception from there.

Add resources and config files to your JAR using Gradle Build for Grails Application

How do I add config files or any other resources into my jar using gradle?
How to include everything inside src/main/java to compile

I have a project. Inside the project I have few files like x.properties, x.csv and many more under src/main/webapp package, I used gradle to compile, I didn’t see src/main/webapp directory in my build folder.

How can I include everything to build? Because I need to include contents inside src/main/webapp directory to be include in my jar so that resources can be accessible by who will use my jar.
So first step is to tell compiler to include files in jar file using below code snippet -
Add below code snippet to build.gradle:

sourceSets {
    main {
        resources {
            srcDir "src/main/webapp"
            include "*.properties"
            include "*.csv"
            exclude "*.png"
            exclude "a1"
            exclude "a2"

            srcDir "src/main/webapp/a1"
            exclude "**/*.text"
            include "**/*.csv"

            srcDir "src/main/webapp/a2"
            include "**/*.html"
        }
    }
}
My project structure is like below:
And the files stored in the generated jar file as below:
Now the process to use the resources file into the project, add below configuration to build.gradle file (which project will use jar created above):

copy{
    from(zipTree("path_to_jar_file/plain.jar"))
    into("./")
    include "src/main/webapp/**/*.*" [will extract all to root directory]
    include "src/main/webapp/kkk/*.*" [will extract all files in folder named kkk to root directory]
}
Above configuration will extract all file from (jar) "src/main/webapp" to projectRoot/src/main/webapp directory as below:


Wednesday, September 28, 2022

How To Use Gradients in Flutter with BoxDecoration and GradientAppBar

Color gradients take a starting color and position and ending color and position. Then it performs a transition between the colors. With consideration for color theory, they can make an application more visually interesting than a plain design.
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Gradient Example'),
      ),
      body: Center(
        child: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [
                  Colors.blue,
                  Colors.red,
                  Colors.green,
                  Colors.yellow
                ],
              )
          ),
          child: Center(
            child: Text(
              'Hello Gradient!',
              style: TextStyle(
                fontSize: 48.0,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Compile your code and have it run in an emulator:
This creates a linear gradient that starts at 0.0 of the way down the screen with blue then red then green and last yellow.

Create Script To Copy Files From One Folder To Another On Windows 10 - Batch file to copy files from one folder to another folder

The batch script, also known as batch file, actually refers to a list of several commands; whenever you double-click the file, the commands will be executed. If you want to copy files or folders from one folder to another automatically, creating a batch script is a nice choice. As for how to get the work done.
Yet, do you want to automatically move files from one folder to another? The copying and pasting process can be finished quickly by using the Windows command line.
You can create a batch file to copy data in a file from one place to another place on your computer automatically. The following steps are finished on Windows 10.

1. Open Windows search.
2. Type notepad.
3. Open the Notepad app.
4. Copy & paste the script into the new Notepad file.
5. Select File -> Save As.
6. Give it a name like copy_file.bat
7. Select All Files.
8. Click Save to finish creating the batch file.
Step four: type the following script in or copy & paste it into Notepad. This will copy source_file.txt to destination folder

@echo off
set "source=C:\Users\developer\Desktop\source_file.txt"
set "destination=C:\Users\developer\Desktop\"
xcopy /s %source% %destination% /Y
/b

Wednesday, September 21, 2022

Web Views Open Website URL in Flutter Example & Output

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.

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.