When I use Share.share using Flutter share_plus package the app bar disappears and the content of the screen moves up...and it doesn't look very nice.
Simple example:
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
class Example extends StatelessWidget {
const Example({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () async {
await Share.share('Some text');
},
child: const Text('Press me'),
);
}
}
Anyone knows why that happens?
Try SafeArea around Scaffold widget.
Try this out It works fine for me
final RenderBox box = context.findRenderObject() as RenderBox;
Share.share(shareURL,
subject: title,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
Related
I'm developing a flutter app using Bloc, Division, and Get for the navigation.
I have a text field reusable component that I use in most of the fields that I create, the text field works properly on iOS without any issues, but on Android the Keyboard immediately disappear when I tap the field.
Here are the things that I've done but it still doesn't work:
Not using text field from the component, but directly create another one on the page
Create my own testing page that only used to test the text field
Using resizeToAvoidBottomInset: false, defined the controller as a static final
The only thing that works is when I create another flutter project from the start.
my-testing.dart
import 'package:flutter/material.dart';
class MyTesting extends StatefulWidget {
const MyTesting({Key? key}) : super(key: key);
#override
State<MyTesting> createState() => _MyTestingState();
}
class _MyTestingState extends State<MyTesting> {
late TextEditingController controller;
#override
void initState() {
controller = TextEditingController();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Just testing'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: TextField(
controller: controller,
),
),
);
}
}
TLDR; I added a custom page transition, sliding to the left. It works perfectly for every page in the App except the Welcome screen which contains only a video player. After the video ends, I get transferred to the desired page but the normal 'transition animation' happens instead of my custom one.
More info: I use Navigator.pushNamed(context, '/pagename') for my page travels. I created a custom transition, CustomPageRoute, which did not support named navigation. After a few searches I made it work and now supports both the cleanness of named navigation plus the transition animations plus the data transferring from page to page. All expect the welcome screen which just plays a video then after 3 seconds it pushes the Login page. Here is the code of the Welcome Page:
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class WelcomePage extends StatefulWidget {
const WelcomePage({Key? key}) : super(key: key);
#override
State<WelcomePage> createState() => _WelcomePageState();
}
class _WelcomePageState extends State<WelcomePage> {
late VideoPlayerController _controller;
#override
void initState() {
super.initState();
_controller = VideoPlayerController.asset('assets/welcome_vid.mp4')
..initialize().then((_) {
setState(() {});
})
..setVolume(0.0);
_playVideo();
}
void _playVideo() async {
_controller.play();
await Future.delayed(const Duration(seconds: 3))
.then((_) => Navigator.pushNamed(context, '/login'));
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: _controller.value.isInitialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(
_controller,
),
)
: Container(),
));
}
}
Also, here is a sample of the named routes I use in main.dart:
static Route onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case '/login':
return CustomPageRoute(child: LoginPage(), settings: settings);
Also, Flutter Doctor detected no problems, everything is up to date and I don't think any libraries overlap another. I even tried to change the destination if maybe somehow that affected anything but no.
Why doesn't it work?
I am currently using flutter for an android app and I am using the "Routegenerator.dart" method for navigating . In this project, a certain button gets repeated multiple times and always leads to the same page. I want to create a variable of this button to clean the code a bit and avoid myself useless repetitions. The issue here is that I need to put the variable after the class with the scaffold, and this causes the Navigator.of(context).pushNamed() to give me an error in the (context).
How to solve this issue please?
you can call TextButtonWidget anywhere in your screen like this:
TextButtonWidget(
onTap: (){
Navigator.of(context).pushNamed('anyScreen');
},),
Import this widget
import 'package:flutter/material.dart';
class TextButtonWidget extends StatelessWidget {
const TextButtonWidget({
Key? key,
required this.onTap,
}) : super(key: key);
final VoidCallback onTap;
#override
Widget build(BuildContext context) {
ThemeData _theme = Theme.of(context);
return Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Container(
padding: const EdgeInsets.all(8.0),
child: const Text('Choose me')),
),
);
}
}
Hi I have bean creating a new Flutter project, but in some of screen sizes of the Status bar and the Toolbar has a space between themselves, how can I solve it?
import 'package:flutter/material.dart';
class MapPage extends StatelessWidget {
const MapPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Map"),
),
);
}
}
It's the space for the cutout on device
You need to turn off Simulate display with cutout in developper settings, or edit your manifest app like showed here
https://developer.android.com/guide/topics/display-cutout
I am hitting a wall since I am trying use the context (to call a snackbar when the async action is done) of a widget from a reducer.
I have tried using GlobalKey, but when I get such key and I try to use the context of it to call the widget Scaffold it throws an error which says there is no Scaffold for such context, when there is infact.
Sadly I cannot provide the code because is for a customer and the code cannot be shown, but I can provide you with the details if it is needed.
The snackbar/toast itself is not that important, but if the context of a widget
cannot be get from the reducer would be and important issue in the future.
Thanks
EDIT:
Here is a reproduce of the code as #Günter Zöchbauer has suggested:
-keys file
import 'package:flutter/widgets.dart';
class Keys {
static final GlobalKey<MyWidgetState> myWidgetStateKey = new
GlobalKey<MyWidgetState>();
}
-Widget file
import 'package:myapp/keys.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
class MyPage extends StatefulWidget{
#override
MyPageState createState()=>new MyPageState();
}
class MyPageState extends State<MyPage>{
#override
Widget build(BuildContext conext){
return new Scaffold(
key: Keys.myWidgetStateKey,
appBar:new AppBar(
title:new Text("My app bar")
),
body: new Text("My app body")
);
}
}
-Reducer file
import 'package:flutter/material.dart';
import 'package:casinoadmiralapp/appState.dart';
import 'package:casinoadmiralapp/actions.dart';
import 'package:casinoadmiralapp/keys.dart';
AppState reducer(AppState state,action){
BuildContext context = Keys.myWidgetStateKey.currentContext;
if(action is TheAction){
Scaffold.of(context).showSnackBar(
new SnackBar(
content:new Text("You have done an action"),
action: new SnackBarAction(
label: "UNDO",
onPressed: () => Scaffold.of( context ).hideCurrentSnackBar(),
),
)
);
}
}
I little-bit understand your problem. you are getting problem to show snack toast on view so I have share code to implementing snack bar.
class AprovedScreenState extends State<AprovedScreen> {
BuildContext buildContext;
final key = new GlobalKey<ScaffoldState>();
void navigationPage() {
key.currentState.showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
write key code in scaffold method like below:
#override
Widget build(BuildContext context) {
// TODO: implement build
buildContext = context;
return new Scaffold(
key: key,
)}
Finally I decided to follow #Günter Zöchbauer advice and to put the Scaffold into a store conector so when the store is updated I check the value of the store and due that changing the page.