Trying to use GLB file in flutter - flutter

I am Created a 3D model and exported the file in the extension .GLB I like to use that file in flutter is there any way to use.
And I have tried with this dependencies model_viewer_plus: ^1.5.0, babylon_dart 1.1.2 and babylonjs_viewer 1.2.1 this and not working.

Try to use model_viewer package inorder to display 3D model in your app.
Try my code to give you an idea:
import 'package:flutter/material.dart';
import 'package:model_viewer/model_viewer.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ModelViewer(
src: 'assets/model.glb',
alt: 'A 3D model',
ar: true,
autoPlay: true,
preferredDevicePixelRatio: 1.0,
),
),
);
}
}

Related

Flutter: Coding so that the logic waits until class members are populated before rendering app on screen

A flutter newbie, I'm trying to get my app to take values from a local json file and render them on-screen.
The logic isn't waiting for the class constructor to populate the relevant string variable before rendering the app on screen.
Here's the code that illustrates my problem.
First, my main.dart file:
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'sampleDataClass.dart';
import 'package:provider/provider.dart';
String assetFilePath = 'assets/basicData.json';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (details) {
FlutterError.presentError(details);
if (kReleaseMode) exit(1);
};
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
child: const MaterialApp(
title: "Sample screen",
home: MyHomePage(),
)
);
}
}
class MyAppState extends ChangeNotifier {
SampleDataClass current = SampleDataClass(assetFilePath);
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
#override
Widget build(BuildContext context) {
var myAppState = context.watch<MyAppState>();
var myAppBasicData = myAppState.current;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo,
foregroundColor: Colors.amberAccent,
title: const Text("This is the App Bar"),
elevation: 10,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Expanded(
child: Container(
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (
Text(myAppBasicData.language,
style: const TextStyle(
color: Colors.white,
))))),
]),
]
),
);
}
}
Here is my SampleDataClass.dart file:
import 'dart:core';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
class SampleDataClass {
String classFilePath = "";
String language = "not populated";
String batteryName = "not populated";
SampleDataClass(filePath) {
rootBundle.loadString(filePath).then((jsonDataString) {
Map classDataMap = jsonDecode(jsonDataString);
language = classDataMap['language'];
print(language);
batteryName = classDataMap['batteryName'];
print(batteryName);
});
}
Map<String, dynamic> toJson() => {
'language': language,
'batteryName': batteryName,
};
}
And here's my pubspec.yaml file:
name: samples
description: A new Flutter project.
environment:
sdk: '>=2.18.6 <3.0.0'
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
provider: ^4.1.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- assets/basicData.json
Finally, my basicData.json file looks like this:
{
"language": "English",
"batteryName": "Exercise 32"
}
The print statements in the sampleDataClass class work fine but as you can see if you run the code (from the command line using "flutter run --no-sound-null-safety"), the app renders the initial values of the variables before the class constructor assigns the json values to them.
What I expected was the class constructor to complete writing the relevant json values to the class members before handing back the class instance to the build widget in main.dart to render the app on-screen.
I realise that this is a very elementary question, but it is a key pending learning task for me!
Thank you all very much in advance!
it looks like rootBundle is an async function, you have to wait till it finishes then you notify the UI to render with new data,
How to handle this :
you should have 3 states loading state , error state , loaded state ,
create an init(path) function in SampleDataClass class that returns SampleDataClass instance.
Also, in the init() function at the beginning you have to change the state to loading state then when you get the data you set the state to loaded state, then notify listeners this will help the screen to know which state the page is in.
in the screen , call didChangeDependencies() and inside it, call your Provider => init function ( current = contecxt.read<YPUR_PROVIDER>().init(path); )
therefore the current object is being initialized, the page is showing a loader, and once the initialization is done, the provider will notify the page and it will change the loading view to the loaded view (your current view).
Another Tip::
your app will close whenever there is an error, it is not a good practice, since flutter will through non-fatal exception
FlutterError.onError = (details) {
FlutterError.presentError(details);
if (kReleaseMode) exit(1);
There is a choice of two excellent solutions to my problem with very little effort and no need for any change management (yet).
Indeed, so similar is the problem described that I probably should have found it before I posted this query here. As a newbie in this discipline, I clearly didn't conduct my search with the appropriate key words; for that I apologise!
Thanks very much to everyone for your patience.
The two solution options (which even I was able to follow) can be found here:
Calling an async method from a constructor in Dart

Flutter Stripe CardField not working in web

I am trying to get a CardField from the flutter_stripe package to appear on web. Simply with this code, a card field will display on ios and android. Though once I run on the app on Chrome, a blank line appears with the error, "Bad state: Source maps are not done loading".
I have the flutter_stripe and flutter_stripe_web package both installed and I cannot seem to figure this out. Any advice would be appreciated!
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
#override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
CardFieldInputDetails? _card;
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: Scaffold(
body: Column(
children: [
CardField(
cursorColor: Colors.black,
enablePostalCode: true,
countryCode: 'US',
postalCodeHintText: 'Enter the us postal code',
onCardChanged: (card) {
setState(() {
_card = card;
});
},
),
],
),
),
);
}
}
ios image
web image
Turns out I just needed to include Stripe.publishableKey = 'your publishable key' inside main()!
The flutter_stripe project only has limited, experimental support for the web right now. The maintainers recommend you use Stripe Checkout instead.
If you really want to get the CardField working on the web I was going to suggest you post an issue to the flutter_stripe repo, but it looks like you've already done so.

Is it possible to get the number of pages from a File?

Guys I'm using file_picker to get files from the device. I use open_file to display the file. Is there any way to get the number of pages from the file? I have already seen the possibility with .pdf, but I would like to get from .docs as well.
Try Flutter FileReader
https://pub.dev/packages/flutter_filereader
A local file view widget,Support a variety of file types, such as Doc Eexcl PPT TXT and so on,Android is implemented by Tencent X5,iOS is implemented by WKWebView
import 'package:flutter/material.dart';
import 'package:flutter_filereader/flutter_filereader.dart';
class FileReaderPage extends StatefulWidget {
final String filePath;
FileReaderPage({Key: Key, this.filePath});
#override
_FileReaderPageState createState() => _FileReaderPageState();
}
class _FileReaderPageState extends State<FileReaderPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("doc"),
),
body: FileReaderView(
filePath: widget.filePath,
),
);
}
}
OR open_document
Opening pdf, xlsx, docs, ppt and zip files #
https://pub.dev/packages/open_document

How do I create an animation in After Effects for Flutter?

Currently in the company where I work, they are using Flutter, before I created the animations in After Effects, created a JSON and sent it to the developers. Now, I need to find a way in which After Affects talks to FLUTTER.
Use the lottie package. Add your JSON as an asset or host it somewhere. Then you can follow the example code(shown below) to display it.
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListView(
children: [
// Load a Lottie file from your assets
Lottie.asset('assets/LottieLogo1.json'),
// Load a Lottie file from a remote url
Lottie.network(
'https://raw.githubusercontent.com/xvrh/lottie-flutter/master/example/assets/Mobilo/A.json'),
// Load an animation and its images from a zip file
Lottie.asset('assets/lottiefiles/angel.zip'),
],
),
),
);
}
}

How to open a PDF file in a Flutter App and have a selectable view?

I want to implement a App in Flutter that allows the user to view PDF-files and select and copy the text thereinin so that I can proceed using the copied text from the Clipboard.
To be more precise I want want my Code to be somewhat like the following but with a package that allows to copy from the displayed PDF file.
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
import 'package:flutter/services.dart';
class PDFPage extends StatefulWidget {
final File file;
final Function onPressed;
const PDFPage(this.file,this.onPressed);
#override
_PDFPageState createState() => _PDFPageState();
}
class _PDFPageState extends State<PDFPage> {
#override
void initState() {
Clipboard.setData(ClipboardData(text: ''));
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: PdfViewer(
filePath: widget.file.path,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.beach_access),
onPressed: ()async{
ClipboardData data = await Clipboard.getData('text/plain');
widget.onPressed(data);
},
),
);
}
}
The package I am using here is
pdf_viewer_plugin: ^1.0.0+2
That seems not to work. It does display the PDF file but I can't select text or copy from it. I have also tried
flutter_full_pdf_viewer: ^1.0.6
advance_pdf_viewer: ^1.1.6
and some more I can't remind the name of.
Is there a package that allows me to do so, or a modest way to either modify or create such a package?
Thanks a lot.