text
stringlengths 1
372
|
---|
in android, you typically subclass view, or use a pre-existing view,
|
to override and implement methods that achieve the desired behavior.
|
in flutter, build a custom widget by composing
|
smaller widgets (instead of extending them).
|
it is somewhat similar to implementing a custom ViewGroup
|
in android, where all the building blocks are already existing,
|
but you provide a different behavior—for example,
|
custom layout logic.
|
for example, how do you build a CustomButton that takes a label in
|
the constructor? create a CustomButton that composes a ElevatedButton with
|
a label, rather than by extending ElevatedButton:
|
<code_start>
|
class CustomButton extends StatelessWidget {
|
final string label;
|
const CustomButton(this.label, {super.key});
|
@override
|
widget build(BuildContext context) {
|
return ElevatedButton(
|
onPressed: () {},
|
child: text(label),
|
);
|
}
|
}
|
<code_end>
|
then use CustomButton, just as you’d use any other flutter widget:
|
<code_start>
|
@override
|
widget build(BuildContext context) {
|
return const center(
|
child: CustomButton('Hello'),
|
);
|
}
|
<code_end>
|
<topic_end>
|
<topic_start>
|
intents
|
<topic_end>
|
<topic_start>
|
what is the equivalent of an intent in flutter?
|
in android, there are two main use cases for intents: navigating between
|
activities, and communicating with components. flutter, on the other hand,
|
does not have the concept of intents, although you can still start intents
|
through native integrations (using a plugin).
|
flutter doesn’t really have a direct equivalent to activities and fragments;
|
rather, in flutter you navigate between screens, using a navigator and
|
routes, all within the same activity.
|
a route is an abstraction for a “screen” or “page” of an app, and a
|
navigator is a widget that manages routes. a route roughly maps to an
|
activity, but it does not carry the same meaning. a navigator can push
|
and pop routes to move from screen to screen. navigators work like a stack
|
on which you can push() new routes you want to navigate to, and from
|
which you can pop() routes when you want to “go back”.
|
in android, you declare your activities inside the app’s AndroidManifest.xml.
|
in flutter, you have a couple options to navigate between pages:
|
the following example builds a map.
|
<code_start>
|
void main() {
|
runApp(MaterialApp(
|
home: const MyAppHome(), // becomes the route named '/'.
|
routes: <string, WidgetBuilder>{
|
'/a': (context) => const MyPage(title: 'page a'),
|
'/b': (context) => const MyPage(title: 'page b'),
|
'/c': (context) => const MyPage(title: 'page c'),
|
},
|
));
|
}
|
<code_end>
|
navigate to a route by pushing its name to the navigator.
|
<code_start>
|
Navigator.of(context).pushNamed('/b');
|
<code_end>
|
the other popular use-case for intents is to call external components such
|
as a camera or file picker. for this, you would need to create a native platform
|
integration (or use an existing plugin).
|
to learn how to build a native platform integration,
|
see developing packages and plugins.
|
<topic_end>
|
<topic_start>
|
how do i handle incoming intents from external applications in flutter?
|
flutter can handle incoming intents from android by directly talking to the
|
android layer and requesting the data that was shared.
|
the following example registers a text share intent filter on the native
|
activity that runs our flutter code, so other apps can share text with
|
our flutter app.
|
the basic flow implies that we first handle the shared text data on the
|
android native side (in our activity), and then wait until flutter requests
|
for the data to provide it using a MethodChannel.
|
first, register the intent filter for all intents in AndroidManifest.xml:
|
then in MainActivity, handle the intent, extract the text that was
|
shared from the intent, and hold onto it. when flutter is ready to process,
|
it requests the data using a platform channel, and it’s sent
|
across from the native side:
|
finally, request the data from the flutter side
|
when the widget is rendered:
|
<code_start>
|
import 'package:flutter/material.dart';
|
import 'package:flutter/services.dart';
|
void main() {
|
runApp(const SampleApp());
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.