currently I am developing an app using Flutter Web and I've been trying to use this library which does not have a lot of documentation.
I've tried the example provided but for some reason it's not working
In the example there is no onLoaded() {} function method and without that I get an error saying that I have to implement it.
Finally, if I want to set the width and height of the website I should call setState(). How do I do that?
Link to the library https://pub.dev/packages/easy_web_view2
Code: (I'm running main() in another file)
import 'package:flutter/material.dart';
import 'package:easy_web_view2/easy_web_view2.dart';
class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('CyberQuiz'),
),
body: EasyWebView(
src: 'https://www.ncsc.gov.uk/',
onLoaded: () {
print('Loaded!!');
},
),
);
}
}
A bit of an unobvious issue. I was trying to embed a government website. Apparently you are not allowed to do that.
I am also using a different library which is called webviewx. It has better documentation than the other one
Related
iam new at flutter iam trying to add CircularProgressIndicator at page loading and then change state but its seems like it freeze on navigation
Main.Dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: AppString.appName,
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: Colors.grey.shade900,
),
home: const DummyScreen(),
);
}
}
Navigation Screen
class DummyScreen extends StatelessWidget {
const DummyScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
child: const Text("Navigate"),
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const AnotherDummyScreen()));
},
),
),
);
}
}
CircularProgressIndicator Screen
class AnotherDummyScreen extends StatelessWidget {
const AnotherDummyScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
}
CircularProgressIndicator Widget Should be more smoothly at page Load as it take a while to start animate
I assume you run this on an emulator or are performing any other heavy performance tasks either in your app or on the device?
Why you have to test on real device taken from the flutter docs:
Simulators and emulators don’t use the same hardware, so their performance characteristics are different some operations are faster on simulators than real devices, and some are slower.
Debug mode enables additional checks (such as asserts) that don’t run in profile or release builds, and these checks can be expensive.
Debug mode also executes code in a different way than release mode. The debug build compiles the Dart code “just in time” (JIT) as the app runs, but profile and release builds are pre-compiled to native instructions (also called “ahead of time”, or AOT) before the app is loaded onto the device. JIT can cause the app to pause for JIT compilation, which itself can cause jank.
"Jank" can have multiple reasons. The you provided code looks totally fine.
If you are running this on a real device I recommened to take look at concrete profiling in flutter. You can do for instance:
flutter run --profile
Note that you also should do profiling always on a real device.
With profiling you can then identify the root issue of your jittering animation. The link from the flutter docs above also does provide a good understanding on how profiling works and how to interpret everything. It also provides information on how to use the flutter dev tools, VScode and Android Studio for further performance analysis.
Rebuild app and try or try on other device.
I have been working on flutter mobile apps, already released multiple version to AppStore/PlayStore.
The code is built for mobile app design.
I am currently looking to support website using the same codebase.
One of the issue with supporting both mobile apps and web is that the UI layout is different.
For example: We will have top bar actions in web but bottom bar navigation in mobile apps.
I think I can use kIsWeb like below to create different appBar and bottomNavigationBar
for each Scaffold widget in each screen.
if (kIsWeb){
\\ web code
}
else{
\\ app code
}
What is the best strategy to build adaptive UI which works for mobile apps and website using same codebase?
Modify this according to your use case :)
1.) Define constraints
const mobileWidth = 480;
const tabletWidth = 900;
const desktopWidth = 1180;
2.) Create a Responsive widget which change layout according to screen size
class ResponsiveLayout extends StatelessWidget {
const ResponsiveLayout({
Key? key,
this.mobileView,
this.tabletView,
this.desktopView,
}) : super(key: key);
final Widget? mobileView;
final Widget? tabletView;
final Widget? desktopView;
#override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, dimens) {
if (dimens.maxWidth <= tabletWidth) {
if (dimens.maxWidth <= mobileWidth) {
return mobileView ?? Text("Mobile view");
} else {
return tabletView ?? Text("Tablet view");
}
} else {
return desktopView ?? Text("Desktop view");
}
});
}
}
3.) Use this responsive widget where you want
class CourseScreen extends StatelessWidget {
const CourseScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const ResponsiveLayout(
mobileView: CourseMobileScreen(),
tabletView: CourseTabletScreen(),
desktopView: CourseDesktopScreen(),
);
}
}
Most likely UI depends on screen size rather than it is running on web or not. The web page can be resized and needed to maintain UI. Mostly I prefer using LayoutBuilder for responsiveness. You can also find some good package on pub. While there are some different functionality/feature depends on between os app/ web app, in this case I use kIsWeb. A web app can be used by android browser.
You can check more about adaptive-responsive design.
You should try responsive_framework pkg. I have used it in my Single code base and created different screen resolution breakpoints as per my use cases.
For ex.
builder: (context, widget) => ResponsiveWrapper.builder(
BouncingScrollWrapper.builder(context, widget),
maxWidth: MediaQuery.of(context).size.width/3,
minWidth: 450,
defaultScale: true,
breakpoints: [
ResponsiveBreakpoint.resize(450, name: MOBILE),
ResponsiveBreakpoint.autoScale(800, name: TABLET),
ResponsiveBreakpoint.autoScale(1000, name: TABLET),
ResponsiveBreakpoint.resize(1200, name: DESKTOP),
ResponsiveBreakpoint.autoScale(2460, name: "4K"),
],
background: Container(color: Color(0xFFF5F5F5))
),
Accordingly, use breakpoints for your UI.
Or
You can create your own screen configs using MediaQuery in a separate file e.g., SizeConfig
For ex.
For Mobile > max_width x maxheight can be 300 x 480. likewise for Tablet and Desktop.
Then you can use it to inflate list items in GridView (for crossAxisCount) and ListView items
There is a custom appbar:
class _MyAppBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SliverAppBar(
title: Text('Catalog', style: Theme.of(context).textTheme.headline1),
...
);
}
}
and usage of it
return Scaffold(
body: CustomScrollView(
slivers: [
_MyAppBar(),
const SliverToBoxAdapter(child: SizedBox(height: 12)),
...
In that code const using wildly (it's the Flutter team code as an example of a particular package usage) and why in this case const isn't used for _MyAppBar() (we could add const constructor in _MyAppBar definition)?
When you create a widget and is able to prefix it with const that widget will not rebuild. The framework knows not to rebuild such widgets because it can tell that the objects did not change. Constant objects are resolved at compile-time, and due to a process known as canonicalization if two or more objects have the same parameters, and are constant, they will refer to the same instance.
For example, if you have
#override
Widget build() {
const MyWidget();
}
And then somewhere you call setState, MyWidget will not be reconstructed because it was already resolved at compile-time, and the framework will also not call its build method which makes sense because non of the arguments passed to MyWidget (which are none here) has changed as a result of issuing the rebuild so the configuration is still the same.
Moreover if somewhere else you call const MyWidget(); you will still refer to the same object/instance so it's more optimal.
And they added the lint rule so that people add a const constructor and are able to invoke their widgets/classes with const and it uses by default in flutter. Flutter apps, packages, and plugins created with flutter create starting with Flutter version 2.3.0 are already set up to use the lints defined in this package.
Disabling individual rules
include: package:lints/recommended.yaml
linter:
rules:
avoid_shadowing_type_parameters: false
await_only_futures: true
For more read this analysis option and const keyword.
After reading this, it would be helped you to understand why in your case const isn't used for _MyAppBar().
SRC From: remove const
when you use const behind widgets it changes the way it is rebuilt it will not rebuild completely from scratch when you setState the widget.it uses the data that has been stored on ram and rebuilds it with that.
when your widget does not change with rebuilding the page it is recommended to improve the application behavoiur
I tried to implement math equations in flutter application using the flutter TeX package. It takes much time to render the equation.
It doesn't look so nice as I wanted to be. Are there any other implementations to effectively use math chemistry and other complex format equations without compromising the design.
here's my code:
import 'package:flutter/material.dart';
import 'package:flutter_tex/flutter_tex.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget{
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body:TeXView(
teXHTML: r"""
<style>#myDiv
{color: "#CC0000",}</style>
<div id='myDiv'>$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$</div>
""" ,
renderingEngine: RenderingEngine.Katex, // Katex for fast render and MathJax for quality render.
onRenderFinished: (height) {
print("Widget Height is : $height");
},
onPageFinished: (string) {
print("Page Loading finished");
},
)
);
}}
Here's the output: [screenshot][1]
There is now also the catex package (full disclosure: I am an author).
CaTeX does not need web views, which is why you can render your equations extremely fast (basically as fast as any other widget).
Note: it is currently in pre-release, which means that a lot of functionality is still unsupported.
import 'package:catex/catex.dart';
Widget build(BuildContext context) => CaTeX(r'x = {-b \pm \frac{\sqrt{b^2-4ac}} {2a}}');
Just take a look at the latest version of flutter_tex:^3.5.0+2, styling feature has been added, now you can style each and everything very easily. There are some API changes in this version so please be careful before upgrading and do check the example before proceeding.
As for rendering speed is concerned you should change rendering engine from Mathjax to Katex which is much faster than Mathjax. e.g. renderingEngine:RenderingEngine.Katex
I am currently developing an application with Flutter, I want to add bluetooth support to it, I was thinking that it could be cool to use Android's built in bluetooth menu to choose which bluetooth device to pair with instead of developing my own, but is it possible ?
I have searched on google but found no answer (let me know if I didn't searched enough), would be cool if someone could enlighten this topic. Thanks.
Try using system_setting package.
Here's an example
import 'package:flutter/material.dart';
import 'package:system_setting/system_setting.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
onPressed: _jumpToSetting,
child: Text('Goto setting'),
),
),
),
));
_jumpToSetting() {
SystemSetting.goto(SettingTarget.BLUETOOTH);
}
Instead of system_setting package use app_setings package.
ElevatedButton(
child: Text("Bluetooth"),
onPressed: () {
AppSettings.openBluetoothSettings();
},
)
This Code will take you to the Bluetooth Settings of Android and iOS. More examples in https://pub.dev/packages/app_settings/example
https://pub.dev/packages/android_intent is discontinued
https://pub.dev/packages/intent is not nullsafe support
https://pub.dev/packages/android_intent_plus its ok
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:app_settings/app_settings.dart';
import 'package:android_intent_plus/android_intent.dart';
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
if (Platform.isAndroid) {
const AndroidIntent(
action:
'android.bluetooth.adapter.action.REQUEST_ENABLE',
).launch().catchError(
(e) => AppSettings.openBluetoothSettings());
} else {
AppSettings.openBluetoothSettings();
}
}
return Center(child: CircularProgressIndicator());
}
If I understand correctly, what you want is to open the Android Bluetooth config screen when the user clicks a button in Flutter, is that right?
To achieve that you can use the plugin android_intent [1] to open the settings screen
AndroidIntent intent = AndroidIntent(
action: 'android.settings.BLUETOOTH_SETTINGS',
);
await intent.launch();
You may want to check if the platform is android before to do that in case that you export the app to iOS.
[1] https://pub.dev/packages/android_intent