英文:
How can i make the first screen of my flutter show up before executing native based java code that will fetch data using a java library
问题
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.dev/battery";
@Override
public void configureFlutterEngine(FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("getDate")) {
try {
result.success(getReal2());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
result.notImplemented();
}
}
);
}
public static List getCoffee() throws IOException {
ArrayList coffee = new ArrayList();
Document doc;
doc = Jsoup.connect("https://www.bankofabyssinia.com").timeout(6000).get();
for (int i = 0; i <= 8; i++) {
for (int j = 2; j <= 3; j++) {
coffee.add(doc.select("#myTable > tbody > tr:nth-child(" + i + ") > td:nth-child(" + j + ")").text());
}
}
return coffee;
}
@TargetApi(Build.VERSION_CODES.N)
public List getReal2() throws ExecutionException, InterruptedException {
CompletableFuture<List> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
return getCoffee();
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
while (!completableFuture.isDone()) {
// Waiting for the CompletableFuture to complete
}
List result = completableFuture.get();
return result;
}
}
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Home.dart';
class Load extends StatefulWidget {
@override
_LoadState createState() => _LoadState();
}
class _LoadState extends State<Load> {
static const platform = const MethodChannel('samples.flutter.dev/battery');
void getWholeJava() async {
try {
final List result = await platform.invokeMethod('getDate');
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (BuildContext context) => Home(
whole: result,
),
));
} on PlatformException catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
getWholeJava();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(builder: (BuildContext context) {
return Container(
child: Center(
child: Column(
children: <Widget>[
Text(
'The app is loading',
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w900,
color: Colors.green[900],
),
),
],
),
),
);
}),
);
}
}
英文:
i was working on some project with flutter that has to fetch data from the internet i tried to get it done using dart but it was not possible so i decided to do it in java and it works the problem the native java code started executing before the loading screen shows up on the screen and it shows some white screen and after that it just goes to the homepage is there anyway that i can make my loading screen appear first and then load the data from the internet as the loading screen is showing while the user waits and push the screen to the home screen after the loading is over.
i have tried putting sleep Duration for one second so that it can render the loading screen first but it didn't work.
Here is my java code
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.dev/battery";
@Override
public void configureFlutterEngine( FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("getDate")) {
try {
result.success(getReal2());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
else {
result.notImplemented();
}
}
);
}
public static List getCoffee() throws IOException {
ArrayList coffee = new ArrayList();
Document doc;
doc= Jsoup.connect("https://www.bankofabyssinia.com").timeout(6000).get();
for(int i=0;i<=8;i++) {
for (int j=2;j<=3;j++){
coffee.add(doc.select(" #myTable > tbody > tr:nth-child("+i+") > td:nth-child("+j+")").text());
}
}
return coffee;
}
@TargetApi(Build.VERSION_CODES.N)
public List getReal2() throws ExecutionException, InterruptedException {
CompletableFuture<List > completableFuture = CompletableFuture.supplyAsync(() -> {
try {
return getCoffee();
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
while (!completableFuture.isDone()) {
}
List result = completableFuture.get();
return result;
}
}
and here is my flutter code
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Home.dart';
class Load extends StatefulWidget {
@override
_LoadState createState() => _LoadState();
}
class _LoadState extends State<Load> {
static const platform = const MethodChannel('samples.flutter.dev/battery');
void getWholeJava() async {
try {
final List result = await platform.invokeMethod('getDate');
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (BuildContext context) => Home(
whole: result,
)));
} on PlatformException catch (e) {
print(e);
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
getWholeJava();
}
@override
Widget build(BuildContext context) {
return Scaffold(body: Builder(builder: (BuildContext context) {
return Container(
child: Center(
child: Column(
children: <Widget>[
Text(
'The app is loading',
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w900,
color: Colors.green[900]),
),
],
)),
);
}));
}
}
答案1
得分: 0
首先,Android 将默认显示其启动屏幕,由模板 Flutter Android 项目中的 launch_background.xml
管理。这意味着你需要为启动屏幕使用一个背景颜色,以便与 Android 原生启动屏幕的可绘制资源相匹配。
接下来,要实现一个启动屏幕,然后导航到一个页面,在该页面上可以运行和加载数据,你可以参考以下代码:
import 'package:async/async.dart';
class SampleSplashScreenPage extends StatefulWidget {
@override
_SampleSplashScreenPageState createState() => new _SampleSplashScreenPageState();
}
class _SampleSplashScreenPageState extends State<SampleSplashScreenPage> {
@override
void initState() {
super.initState();
new RestartableTimer(new Duration(seconds: 3), () async {
await Navigator.push(context, MaterialPageRoute(builder: (_) {
return FlutterDataLoadingPage();
}));
});
}
@override
Widget build(BuildContext context) {
return new Text("Your splash screen");
}
}
RestartableTimer
是一个类,它会启动一个计时器,并计时达到指定的持续时间。一旦达到这个持续时间,它将调用你传递给它的处理程序,这在本例中是导航逻辑,用于前往负责读取远程数据的页面。值得注意的是,该计时器不会重新启动,并且会在垃圾回收器中自动释放。
详细了解请参阅文档:
https://api.flutter.dev/flutter/package-async_async/RestartableTimer-class.html
英文:
First, android will default to its launch screen, which is managed by launch_background.xml
inside the template flutter android project. This means that you will need to use a background color for the splash screen, that will work with the android native launch screen drawable.
Next, to implement a splash screen, that will then navigate to a page, where you can run and load your data, you can look at this:
import 'package:async/async.dart';
class SampleSplashScreenPage extends StatefulWidget {
@override
_SampleSplashScreenPageState createState() => new _SampleSplashScreenPageState();
}
class _SampleSplashScreenPageState extends State<SampleSplashScreenPage> {
@override
void initState() {
super.initState();
new RestartableTimer(new Duration(seconds: 3), () async {
await Navigator.push(context, MaterialPageRoute(builder: (_) {
return FlutterDataLoadingPage();
}));
});
}
@override
Widget build(BuildContext context) {
return new Text("Your splash screen");
}
RestartableTimer
is a class that will begin a stopwatch, and will count up to the designated duration. Once this duration is reached, it will then invoke the handler you pass to it, which in this case is the navigation logic to the page responsible for reading the remote data. This counter by the way does not restart, and will automatically be disposed by the garbage collector.
Take a look at the documentation to learn more:
https://api.flutter.dev/flutter/package-async_async/RestartableTimer-class.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论