given the following dart code
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
code
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
class _HomeScreenState extends State {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
code
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Update the state to display the content in the lower text box
});
}
} else {
// User canceled the picker
print('User canceled file picker');
}
Your Dart code is structured as a Flutter application that utilizes various packages for file picking, window management, and UI design. It appears to be a desktop application designed to handle INI files, with specific UI features and functionalities. However, there are a few issues and improvements to address:
Issues
Incorrect Widget Property Usage:
In the button styles, you used WidgetStateProperty, which doesn't exist in Flutter. The correct usage is MaterialStateProperty.
Unused Upper Text Box:
You've declared a variable upperTextBoxContent, but it seems you haven't implemented any functionality to populate or utilize it.
Improvements
Button Styles:
Correct the usage to MaterialStateProperty for button styling.
Add Functionality to Upper Text Box:
You could add functionality to allow users to input or manipulate text in the upper text box before encoding or decoding it.
Interactive Elements:
Implement the logic for "Preview ini File" and "Save ini File" buttons, as they currently do nothing when pressed.
User Feedback:
Add some error handling or user feedback when an action is performed (like displaying a message when an INI file is successfully loaded).
Here's an updated version with the corrected button property usage:
dart
User
please add a functio so that when a file has been loaded into text box 2 of column 2 the loaded text is then base64 decoded and the decoded contents placed in text box 1 of column 2
Assistant
Sure! To achieve this, you need to add a function that decodes the loaded INI file content using Base64 and then places the decoded text into the upper text box. Here's how you can implement this function:
dart
User
getting error "Decoding error Invalid Base64 content". The content is base64 encoded would the fact its utf8 encoded make a difference?
Assistant
If you're receiving a "Decoding Error: Invalid Base64 content" message, it might be due to the way the Base64 string is being interpreted or stored. Let's address some potential issues and reasons why the decoding might fail:
UTF-8 Encoding: UTF-8 encoding will usually not affect the Base64 decoding unless the Base64 string itself has been altered (e.g., through character encoding/decoding operations that might change the nature of the string).
Proper Trimming: Ensure that the Base64 string does not contain extra spaces, newline characters, or any non-Base64 characters. This is particularly important if the content contains any headers or footers that you should remove before decoding.
Padding Issues: Ensure that your Base64 string has the correct padding. Base64 strings should have a length that is a multiple of 4. If they don't, they may need to be padded with = characters.
Reading the File: Make sure that the file is being read correctly and that the content is indeed a valid Base64 string.
Let's refine the code to attempt to handle these issues:
dart
User
still the same error. the ini file is deffinately base64?
Assistant
To troubleshoot the issue further, let's consider a few more possibilities:
Verify the Base64 Content: First, double-check that the file content you are trying to decode is indeed correctly Base64-encoded. You can use an online Base64 decoder to verify this — copy the content from the .ini file and decode it using an online tool.
Inspect the File Content: Make sure there are no additional headers, footers, or metadata in the .ini file that aren't part of the Base64 content but might accidentally get read in as well.
File Reading Issues: It's also important to make sure that the file is being read correctly without any corruption or unintended conversion.
Correcting Decoding Logic: Let's also ensure that all previously mentioned padding and trimming logic are correctly applied when decoding.
Here is a slightly modified approach to try addressing potential issues:
dart
User
new error lib/main.dart:68:67: Error: The
method 'trim' isn't defined for
the class 'Future'.
Assistant
The error you're encountering is due to trying to call the trim() method directly on a Future<String>. The File.readAsString() method returns a Future<String>, not a plain String, because file reading is asynchronous in Dart. To resolve this issue, you should await the result of readAsString() before calling trim().
Here's how you can fix the error:
Use await File(filePath).readAsString(); to wait for the file to be read into a string, then call trim() on the resulting string.
Ensure the _loadIniFile function is marked as async since it uses await.
Here's how you can update the code to fix the error:
dart
User
well done no errors and ini file now correctly base64 decoded
Assistant
Great! I'm glad to hear that the code is now working correctly and decoding the Base64 content from the INI file as you intended. If you have any more questions or need further assistance with your project, feel free to ask. Happy coding!
User
can we have the "Save ini File" button dissabled at start up
Assistant
Certainly! Disabling the "Save ini File" button at startup is straightforward. You can manage the enabled/disabled state of the button using a boolean variable in your state. Here's how you can do it:
Create a Boolean Variable: Add a boolean variable to control the enabled state of the button.
Initialize the Button State: Set the button to be disabled initially in the initState() method.
Enable the Button When Needed: Update the state to enable the button when you have the conditions for saving (like after loading or modifying the file).
Here's the modified code with these changes:
dart
User
can the button text be grey when dissabled a nd white when enabled
Assistant
Yes, you can customize the button's appearance based on its enabled or disabled state. In Flutter, ElevatedButton supports different states like enabled, disabled, hovered, etc., through the MaterialStateProperty class. You can specify different styles for these states, such as the foreground color.
Here's how you can achieve the desired behavior with code adjustments:
dart
User
The Save ini File button should not be enabled when the Load ini File button is actioned
Assistant
If you want the "Save ini File" button to remain disabled even after clicking the "Load ini File" button initially, you should ensure that the logic controlling when the "Save ini File" button gets enabled is separate from merely loading a file. The button will only enable upon some other condition, like a modification or a successful decoding.
Here's how you can define the initial state and then enable the "Save ini File" button only under specific conditions:
Initial State: Ensure the save button is always disabled after loading.
Enable Button: Define clearly when the button should be enabled (e.g., after decoding).
Here's how you can implement this logic:
dart
Not yet included in a published release.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Update the state to display the content in the lower text box
});
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
This corrected version uses MaterialStateProperty for button styles. Make sure to implement the desired logic for the other buttons and possibly add more features and error handling for a more robust application.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Update the state to display the content in the lower text box
_decodeBase64Content(fileContent); // Decode the content after loading it
});
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Decode the Base64 content
final decodedBytes = base64.decode(base64Content);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Key Changes:
Base64 Decoding Function:
Added _decodeBase64Content(String base64Content), which decodes the Base64 encoded content and updates the upperTextBoxContent.
Decode After Loading:
Call _decodeBase64Content(fileContent) within _loadIniFile, right after setting the lowerTextBoxContent.
Error Handling:
Catch errors when decoding Base64 content. If there’s an error, show an error message in the upper text box.
This modification will allow the application to decode the content of the loaded INI file (assuming the content is Base64 encoded) and display it in the upper text box.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Update the state to display the content in the lower text box
_decodeBase64Content(fileContent); // Decode the content after loading it
});
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim the content to remove any unauthorized characters
String trimContent = base64Content.trim();
// Make sure padding is correct
while (trimContent.length % 4 != 0) {
trimContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(trimContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Key Adjustments:
Trim and Pad: The code trims the content and ensures appropriate padding (= characters) when converting to Base64.
Error Handling: Proper try-catch around the decoding logic to catch any errors if the input isn't valid Base64.
Make sure your Base64 string is properly formatted and that any extraneous data is cleaned up before decoding. If the content is consistently failing, it's worth verifying the Base64 encoding and ensure it's not corrupted or formatted incorrectly.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
try {
// Read the file and get its content
final fileContent = await File(filePath).readAsString().trim();
setState(() {
lowerTextBoxContent = fileContent; // Display the raw content
_decodeBase64Content(fileContent); // Decode the content after loading it
});
} catch (e) {
print('Error reading file: $e');
}
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim and remove any unwanted characters
String cleanContent = base64Content.replaceAll(RegExp(r'\s'), '');
// Make sure padding is correct
while (cleanContent.length % 4 != 0) {
cleanContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(cleanContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
What to Check/Do Next:
Manual Testing: Copy the content of your .ini file and use a Base64 decoder tool to verify if it decodes correctly outside of this application.
Logging Content: Add print statements to see the exact content being processed before it's being decoded. This will help you identify if there are characters/issues that might not be immediately obvious.
Online Verification: Use an online Base64 decoder/encoder to compare results and ensure the content is correct. This might also help identify formatting or content issues not apparent in code alone.
If decoding works outside of the Flutter app, but not within it, there might be a file reading or other environment-specific issue.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
try {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Display the raw content
_decodeBase64Content(fileContent); // Decode the content after loading it
});
} catch (e) {
print('Error reading file: $e');
}
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim and remove any unwanted characters
String cleanContent = base64Content.replaceAll(RegExp(r'\s'), '');
// Make sure padding is correct
while (cleanContent.length % 4 != 0) {
cleanContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(cleanContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Key Changes:
Await the Result: Ensure you await the readAsString() call to get the string content, enabling you to use trim() after you've received the result.
Async/Await: Ensure that the asynchronous function _loadIniFile uses async to allow await calls within it.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
bool isSaveButtonEnabled = false; // Control the save button state
@override
void initState() {
super.initState();
isSaveButtonEnabled = false; // Initially disable the save button
}
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
try {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Display the raw content
_decodeBase64Content(fileContent); // Decode the content after loading it
isSaveButtonEnabled = true; // Enable the save button after loading a file
});
} catch (e) {
print('Error reading file: $e');
}
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim and remove any unwanted characters
String cleanContent = base64Content.replaceAll(RegExp(r'\s'), '');
// Make sure padding is correct
while (cleanContent.length % 4 != 0) {
cleanContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(cleanContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: isSaveButtonEnabled ? () {} : null, // Disable when not enabled
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Explanation:
Boolean Flag: The isSaveButtonEnabled boolean flag is used to manage the enabled state of the "Save ini File" button.
Conditional Button Enabling: The onPressed property of the "Save ini File" button checks if isSaveButtonEnabled is true. If it is, the button's onPressed function is enabled; otherwise, it's null, disabling the button.
State Update: After loading an INI file, the state is updated to enable the "Save ini File" button.
This setup lets you control the button state effectively, enabling it only when appropriate actions warrant it.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
bool isSaveButtonEnabled = false; // Control the save button state
@override
void initState() {
super.initState();
isSaveButtonEnabled = false; // Initially disable the save button
}
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
try {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Display the raw content
_decodeBase64Content(fileContent); // Decode the content after loading it
isSaveButtonEnabled = true; // Enable the save button after loading a file
});
} catch (e) {
print('Error reading file: $e');
}
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim and remove any unwanted characters
String cleanContent = base64Content.replaceAll(RegExp(r'\s'), '');
// Make sure padding is correct
while (cleanContent.length % 4 != 0) {
cleanContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(cleanContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: isSaveButtonEnabled ? () {} : null, // Disable when not enabled
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey; // Disabled state
}
return Colors.white; // Default enabled state
},
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Explanation:
Foreground Color Adjustment: The foregroundColor of the "Save ini File" button is managed using MaterialStateProperty.resolveWith(), which allows us to specify different colors for different states.
Enable and Disable Colors: The button's text color is set to Colors.grey when disabled and Colors.white when enabled.
This approach allows the button to visually indicate whether it's enabled or disabled by changing the text color appropriately.
import 'dart:convert'; // Import the dart:convert library for Base64 decoding
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
await windowManager.ensureInitialized();
windowManager.waitUntilReadyToShow().then((_) async {
// Set window title
await windowManager.setTitle('JMS AG J-TF ini File Utility');
// Set window size and make it non-resizable
await windowManager.setSize(const Size(846, 530)); // Adjust height to 530
await windowManager.setMinimumSize(const Size(846, 530));
await windowManager.setMaximumSize(const Size(846, 530));
// Show window
await windowManager.show();
await windowManager.focus();
});
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String upperTextBoxContent = ''; // Variable to hold the content of the upper text box
String lowerTextBoxContent = ''; // Variable to hold the content of the lower text box
bool isSaveButtonEnabled = false; // Control the save button state
void _loadIniFile() async {
// Open file picker
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['ini'], // Allow .ini files only
);
if (result != null) {
PlatformFile file = result.files.first; // Get the selected file
String? filePath = file.path; // Extract the file path
if (filePath != null) {
try {
// Read the file and get its content
final fileContent = await File(filePath).readAsString();
setState(() {
lowerTextBoxContent = fileContent; // Display the raw content
_decodeBase64Content(fileContent); // Decode the content after loading it
isSaveButtonEnabled = false; // Keep the save button disabled after loading a file
});
} catch (e) {
print('Error reading file: $e');
}
}
} else {
// User canceled the picker
print('User canceled file picker');
}
}
void _decodeBase64Content(String base64Content) {
try {
// Trim and remove any unwanted characters
String cleanContent = base64Content.replaceAll(RegExp(r'\s'), '');
// Make sure padding is correct
while (cleanContent.length % 4 != 0) {
cleanContent += '=';
}
// Decode the Base64 content
final decodedBytes = base64.decode(cleanContent);
final decodedContent = utf8.decode(decodedBytes);
setState(() {
upperTextBoxContent = decodedContent; // Update the upper text box with decoded content
isSaveButtonEnabled = true; // Enable the save button only after successful decoding
});
} catch (e) {
print('Error decoding Base64: $e');
setState(() {
upperTextBoxContent = 'Decoding Error: Invalid Base64 content';
isSaveButtonEnabled = false; // Ensure the save button remains disabled on error
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 30,
child: Container(
width: double.infinity,
color: const Color.fromRGBO(0, 128, 192, 1.0),
child: const Center(
child: Text(
'JMS AG J-TF ini File Utility',
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.white,
child: const Center(child: Text('Column 1')),
),
),
Container(
width: 410,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Plain Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 207,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
upperTextBoxContent, // Display upper text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
const SizedBox(height: 8),
const Text(
'Encoded Text',
style: TextStyle(
fontSize: 12, // Reduced font size
fontWeight: FontWeight.bold,
),
),
Container(
width: 410,
height: 132,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 16.0, 8.0), // Increased right padding for scrollbar
child: Text(
lowerTextBoxContent, // Display lower text box content
style: const TextStyle(fontSize: 12), // Font size 12
),
),
),
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10), // Move buttons up by 10px
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Preview ini File'),
),
ElevatedButton(
onPressed: isSaveButtonEnabled ? () {} : null, // Disable when not enabled
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey; // Disabled state
}
return Colors.white; // Default enabled state
},
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Save ini File'),
),
ElevatedButton(
onPressed: _loadIniFile, // Call the method to load .ini file
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
const Color.fromRGBO(0, 128, 192, 1.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
child: const Text('Load ini File'),
),
],
),
),
],
),
);
}
}
Key Adjustments:
Save Button State Handling: The "Save ini File" button is initially disabled upon loading a file. It only becomes enabled after a successful Base64 decoding.
Error Handling: The save button remains disabled if there's an error during decoding, ensuring that no incomplete or incorrect content gets saved.
With these changes, the "Save ini File" button will remain disabled initially when the "Load ini File" button is actioned and become enabled only under explicitly defined conditions (like successfully decoding content).