Flutterflow | Common Functions
Here you will find every common function used along the projects, avoid reinventing the wheel, that's why we have this guide here
Convert String Into Currency Format
When you have a currency textfield input, make sure to use it inside of the On Change action, with 0 ms of delay, to update the same textfield
import 'dart:convert';import 'dart:math' as math; import 'package:flutter/material.dart';import 'package:google_fonts/google_fonts.dart';import 'package:intl/intl.dart';import 'package:timeago/timeago.dart' as timeago;import '/flutter_flow/custom_functions.dart';import '/flutter_flow/lat_lng.dart';import '/flutter_flow/place.dart';import '/flutter_flow/uploaded_file.dart';import '/backend/schema/structs/index.dart';import '/backend/schema/enums/enums.dart';import '/auth/custom_auth/auth_util.dart'; String convertStringIntoCurrencyFormat(String input) { /// MODIFY CODE ONLY BELOW THIS LINE // remove all non numeric characters, leave only numbers and format as US currency String result = input.replaceAll(RegExp(r'[^0-9]'), ''); if (result.isEmpty) { return '\$0.00'; } int cents = int.parse(result); double dollars = cents / 100.0; final formatter = NumberFormat.currency(locale: 'en_US', symbol: ''); return formatter.format(dollars); /// MODIFY CODE ONLY ABOVE THIS LINE}
Check Internet Connection Async
Make sure to call the function on the "Final Actions" on main.dart
import '/custom_code/actions/index.dart' as actions;import 'package:provider/provider.dart';import 'package:flutter/gestures.dart';import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart';import 'package:flutter_web_plugins/url_strategy.dart'; import 'auth/custom_auth/auth_util.dart';import 'auth/custom_auth/custom_auth_user_provider.dart'; import '/flutter_flow/flutter_flow_theme.dart';import 'flutter_flow/flutter_flow_util.dart';import 'flutter_flow/internationalization.dart';import 'flutter_flow/nav/nav.dart';import 'index.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); GoRouter.optionURLReflectsImperativeAPIs = true; usePathUrlStrategy(); await authManager.initialize(); final appState = FFAppState(); // Initialize FFAppState await appState.initializePersistedState(); // Start final custom actions code await actions.checkInternetConnection(); // End final custom actions code runApp(ChangeNotifierProvider( create: (context) => appState, child: MyApp(), ));}
Function
// Automatic FlutterFlow importsimport '/backend/schema/structs/index.dart';import '/backend/schema/enums/enums.dart';import '/actions/actions.dart' as action_blocks;import '/flutter_flow/flutter_flow_theme.dart';import '/flutter_flow/flutter_flow_util.dart';import '/custom_code/actions/index.dart'; // Imports other custom actionsimport '/flutter_flow/custom_functions.dart'; // Imports custom functionsimport 'package:flutter/material.dart';// Begin custom action code// DO NOT REMOVE OR MODIFY THE CODE ABOVE! import 'package:connectivity_plus/connectivity_plus.dart';import 'dart:async'; Future checkInternetConnection() async { // Add your function code here! StreamSubscription<ConnectivityResult>? subscription; subscription = Connectivity() .onConnectivityChanged .listen((ConnectivityResult result) async { // Got a new connectivity status! bool hasConnection = false; if (result == ConnectivityResult.mobile || result == ConnectivityResult.wifi || result == ConnectivityResult.ethernet || result == ConnectivityResult.vpn || result == ConnectivityResult.other) { hasConnection = true; } FFAppState().update(() { FFAppState().hasInternet = hasConnection; }); FFAppState().notifyListeners(); });}