Fix vertical overflow by removing nested Scaffold architecture

- Remove nested Scaffold from ScoringPage (now uses LayoutBuilder)
- Use proper constraint-based height calculation instead of MediaQuery
- Maintain score preservation across navigation without nested scaffolds
- Fix viewport architecture to work within MainNavigationPage body
- Minor keypad layout adjustment (77%/23% flex ratio)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mikkeli Matlock
2025-08-24 13:28:43 +09:00
parent fd5a034ed4
commit 8f7d870985
16 changed files with 1289 additions and 86 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ plugins {
}
android {
namespace = "com.novoyuuparosk.archery_bookkeeper"
namespace = "com.novoyuuparosk.archery_scorer"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
@@ -21,7 +21,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.novoyuuparosk.archery_bookkeeper"
applicationId = "com.novoyuuparosk.archery_scorer"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
+1 -1
View File
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="archery_bookkeeper"
android:label="Archery scorer"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
@@ -1,4 +1,4 @@
package com.novoyuuparosk.archery_bookkeeper
package com.novoyuuparosk.archery_scorer
import io.flutter.embedding.android.FlutterActivity
+2 -2
View File
@@ -5,7 +5,7 @@
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Archery Bookkeeper</string>
<string>Archery scorer</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
@@ -13,7 +13,7 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>archery_bookkeeper</string>
<string>archery_scorer</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
+4 -4
View File
@@ -2,16 +2,16 @@ import 'package:flutter/material.dart';
import 'presentation/pages/main_navigation_page.dart';
void main() {
runApp(const ArcheryBookkeeperApp());
runApp(const ArcheryScorerApp());
}
class ArcheryBookkeeperApp extends StatelessWidget {
const ArcheryBookkeeperApp({super.key});
class ArcheryScorerApp extends StatelessWidget {
const ArcheryScorerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Archery Bookkeeper',
title: 'Archery scorer',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.green,
@@ -123,7 +123,7 @@ class _MainNavigationPageState extends State<MainNavigationPage> {
const Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Professional archery scoring with sophisticated end interaction system.',
'REPLACE ALL THESE TEXTS WITH LOCALISATION MARKERS!!',
style: TextStyle(
fontSize: 12,
color: Colors.grey,
+64 -64
View File
@@ -383,78 +383,78 @@ class _ScoringPageState extends State<ScoringPage> with SingleTickerProviderStat
animation: _keypadAnimation,
builder: (context, child) {
// Calculate viewport heights based on animation progress
// AppBar is in parent navigation, account for status bar and safe area
final availableHeight = (MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.top -
kToolbarHeight).clamp(200.0, double.infinity); // Minimum 200px
final keypadHeight = (availableHeight * 0.30 * _keypadAnimation.value).clamp(0.0, availableHeight * 0.30);
final upperViewportHeight = (availableHeight - keypadHeight).clamp(100.0, availableHeight);
return Scaffold(
body: GestureDetector(
onTap: _onBackgroundTapped,
behavior: HitTestBehavior.opaque,
child: Column(
children: [
// Upper viewport - Settings and Scoring grid (scrollable)
SizedBox(
height: upperViewportHeight,
child: Column(
children: [
// Settings toggles
Padding(
padding: const EdgeInsets.all(16.0),
child: ToggleSwitches(
arrowsPerEnd: arrowsPerEnd,
onArrowsPerEndChanged: _onArrowsPerEndChanged,
),
),
// Scoring grid (scrollable within upper viewport)
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ScoringGrid(
scores: scores,
// Since we're inside MainNavigationPage's Scaffold body, use LayoutBuilder to get actual constraints
return LayoutBuilder(
builder: (context, constraints) {
final availableHeight = constraints.maxHeight.clamp(200.0, double.infinity); // Minimum 200px
final keypadHeight = (availableHeight * 0.30 * _keypadAnimation.value).clamp(0.0, availableHeight * 0.30);
final upperViewportHeight = (availableHeight - keypadHeight).clamp(100.0, availableHeight);
return GestureDetector(
onTap: _onBackgroundTapped,
behavior: HitTestBehavior.opaque,
child: Column(
children: [
// Upper viewport - Settings and Scoring grid (scrollable)
SizedBox(
height: upperViewportHeight,
child: Column(
children: [
// Settings toggles
Padding(
padding: const EdgeInsets.all(16.0),
child: ToggleSwitches(
arrowsPerEnd: arrowsPerEnd,
endsPerSet: endsPerSet,
selectedRow: selectedRow,
selectedColumn: selectedColumn,
activeEndIndex: activeEndIndex,
onCellSelected: _onCellSelected,
onArrowsPerEndChanged: _onArrowsPerEndChanged,
),
),
),
],
),
),
// Lower viewport - Keypad (animated height)
if (keypadHeight > 0)
SizedBox(
height: keypadHeight,
child: Container(
decoration: BoxDecoration(
color: Colors.grey.shade100,
border: Border(
top: BorderSide(color: Colors.grey.shade300, width: 1),
// Scoring grid (scrollable within upper viewport)
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ScoringGrid(
scores: scores,
arrowsPerEnd: arrowsPerEnd,
endsPerSet: endsPerSet,
selectedRow: selectedRow,
selectedColumn: selectedColumn,
activeEndIndex: activeEndIndex,
onCellSelected: _onCellSelected,
),
),
),
),
child: Opacity(
opacity: _keypadAnimation.value,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ScoringKeypad(
onScoreInput: _onScoreInput,
],
),
),
// Lower viewport - Keypad (animated height)
if (keypadHeight > 0)
SizedBox(
height: keypadHeight,
child: Container(
decoration: BoxDecoration(
color: Colors.grey.shade100,
border: Border(
top: BorderSide(color: Colors.grey.shade300, width: 1),
),
),
child: Opacity(
opacity: _keypadAnimation.value,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ScoringKeypad(
onScoreInput: _onScoreInput,
),
),
),
),
),
),
],
),
),
],
),
);
},
);
},
);
@@ -32,9 +32,9 @@ class ScoringKeypad extends StatelessWidget {
// Use Flex widgets for proper percentage distribution
return Row(
children: [
// Score buttons section (75% width)
// Score buttons section (77% width)
Expanded(
flex: 75,
flex: 77,
child: Column(
children: List.generate(4, (rowIndex) {
return Expanded(
@@ -59,9 +59,9 @@ class ScoringKeypad extends StatelessWidget {
const SizedBox(width: 2),
// Functional buttons section (25% width)
// Functional buttons section (23% width)
Expanded(
flex: 25,
flex: 23,
child: Column(
children: List.generate(4, (index) {
final buttonText = functionalButtons[index];
+2 -2
View File
@@ -1,5 +1,5 @@
name: archery_bookkeeper
description: "Archery scoring companion."
name: archery_scorer
description: "Archery scorer - A Flutter application."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
+11 -5
View File
@@ -5,16 +5,22 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:archery_bookkeeper/main.dart';
import 'package:archery_scorer/main.dart';
void main() {
testWidgets('Archery Bookkeeper app loads', (WidgetTester tester) async {
testWidgets('App loads successfully with basic structure', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const ArcheryBookkeeperApp());
await tester.pumpWidget(const ArcheryScorerApp());
// Verify that the app loads properly
expect(find.text('Archery Scoring'), findsOneWidget);
// Verify core app structure exists - completely localization-safe
expect(find.byType(MaterialApp), findsOneWidget);
expect(find.byType(Scaffold), findsAtLeastNWidgets(1));
expect(find.byType(AppBar), findsAtLeastNWidgets(1));
// Verify drawer menu button by icon type (drawer hamburger menu)
expect(find.byIcon(Icons.menu), findsOneWidget);
});
}
+253
View File
@@ -0,0 +1,253 @@
# Flutter Rename Tool
A comprehensive Python script to rename Flutter applications across all platform configurations. This tool handles package names, display names, class names, and bundle identifiers consistently across Android, iOS, and Flutter files.
## Features
- **Automatic detection** of current app configuration across all platforms
- **Default value support** - Press ENTER to keep current values unchanged
- **Smart change detection** - Only proceed if changes are actually needed
- **Comprehensive validation** for package names and app display names
- **Synchronized updates** across all platform files:
- `pubspec.yaml` - Package name and description
- `lib/main.dart` - App title and main class name
- `android/app/src/main/AndroidManifest.xml` - Android app label
- `android/app/build.gradle.kts` - Android namespace and applicationId
- `ios/Runner/Info.plist` - iOS display name and bundle name
- **Automatic Flutter refresh** - Runs `flutter clean` and `flutter pub get` after successful rename
- **Preview and confirmation** before making changes
- **Safe execution** with validation and error handling
## Usage
### Interactive Mode
Run the script from your Flutter project root:
```bash
python tools/flutter_rename.py
```
The script will:
1. Detect your current app configuration
2. Prompt for new package name and app display name
3. Show a preview of all changes
4. Ask for confirmation before applying changes
5. Update all relevant files
### Example Session
```
Flutter App Renaming Tool
============================================================
============================================================
CURRENT APP CONFIGURATION
============================================================
Package Name : archery_bookkeeper
Description : Archery scoring companion.
App Title : Archery Bookkeeper
Main Class : ArcheryBookkeeperApp
Android Label : archery_bookkeeper
Ios Display Name : Archery Bookkeeper
Ios Bundle Name : archery_bookkeeper
============================================================
============================================================
ENTER NEW APP CONFIGURATION
============================================================
Press ENTER to keep current value, or type new value:
New package name (current: archery_scorer): archery_scorecard
New app display name (current: Archery Scoresheet): Archery Scorecard
============================================================
PREVIEW OF CHANGES
============================================================
Package Name : archery_bookkeeper → archery_scorecard
App Display Name : Archery Bookkeeper → Archery Scorecard
Main Class Name : ArcheryBookkeeperApp → ArcheryScorecardApp
============================================================
Files to be updated:
✓ pubspec.yaml
✓ lib/main.dart
✓ android/app/src/main/AndroidManifest.xml
✓ android/app/build.gradle.kts
✓ ios/Runner/Info.plist
This operation will modify the above files.
Proceed with renaming? (y/N): y
Applying changes...
----------------------------------------
✓ Updated pubspec.yaml
✓ Updated main.dart
✓ Updated Android manifest
✓ Updated Android build.gradle.kts
✓ Moved and updated MainActivity.kt
From: J:\dev\archery_bookkeeper\android\app\src\main\kotlin\com\novoyuuparosk\old_app
To: J:\dev\archery_bookkeeper\android\app\src\main\kotlin\com\novoyuuparosk\new_app
✓ Updated test files: widget_test.dart
✓ Updated iOS Info.plist
----------------------------------------
Rename completed: 7/7 files updated successfully
✓ All files updated successfully!
Refreshing Flutter project...
----------------------------------------
Running 'flutter clean'...
✓ flutter clean completed
Running 'flutter pub get'...
✓ flutter pub get completed
============================================================
✓ Project refresh completed successfully!
Your Flutter app has been renamed and is ready to use.
You can now run 'flutter run' to test the app.
```
## Validation Rules
### Package Name
- Must start with a lowercase letter
- Can contain only lowercase letters, numbers, and underscores
- Cannot start or end with underscore
- Cannot contain consecutive underscores
- Cannot be a Dart reserved word
### App Display Name
- Must be at least 2 characters long
- Cannot exceed 50 characters
- Cannot be empty or contain only whitespace
## Files Modified
The script updates the following files:
1. **`pubspec.yaml`**
- `name:` field (package name)
- `description:` field (generated from app name)
2. **`lib/main.dart`**
- `title:` in MaterialApp (app display name)
- Main class name and constructor
- `runApp()` instantiation
3. **`android/app/src/main/AndroidManifest.xml`**
- `android:label` attribute in application tag
4. **`android/app/build.gradle.kts`**
- `namespace` (Android namespace)
- `applicationId` (Android application identifier)
5. **`android/app/src/main/kotlin/.../MainActivity.kt`**
- Updates package declaration
- Moves file to match new package structure
6. **`test/**/*.dart`**
- Updates package imports in test files
- Updates class name references in tests
7. **`ios/Runner/Info.plist`**
- `CFBundleDisplayName` (iOS display name)
- `CFBundleName` (iOS bundle name)
## Testing
Test the detection and validation functionality:
```bash
python tools/test_detection.py
```
This will show current configuration detection and test validation rules.
## User Experience Features
### Default Values
When prompted for input, simply press ENTER to keep the current value:
```
New package name (current: archery_scorer):
Using current value: archery_scorer
```
### Smart Change Detection
The script detects if no actual changes are needed:
```
============================================================
No changes detected - configuration is already up to date.
============================================================
```
### Automatic Project Refresh
After successful renaming, the script automatically runs:
- `flutter clean` to clear build cache
- `flutter pub get` to refresh dependencies
No manual steps required - your app is ready to run immediately.
## Post-Rename Steps
If automatic Flutter refresh succeeds:
1. **Test the application**: `flutter run`
2. **Verify on all target platforms** (Android, iOS, Web, etc.)
If automatic refresh fails (fallback):
1. **Clean build cache**: `flutter clean`
2. **Refresh dependencies**: `flutter pub get`
3. **Test the application**: `flutter run`
4. **Verify on all target platforms**
## Safety Features
- **Non-destructive preview** - Shows exactly what will change before applying
- **File existence checks** - Warns about missing files
- **Comprehensive validation** - Prevents invalid names
- **Error handling** - Reports specific issues if updates fail
- **Backup recommendation** - Always commit your changes to version control first
## Requirements
- Python 3.8 or higher
- Must be run from Flutter project root directory
- All target platform files should exist (created by `flutter create`)
## Troubleshooting
**Issue**: "File not found" warnings
- **Solution**: Ensure you're running from the Flutter project root directory
**Issue**: Some files not updated
- **Solution**: Check file permissions and ensure files are not read-only
**Issue**: Invalid package name error
- **Solution**: Follow Dart package naming conventions (lowercase, underscores only)
**Issue**: App doesn't reflect changes after rename
- **Solution**: Run `flutter clean` and `flutter pub get`, then rebuild
**Issue**: Android app crashes with ClassNotFoundException after rename
- **Solution**: This indicates the MainActivity wasn't moved to the new package structure - the script now handles this automatically
**Issue**: Tests fail after renaming due to old package imports or class names
- **Solution**: The script now automatically updates test files with new imports and class references
## Best Practices
### Test Writing for Renamed Apps
When writing tests, use structural widget checks instead of text-based assertions:
```dart
// Good - completely localization-safe and robust
expect(find.byType(MaterialApp), findsOneWidget);
expect(find.byType(Scaffold), findsAtLeastNWidgets(1));
expect(find.byIcon(Icons.menu), findsOneWidget);
expect(find.byKey(const Key('specific-widget')), findsOneWidget);
// Avoid - brittle and breaks with localization
expect(find.text('Specific Text'), findsOneWidget);
expect(find.byTooltip('Open navigation menu'), findsOneWidget);
```
+756
View File
@@ -0,0 +1,756 @@
#!/usr/bin/env python3
"""
Flutter App Renaming Tool
A comprehensive script to rename Flutter applications across all platform configurations.
Handles package names, display names, class names, and bundle identifiers consistently.
Usage: python flutter_rename.py
"""
import os
import re
import json
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import Dict, List, Optional, Tuple
class FlutterRenamer:
def __init__(self, project_root: str = "."):
self.project_root = Path(project_root).resolve()
self.current_config = {}
def detect_current_configuration(self) -> Dict[str, str]:
"""Detect current app configuration across all platform files."""
config = {}
# Read pubspec.yaml
pubspec_path = self.project_root / "pubspec.yaml"
if pubspec_path.exists():
with open(pubspec_path, 'r', encoding='utf-8') as f:
content = f.read()
if match := re.search(r'^name:\s*(.+)$', content, re.MULTILINE):
config['package_name'] = match.group(1).strip()
if match := re.search(r'^description:\s*["\']?([^"\']+)["\']?$', content, re.MULTILINE):
config['description'] = match.group(1).strip()
# Read main.dart
main_dart_path = self.project_root / "lib" / "main.dart"
if main_dart_path.exists():
with open(main_dart_path, 'r', encoding='utf-8') as f:
content = f.read()
if match := re.search(r'title:\s*["\']([^"\']+)["\']', content):
config['app_title'] = match.group(1)
if match := re.search(r'class\s+(\w+App)\s+extends', content):
config['main_class'] = match.group(1)
# Read Android manifest
android_manifest_path = self.project_root / "android" / "app" / "src" / "main" / "AndroidManifest.xml"
if android_manifest_path.exists():
try:
with open(android_manifest_path, 'r', encoding='utf-8') as f:
content = f.read()
if match := re.search(r'android:label="([^"]*)"', content):
config['android_label'] = match.group(1)
except Exception:
pass
# Read Android build.gradle.kts
android_gradle_path = self.project_root / "android" / "app" / "build.gradle.kts"
if android_gradle_path.exists():
try:
with open(android_gradle_path, 'r', encoding='utf-8') as f:
content = f.read()
if match := re.search(r'namespace\s*=\s*"([^"]*)"', content):
config['android_namespace'] = match.group(1)
if match := re.search(r'applicationId\s*=\s*"([^"]*)"', content):
config['android_application_id'] = match.group(1)
except Exception:
pass
# Read iOS Info.plist
ios_plist_path = self.project_root / "ios" / "Runner" / "Info.plist"
if ios_plist_path.exists():
try:
with open(ios_plist_path, 'r', encoding='utf-8') as f:
content = f.read()
if match := re.search(r'<key>CFBundleDisplayName</key>\s*<string>([^<]+)</string>', content):
config['ios_display_name'] = match.group(1)
if match := re.search(r'<key>CFBundleName</key>\s*<string>([^<]+)</string>', content):
config['ios_bundle_name'] = match.group(1)
except Exception:
pass
self.current_config = config
return config
def validate_package_name(self, package_name: str) -> Tuple[bool, str]:
"""Validate Flutter package name format."""
if not package_name:
return False, "Package name cannot be empty"
if not re.match(r'^[a-z][a-z0-9_]*$', package_name):
return False, "Package name must start with lowercase letter and contain only lowercase letters, numbers, and underscores"
if package_name.startswith('_') or package_name.endswith('_'):
return False, "Package name cannot start or end with underscore"
if '__' in package_name:
return False, "Package name cannot contain consecutive underscores"
reserved_words = {'abstract', 'as', 'assert', 'async', 'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'default', 'deferred', 'do', 'dynamic', 'else', 'enum', 'export', 'extends', 'external', 'factory', 'false', 'final', 'finally', 'for', 'function', 'get', 'hide', 'if', 'implements', 'import', 'in', 'interface', 'is', 'library', 'mixin', 'new', 'null', 'on', 'operator', 'part', 'rethrow', 'return', 'set', 'show', 'static', 'super', 'switch', 'sync', 'this', 'throw', 'true', 'try', 'typedef', 'var', 'void', 'while', 'with', 'yield'}
if package_name in reserved_words:
return False, f"'{package_name}' is a reserved word and cannot be used as package name"
return True, "Valid package name"
def validate_app_name(self, app_name: str) -> Tuple[bool, str]:
"""Validate app display name."""
if not app_name or not app_name.strip():
return False, "App name cannot be empty"
if len(app_name.strip()) < 2:
return False, "App name must be at least 2 characters long"
if len(app_name.strip()) > 50:
return False, "App name should not exceed 50 characters"
return True, "Valid app name"
def generate_class_name(self, app_name: str) -> str:
"""Generate main class name from app display name."""
# Remove special characters and convert to PascalCase
clean_name = re.sub(r'[^a-zA-Z0-9\s]', '', app_name)
words = clean_name.split()
class_name = ''.join(word.capitalize() for word in words if word)
if not class_name:
class_name = "App"
elif not class_name.endswith('App'):
class_name += "App"
return class_name
def generate_android_package_id(self, package_name: str) -> str:
"""Generate Android package identifier from Flutter package name."""
# Extract the current domain from existing Android package if available
current_android = self.current_config.get('android_namespace', '')
if current_android:
# Try to extract domain part (everything before the last segment)
parts = current_android.split('.')
if len(parts) > 1:
domain = '.'.join(parts[:-1])
return f"{domain}.{package_name}"
# Default domain if no existing Android package found
return f"com.example.{package_name}"
def update_pubspec_yaml(self, package_name: str, app_name: str) -> bool:
"""Update pubspec.yaml with new package name and description."""
pubspec_path = self.project_root / "pubspec.yaml"
if not pubspec_path.exists():
print(f"Warning: {pubspec_path} not found")
return False
try:
with open(pubspec_path, 'r', encoding='utf-8') as f:
content = f.read()
# Update package name
content = re.sub(r'^name:\s*.*$', f'name: {package_name}', content, flags=re.MULTILINE)
# Update description
description = f'"{app_name} - A Flutter application."'
content = re.sub(r'^description:\s*.*$', f'description: {description}', content, flags=re.MULTILINE)
with open(pubspec_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Updated pubspec.yaml")
return True
except Exception as e:
print(f"✗ Error updating pubspec.yaml: {e}")
return False
def update_main_dart(self, package_name: str, app_name: str, class_name: str) -> bool:
"""Update main.dart with new app title and class name."""
main_dart_path = self.project_root / "lib" / "main.dart"
if not main_dart_path.exists():
print(f"Warning: {main_dart_path} not found")
return False
try:
with open(main_dart_path, 'r', encoding='utf-8') as f:
content = f.read()
# Update class name
old_class_pattern = r'class\s+\w+App\s+extends'
content = re.sub(old_class_pattern, f'class {class_name} extends', content)
# Update constructor
old_constructor_pattern = r'const\s+\w+App\('
content = re.sub(old_constructor_pattern, f'const {class_name}(', content)
# Update main() instantiation
old_instantiation_pattern = r'runApp\(const\s+\w+App\(\)\)'
content = re.sub(old_instantiation_pattern, f'runApp(const {class_name}())', content)
# Update title
content = re.sub(r'title:\s*["\'][^"\']*["\']', f"title: '{app_name}'", content)
with open(main_dart_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Updated main.dart")
return True
except Exception as e:
print(f"✗ Error updating main.dart: {e}")
return False
def update_android_manifest(self, app_name: str) -> bool:
"""Update Android manifest with new app label."""
manifest_path = self.project_root / "android" / "app" / "src" / "main" / "AndroidManifest.xml"
if not manifest_path.exists():
print(f"Warning: {manifest_path} not found")
return False
try:
with open(manifest_path, 'r', encoding='utf-8') as f:
content = f.read()
# Update android:label
content = re.sub(
r'android:label="[^"]*"',
f'android:label="{app_name}"',
content
)
with open(manifest_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Updated Android manifest")
return True
except Exception as e:
print(f"✗ Error updating Android manifest: {e}")
return False
def update_ios_info_plist(self, package_name: str, app_name: str) -> bool:
"""Update iOS Info.plist with new bundle and display names."""
plist_path = self.project_root / "ios" / "Runner" / "Info.plist"
if not plist_path.exists():
print(f"Warning: {plist_path} not found")
return False
try:
with open(plist_path, 'r', encoding='utf-8') as f:
content = f.read()
# Update CFBundleDisplayName
content = re.sub(
r'(<key>CFBundleDisplayName</key>\s*<string>)[^<]*(</string>)',
f'\\g<1>{app_name}\\g<2>',
content
)
# Update CFBundleName
content = re.sub(
r'(<key>CFBundleName</key>\s*<string>)[^<]*(</string>)',
f'\\g<1>{package_name}\\g<2>',
content
)
with open(plist_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Updated iOS Info.plist")
return True
except Exception as e:
print(f"✗ Error updating iOS Info.plist: {e}")
return False
def update_android_gradle(self, android_package_id: str) -> bool:
"""Update Android build.gradle.kts with new namespace and applicationId."""
gradle_path = self.project_root / "android" / "app" / "build.gradle.kts"
if not gradle_path.exists():
print(f"Warning: {gradle_path} not found")
return False
try:
with open(gradle_path, 'r', encoding='utf-8') as f:
content = f.read()
# Update namespace
content = re.sub(
r'namespace\s*=\s*"[^"]*"',
f'namespace = "{android_package_id}"',
content
)
# Update applicationId
content = re.sub(
r'applicationId\s*=\s*"[^"]*"',
f'applicationId = "{android_package_id}"',
content
)
with open(gradle_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Updated Android build.gradle.kts")
return True
except Exception as e:
print(f"✗ Error updating Android build.gradle.kts: {e}")
return False
def update_android_package_structure(self, old_package_id: str, new_package_id: str) -> bool:
"""Update Android package directory structure and MainActivity package."""
import shutil
if not old_package_id or not new_package_id or old_package_id == new_package_id:
return True # No change needed
# Convert package IDs to directory paths
old_path_parts = old_package_id.split('.')
new_path_parts = new_package_id.split('.')
old_package_dir = self.project_root / "android" / "app" / "src" / "main" / "kotlin" / Path(*old_path_parts)
new_package_dir = self.project_root / "android" / "app" / "src" / "main" / "kotlin" / Path(*new_path_parts)
try:
# Check if old directory exists
if not old_package_dir.exists():
print(f"Warning: Old Android package directory not found: {old_package_dir}")
return True # Might be a new project or different structure
# Create new directory structure
new_package_dir.parent.mkdir(parents=True, exist_ok=True)
# Move MainActivity.kt and update its package declaration
mainactivity_file = old_package_dir / "MainActivity.kt"
if mainactivity_file.exists():
# Read and update MainActivity.kt
with open(mainactivity_file, 'r', encoding='utf-8') as f:
content = f.read()
# Update package declaration
content = re.sub(
r'^package\s+[a-z0-9_.]+',
f'package {new_package_id}',
content,
flags=re.MULTILINE
)
# Write to new location
new_mainactivity_file = new_package_dir / "MainActivity.kt"
new_package_dir.mkdir(parents=True, exist_ok=True)
with open(new_mainactivity_file, 'w', encoding='utf-8') as f:
f.write(content)
# Remove old file
mainactivity_file.unlink()
print(f"✓ Moved and updated MainActivity.kt")
print(f" From: {old_package_dir}")
print(f" To: {new_package_dir}")
# Clean up empty old directories
try:
current_dir = old_package_dir
while current_dir != current_dir.parent and current_dir.name != "kotlin":
if current_dir.exists() and not any(current_dir.iterdir()):
current_dir.rmdir()
current_dir = current_dir.parent
else:
break
except OSError:
pass # Directory not empty or other issue, that's fine
return True
except Exception as e:
print(f"✗ Error updating Android package structure: {e}")
return False
def update_test_files(self, old_package_name: str, new_package_name: str, old_class_name: str, new_class_name: str) -> bool:
"""Update test files with new package imports and class names."""
test_dir = self.project_root / "test"
if not test_dir.exists():
return True # No test directory, nothing to update
try:
# Find all Dart files in test directory
test_files = list(test_dir.rglob("*.dart"))
if not test_files:
return True # No test files found
updated_files = []
for test_file in test_files:
try:
with open(test_file, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
# Update package imports
if old_package_name and new_package_name:
content = re.sub(
rf"import\s+['\"]package:{re.escape(old_package_name)}/",
f"import 'package:{new_package_name}/",
content
)
# Update class references
if old_class_name and new_class_name and old_class_name != new_class_name:
content = re.sub(
rf"\b{re.escape(old_class_name)}\b",
new_class_name,
content
)
# Write back if changed
if content != original_content:
with open(test_file, 'w', encoding='utf-8') as f:
f.write(content)
updated_files.append(test_file.name)
except Exception as e:
print(f"⚠ Warning: Could not update test file {test_file}: {e}")
if updated_files:
print(f"✓ Updated test files: {', '.join(updated_files)}")
else:
print("✓ No test files needed updating")
return True
except Exception as e:
print(f"✗ Error updating test files: {e}")
return False
def find_flutter_executable(self) -> str:
"""Find Flutter executable, handling Windows PATH issues."""
import subprocess
import os
# First try the simple approach
try:
result = subprocess.run(
["flutter", "--version"],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
return "flutter"
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
# Try finding flutter with 'where' command on Windows
try:
result = subprocess.run(
["where", "flutter"],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0 and result.stdout.strip():
lines = result.stdout.strip().split('\n')
# Prefer .bat files on Windows
for line in lines:
if line.endswith('.bat'):
return line
# Fall back to first result if no .bat found
return lines[0]
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
# Common Flutter installation paths
common_paths = [
"J:\\dev\\flutter-sdk\\bin\\flutter.bat",
"C:\\flutter\\bin\\flutter.bat",
"C:\\src\\flutter\\bin\\flutter.bat",
]
for path in common_paths:
if os.path.exists(path):
return path
return None
def run_flutter_commands(self, auto_refresh: bool = True) -> bool:
"""Run Flutter clean and pub get to refresh the project."""
import subprocess
if not auto_refresh:
print("Skipping automatic Flutter refresh as requested.")
return True
print("\nRefreshing Flutter project...")
print("-" * 40)
# Find Flutter executable
flutter_cmd = self.find_flutter_executable()
if not flutter_cmd:
print("✗ Flutter executable not found.")
print(" Please ensure Flutter is installed and accessible.")
print(" You can manually run: flutter clean && flutter pub get")
return False
print(f"Using Flutter: {flutter_cmd}")
try:
# Run flutter clean
print("Running 'flutter clean'...")
result = subprocess.run(
[flutter_cmd, "clean"],
cwd=self.project_root,
capture_output=True,
text=True,
timeout=60
)
if result.returncode == 0:
print("✓ flutter clean completed")
else:
print(f"⚠ flutter clean warning: {result.stderr.strip()}")
# Run flutter pub get
print("Running 'flutter pub get'...")
result = subprocess.run(
[flutter_cmd, "pub", "get"],
cwd=self.project_root,
capture_output=True,
text=True,
timeout=120
)
if result.returncode == 0:
print("✓ flutter pub get completed")
return True
else:
print(f"✗ flutter pub get failed: {result.stderr.strip()}")
return False
except subprocess.TimeoutExpired:
print("✗ Flutter commands timed out")
return False
except Exception as e:
print(f"✗ Error running Flutter commands: {e}")
return False
def display_current_config(self):
"""Display current app configuration."""
print("\n" + "="*60)
print("CURRENT APP CONFIGURATION")
print("="*60)
if not self.current_config:
print("No configuration detected")
return
for key, value in self.current_config.items():
formatted_key = key.replace('_', ' ').title()
print(f"{formatted_key:20}: {value}")
print("="*60)
def get_user_input(self) -> Optional[Tuple[str, str]]:
"""Get new configuration from user input."""
print("\n" + "="*60)
print("ENTER NEW APP CONFIGURATION")
print("="*60)
print("Press ENTER to keep current value, or type new value:")
print()
# Get package name
while True:
current_package = self.current_config.get('package_name', 'unknown')
package_name = input(f"New package name (current: {current_package}): ").strip()
# Use current value if empty input
if not package_name:
package_name = current_package
print(f"Using current value: {package_name}")
if package_name == 'unknown':
print("Package name is required and current value is not available.")
continue
valid, message = self.validate_package_name(package_name)
if valid:
break
else:
print(f"Invalid package name: {message}")
# Get app display name
while True:
current_title = self.current_config.get('app_title', 'unknown')
app_name = input(f"New app display name (current: {current_title}): ").strip()
# Use current value if empty input
if not app_name:
app_name = current_title
print(f"Using current value: {app_name}")
if app_name == 'unknown':
print("App name is required and current value is not available.")
continue
valid, message = self.validate_app_name(app_name)
if valid:
break
else:
print(f"Invalid app name: {message}")
return package_name, app_name
def confirm_changes(self, package_name: str, app_name: str, class_name: str, android_package_id: str) -> bool:
"""Show preview and confirm changes."""
print("\n" + "="*60)
print("PREVIEW OF CHANGES")
print("="*60)
print(f"Package Name : {self.current_config.get('package_name', 'N/A')} → {package_name}")
print(f"App Display Name : {self.current_config.get('app_title', 'N/A')} → {app_name}")
print(f"Main Class Name : {self.current_config.get('main_class', 'N/A')} → {class_name}")
print(f"Android Package ID : {self.current_config.get('android_namespace', 'N/A')} → {android_package_id}")
print("="*60)
print("\nFiles to be updated:")
files_to_update = [
"pubspec.yaml",
"lib/main.dart",
"android/app/src/main/AndroidManifest.xml",
"android/app/build.gradle.kts",
"android/app/src/main/kotlin/.../MainActivity.kt",
"ios/Runner/Info.plist",
"test/**/*.dart (if present)"
]
for file_path in files_to_update:
full_path = self.project_root / file_path
status = "✓" if full_path.exists() else "⚠ (not found)"
print(f" {status} {file_path}")
print("\nThis operation will modify the above files.")
confirm = input("Proceed with renaming? (y/N): ").strip().lower()
return confirm in ('y', 'yes')
def run_rename(self) -> bool:
"""Execute the complete rename process."""
print("Flutter App Renaming Tool")
print("="*60)
# Detect current configuration
self.detect_current_configuration()
self.display_current_config()
# Get user input
user_input = self.get_user_input()
if not user_input:
print("Rename cancelled.")
return False
package_name, app_name = user_input
class_name = self.generate_class_name(app_name)
android_package_id = self.generate_android_package_id(package_name)
# Check if anything actually changed
no_changes = (
package_name == self.current_config.get('package_name', '') and
app_name == self.current_config.get('app_title', '') and
class_name == self.current_config.get('main_class', '') and
android_package_id == self.current_config.get('android_namespace', '')
)
if no_changes:
print("\n" + "="*60)
print("No changes detected - configuration is already up to date.")
print("="*60)
return True
# Confirm changes
if not self.confirm_changes(package_name, app_name, class_name, android_package_id):
print("Rename cancelled.")
return False
print("\nApplying changes...")
print("-" * 40)
# Apply all updates
success_count = 0
total_updates = 7
if self.update_pubspec_yaml(package_name, app_name):
success_count += 1
if self.update_main_dart(package_name, app_name, class_name):
success_count += 1
if self.update_android_manifest(app_name):
success_count += 1
if self.update_android_gradle(android_package_id):
success_count += 1
# Update Android package structure if namespace changed
old_android_package = self.current_config.get('android_namespace', '')
if self.update_android_package_structure(old_android_package, android_package_id):
success_count += 1
if self.update_ios_info_plist(package_name, app_name):
success_count += 1
# Update test files
old_package_name = self.current_config.get('package_name', '')
old_class_name = self.current_config.get('main_class', '')
if self.update_test_files(old_package_name, package_name, old_class_name, class_name):
success_count += 1
print("-" * 40)
print(f"Rename completed: {success_count}/{total_updates} files updated successfully")
if success_count == total_updates:
print("\n✓ All files updated successfully!")
# Ask about automatic Flutter refresh
auto_refresh = input("\nRun automatic Flutter refresh (flutter clean && flutter pub get)? (Y/n): ").strip().lower()
should_refresh = auto_refresh not in ('n', 'no')
# Automatically run Flutter commands to refresh the project
flutter_success = self.run_flutter_commands(should_refresh)
print("\n" + "="*60)
if flutter_success:
print("✓ Project refresh completed successfully!")
print("\nYour Flutter app has been renamed and is ready to use.")
print("You can now run 'flutter run' to test the app.")
else:
print("⚠ Manual Flutter refresh needed:")
print("1. Run 'flutter clean' to clear build cache")
print("2. Run 'flutter pub get' to refresh dependencies")
print("3. Test the app with 'flutter run'")
return True
else:
print(f"\n⚠ Some files could not be updated. Please check the errors above.")
return False
def main():
"""Main entry point."""
try:
renamer = FlutterRenamer()
renamer.run_rename()
except KeyboardInterrupt:
print("\n\nRename cancelled by user.")
except Exception as e:
print(f"\nUnexpected error: {e}")
if __name__ == "__main__":
main()
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
"""
Test Android package structure fix.
"""
from flutter_rename import FlutterRenamer
from pathlib import Path
def test_android_structure_update():
"""Test Android package structure detection and fix logic."""
print("Testing Android package structure update...")
renamer = FlutterRenamer()
config = renamer.detect_current_configuration()
print(f"Current Android namespace: {config.get('android_namespace', 'N/A')}")
# Check current MainActivity location
old_package = config.get('android_namespace', '')
if old_package:
old_parts = old_package.split('.')
old_package_dir = renamer.project_root / "android" / "app" / "src" / "main" / "kotlin" / Path(*old_parts)
mainactivity_file = old_package_dir / "MainActivity.kt"
print(f"MainActivity location: {mainactivity_file}")
print(f"MainActivity exists: {mainactivity_file.exists()}")
if mainactivity_file.exists():
with open(mainactivity_file, 'r') as f:
content = f.read()
print(f"Current package declaration: {content.split()[1] if 'package' in content else 'Not found'}")
# Test what would happen with a rename
new_package = "com.novoyuuparosk.test_app"
print(f"\nTesting rename to: {new_package}")
new_parts = new_package.split('.')
new_package_dir = renamer.project_root / "android" / "app" / "src" / "main" / "kotlin" / Path(*new_parts)
print(f"New MainActivity location would be: {new_package_dir / 'MainActivity.kt'}")
if __name__ == "__main__":
test_android_structure_update()
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
"""
Test script to verify Flutter app configuration detection.
"""
from flutter_rename import FlutterRenamer
def main():
print("Testing Flutter app configuration detection...")
print("=" * 60)
renamer = FlutterRenamer()
config = renamer.detect_current_configuration()
print("Detected configuration:")
for key, value in config.items():
formatted_key = key.replace('_', ' ').title()
print(f" {formatted_key:20}: {value}")
print("\nValidation tests:")
# Test package name validation
test_packages = ['archery_scorecard', 'my-app', 'MyApp', 'archery__scorecard', '_archery', 'archery_', 'class']
for pkg in test_packages:
valid, msg = renamer.validate_package_name(pkg)
status = "✓" if valid else "✗"
print(f" {status} '{pkg}': {msg}")
# Test app name validation
test_names = ['Archery Scorecard', '', 'A', 'A' * 60]
for name in test_names:
valid, msg = renamer.validate_app_name(name)
status = "✓" if valid else "✗"
print(f" {status} '{name}': {msg}")
# Test class name generation
test_app_names = ['Archery Scorecard', 'My App!', 'Score-Keeper Pro', 'Simple']
print("\nClass name generation:")
for name in test_app_names:
class_name = renamer.generate_class_name(name)
print(f" '{name}' → '{class_name}'")
# Test Android package ID generation
test_packages = ['archery_scorecard', 'my_app', 'simple_app']
print("\nAndroid package ID generation:")
for pkg in test_packages:
android_pkg = renamer.generate_android_package_id(pkg)
print(f" '{pkg}' → '{android_pkg}'")
if __name__ == "__main__":
main()
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""
Test Flutter executable finding.
"""
from flutter_rename import FlutterRenamer
def test_flutter_finder():
"""Test Flutter executable detection."""
renamer = FlutterRenamer()
print("Testing Flutter executable detection...")
flutter_cmd = renamer.find_flutter_executable()
if flutter_cmd:
print(f"✓ Found Flutter: {flutter_cmd}")
# Test if it actually works
import subprocess
try:
result = subprocess.run([flutter_cmd, "--version"], capture_output=True, text=True, timeout=10)
if result.returncode == 0:
print("✓ Flutter command works!")
print(f"Version output: {result.stdout.split()[1] if len(result.stdout.split()) > 1 else 'Unknown'}")
else:
print(f"✗ Flutter command failed: {result.stderr}")
except Exception as e:
print(f"✗ Error testing Flutter: {e}")
else:
print("✗ Flutter not found")
if __name__ == "__main__":
test_flutter_finder()
+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""
Test script for improved Flutter rename script functionality.
"""
from flutter_rename import FlutterRenamer
def test_no_changes_detection():
"""Test that the script correctly detects when no changes are needed."""
print("Testing no-changes detection...")
renamer = FlutterRenamer()
config = renamer.detect_current_configuration()
# Test with current values
if config.get('package_name') and config.get('app_title'):
package_name = config.get('package_name')
app_name = config.get('app_title')
class_name = renamer.generate_class_name(app_name)
android_package_id = renamer.generate_android_package_id(package_name)
# Check if no-changes detection logic works
no_changes = (
package_name == config.get('package_name', '') and
app_name == config.get('app_title', '') and
class_name == config.get('main_class', '') and
android_package_id == config.get('android_namespace', '')
)
print(f"Current package name: {config.get('package_name')}")
print(f"Current app title: {config.get('app_title')}")
print(f"Current class name: {config.get('main_class')}")
print(f"Current Android namespace: {config.get('android_namespace')}")
print()
print(f"Generated class name: {class_name}")
print(f"Generated Android package: {android_package_id}")
print()
print(f"No changes detected: {no_changes}")
if not no_changes:
print("Differences found:")
if class_name != config.get('main_class', ''):
print(f" Class name: {config.get('main_class')} → {class_name}")
if android_package_id != config.get('android_namespace', ''):
print(f" Android package: {config.get('android_namespace')} → {android_package_id}")
else:
print("Configuration not fully detected, cannot test no-changes logic")
def test_current_config_display():
"""Test current configuration detection."""
print("\n" + "="*60)
print("TESTING CURRENT CONFIGURATION DETECTION")
print("="*60)
renamer = FlutterRenamer()
config = renamer.detect_current_configuration()
renamer.display_current_config()
if __name__ == "__main__":
test_current_config_display()
print()
test_no_changes_detection()