Building efficient and user-friendly mobile applications in Flutter requires an in-depth understanding of the Flutter framework and its UI components and performance optimization techniques. Flutter makes the development process more efficient with its concise code and rich UI component library. By making good use of its performance optimization skills, such as animation, layout management and resource management, you can create amazing mobile applications.
Flutter development practical case: building efficient and user-friendly mobile applications.
Flutter, as an open source UI software development kit launched by Google, has quickly become a popular choice for developers due to its efficient development model and cross-platform features. This article will take an in-depth look at how to build an efficient and user-friendly mobile application using the Flutter framework through an actual development case.
Project Overview.
We will build a simple accounting application where users can record daily income and expenses and generate monthly reports. This application will include the following main functions: 1. User login/registration interface 2. The main interface displays today's income and expenses 3. Add a new record of income or expenditure 4. View history 5. Generate monthly reports
Environment construction.
First, make sure you have the Flutter SDK installed. If not installed, you can visit [Flutter official website] (https://flutter.dev/) to download and install.
After the installation is complete, run the following command to create a new Flutter project:
flutter create finance_app
cd finance_app
User login/registration interface.
We will use flutter_bloc
Library to manage the status of user authentication. First, add a dependency in the pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.0.0
firebase_auth: ^3.0.0
google_sign_in: ^5.0.0
Then, create a Bloc to handle user authentication logic. Create a new one in the lib directory auth
Folder and create in it auth_bloc.dart
File:
dart
// lib/auth/auth_bloc.dart
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
part 'auth_event.dart';
part 'auth_state.dart';
class AuthBloc extends Bloc {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
AuthBloc() : super(AuthInitial()) {
on((event, emit) async {
if (event is AuthLoggedInRequested) {
try {
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
await _firebaseAuth.signInWithCredential(credential);
emit(AuthLoggedIn());
} catch (e) {
emit(AuthLoggedOut());
print(e);
}
} else if (event is AuthLoggedOutRequested) {
await _googleSignIn.signOut();
await _firebaseAuth.signOut();
emit(AuthLoggedOut());
}
});
}
}
Next, create an event and status file:
dart
// lib/auth/auth_event.dart
part of 'auth_bloc.dart';
abstract class AuthEvent {}
class AuthLoggedInRequested extends AuthEvent {}
class AuthLoggedOutRequested extends AuthEvent {}
dart
// lib/auth/auth_state.dart
part of 'auth_bloc.dart';
abstract class AuthState {}
class AuthInitial extends AuthState {}
class AuthLoggedIn extends AuthState {}
class AuthLoggedOut extends AuthState {}
Finally, use this Bloc in the main file:
dart
// lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:finance_app/auth/auth_bloc.dart';
import 'package:finance_app/screens/home_screen.dart';
import 'package:finance_app/screens/login_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => AuthBloc()),
],
child: MaterialApp(
title: 'Finance App',
theme: ThemeData(primarySwatch: Colors.blue),
home: BlocBuilder(
builder: (context, state) {
if (state is AuthLoggedIn) {
return HomeScreen();
} else {
return LoginScreen();
}
},
),
),
);
}
}
The main interface displays today's income and expenses.
We will use flutter_bloc
To manage the state of the application. First, add a dependency in the pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.0.0
equatable: ^2.0.0
Then, create a Bloc to handle the state of the application. Create a new one in the lib directory app
Folder and create in it app_bloc.dart
File:
dart
// lib/app/app_bloc.dart
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
part 'app_event.dart';
part 'app_state.dart';
class AppBloc extends Bloc {
AppBloc() : super(AppInitial()) {
on((event, emit) {});
}
}
Next, create an event and status file:
dart
// lib/app/app_event.dart
part of 'app_bloc.dart';
abstract class AppEvent extends Equatable {}
class AppInitialized extends AppEvent {
@override
List
dart
// lib/app/app_state.dart
part of 'app_bloc.dart';
abstract class AppState extends Equatable {}
class AppInitial extends AppState {
@override
List
Finally, use this Bloc in the main file:
dart
// lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:finance_app/app/app_bloc.dart';
import 'package:finance_app/screens/home_screen.dart';
import 'package:finance_app/screens/login_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => AppBloc()),
],
child: MaterialApp(
title: 'Finance App',
theme: ThemeData(primarySwatch: Colors.blue),
home: BlocBuilder(
builder: (context, state) {
if (state is AppInitialized) {
return HomeScreen();
} else {
return LoginScreen();
}
},
),
),
);
}
}
Add a new record of income or expenses.
We will use flutter_bloc
To manage the status of adding records. First, add a dependency in the pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.0.0
equatable: ^2.0.0