I'm trying to find the coordinates for the screen while I'm outside of the app. I found a way to get the coordinates inside the app by this way.
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey key = GlobalKey();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Material(
child: Listener(
child: SizedBox.expand(
key: key,
child: GestureDetector(
onPanUpdate: (details) {
print(details.globalPosition);
},
),
),
),
),
);
}
}
I/flutter ( 9587): Offset(149.4, 182.0)
I/flutter ( 9587): Offset(148.6, 182.8)
I/flutter ( 9587): Offset(148.6, 183.4)
This obviously works inside the app. Is there a way to know the same positions outside of the app. That means like Even shows the location when dragged in the home screen or any other app
Is the way to do it by creating a background service? if so How do you get the app screen(1st picture) and phone screen meaning working any where on the phone(2nd image) to work together?
Edit: Is there a way to get the pointer location data from settings > Developer options > Input > Pointer location
Any help would be appreciated!
Maybe you can try pyautogui. position but this work only for your desktop screen, but if you have an Android studio you can try it and after this, you can calculate the position of your android screen you need not the screen resolution of your desktop, and if you put an Android emulation to corner you can easily calculate with this math operation 1:x(times2. Or if you have only mobile devices you can try some apps in PlayStore
import time
import pyautugui as pg
time.sleep(5)
print(pg.position())
Related
In my flutter project (latest version), I use a physical android device for development. But in my phone, in the flutter debug app, a semi transparent black overlay is always showing... I tried to run a new project & there also was an overlay.. I don't know if it is the issue with my flutter sdk or my device.
My Code so far
//main.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: Home(),
);
}
}
Other widgets
//home.main.dart
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text("SHKSC", style: TextStyle(color: Colors.indigoAccent[50])),
iconTheme: IconThemeData(color: Colors.blue, opacity: 0.7),
),
body: HomeBody(), /* Incoming from home.body */
);
}
}
The image of the running app on my physical device in Scrcpy..
ImageLink
I have an issue with flutter. I have managed to implement a basic navigation system that keeps state when you do either of the following:
switch between tabs
press the android home button and re-open the app (either by clicking on the app again or using the list of active app button (the little square at the bottom))
But if I press the back button - going back to the android homescreen I completely lose state. I have re-implemented some code to randomly generate a number and display it on the app - this way I know if I'm getting the same widget or a new one has been built.
Why do I need this? (if you're interested)
I'm creating an audio app and when I click play song, it plays. but when I click back to the home screen and let it play in the background -> then open the app again, I can play it again and have it playing twice!
Main:
import 'package:flutter/material.dart';
import 'BottomNavigationBarController.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Login',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BottomNavigationBarController(),
);
}
}
Bottom navigation tab (BottomNavigationBarController):
import 'package:flutter/material.dart';
import 'PlaceholderWidget.dart';
class BottomNavigationBarController extends StatefulWidget {
BottomNavigationBarController({Key key}) : super(key: key);
#override
_BottomNavigationBarController createState() => _BottomNavigationBarController();
}
class _BottomNavigationBarController extends State<BottomNavigationBarController>{
int _selectedPage = 0;
List<Widget> pageList = List<Widget>();
#override
void initState() {
pageList.add(PlaceholderWidget());
pageList.add(PlaceholderWidget());
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _selectedPage,
children: pageList,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.phone_android),
title: Text('First Page'),
),
BottomNavigationBarItem(
icon: Icon(Icons.phone_android),
title: Text('Second Page'),
),
],
currentIndex: _selectedPage,
selectedItemColor: Colors.blue,
onTap: _onItemTapped,
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _onItemTapped(int index) {
setState(() {
_selectedPage = index;
});
}
}
Random number widget (PlaceholderWidget):
import 'dart:math';
import 'package:flutter/material.dart';
class PlaceholderWidget extends StatefulWidget {
PlaceholderWidget({Key key, this.color}) : super(key: key);
final Color color;
#override
_PlaceholderWidget createState() => _PlaceholderWidget();
}
class _PlaceholderWidget extends State<PlaceholderWidget> with AutomaticKeepAliveClientMixin {
#override
bool get wantKeepAlive => true;
#override
Widget build(BuildContext context) {
return Container(
color: widget.color,
child: Text(random_num().toString()),
);
}
int random_num(){
Random random = new Random();
int randomNumber = random.nextInt(100);
return randomNumber;
}
}
Any help will be appreciated :)
I think Navigator pop deletes the widget not entirely sure. But If you want to save the current state just use navigator push don't pop. Also use named routes this will help you greatly.
Use Provider to pass state and keep a global store.
If your app will need to scale, now is a good time to start with MobX/BLoC/Redux/InheritedWidget.. etc.
I was writing a code for my flutter application. In that I needed to get size of screen of mobile so I used Media Query but error occurred saying "MediaQuery was called with no context" but I was passing the context.
Here is the code
class MainPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return MaterialApp(
title: 'Text',
home: Scaffold(
body: Container(
height: height,
width: width,
),
),
);
}
}
How can I solve this error. Please help. Thanks in advance.
You can't do the MediaQuery.of(context) call in the MaterialApp create Methode, because the app does not know anything about the Media. The error message is
MediaQuery.of() call with a context that does not contain a MediaQuery
You can use this call after the app is running but not in the start up sequence.
Maybe you create an extra widget underneath the MaterialApp. Then it would work.
It should be working by the way anyways run flutter clean from cmd or your console and then try running the app.
If it is not working then directly give the height and the width of the scaffold,MediaQuery.of(context).size.height, and MediaQuery.of(context).size.width, respectively and some modifications like this:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('text'),
),
body: Container(
color:Colors.red, //Only to se if the container is being displayed.
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
),
);
}
}
This would definitely work.
if you still face any difficulties then you can ask in the comments.
I have a problem in intergrating a simple GoogleMap in my Flutter App.
I correctly inserted the API Key in my Manifest file and inserted the library in the app.
But the emulator is just showing a blank page. I am doing nothing special until now; just trying to create a GoogleMap.
This is the Code i am using in the App:
return Stack(
children: <Widget>[
GoogleMap(initialCameraPosition: CameraPosition(target:
LatLng(-33.870840,151.206286),
zoom: 12)
)
], );
What the emulator is showing:
The first lines in the console(which i think are of special meaning):
E/flutter ( 5736): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/google_maps
I tried several workarounds but only ended up with more errors. Looking Forward to your answers!
I tried to add Google Map in a fresh project and was able to see it on emulator successfully. I followed this article step by step and used your code snippet to show the map and it worked.
Complete code used:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Maps Sample App'),
backgroundColor: Colors.green[700],
),
body: Stack(
children: <Widget>[
GoogleMap(initialCameraPosition: CameraPosition(target:
LatLng(-33.870840,151.206286),
zoom: 12)
)
],
)
),
);
}
}
Couple of points to note:
Double check if the api is enabled (Maps SDK for Android) when you generated key for it.
Do flutter clean once you cross-check to confirm you've latest dependencies.
Hope this helps.
I want my app to simulate the rounded display corners most modern day smartphones have by rounding the corners of the app and making the background black.
Currently, the TikTok app has something like this.
I already tried using the borderRadius property of the Material widget and wrapping the contents into a container with rounded corners. Neither one worked for me.
Any ideas how this could be done?
I would use ClipRRect at the up most level:
Here is a full example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: MyHomePage(title: 'Flutter Demo Home Page')),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Something")),
body: Container(alignment: Alignment.center, color: Colors.blue, child: Text("hello")));
}
}
And here is the result:
Maybe you will want to reduce the radius from 20 to something like 8 to have the result similar to the image you provided.