mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.6 KiB
162 lines
4.6 KiB
5 years ago
|
// Copyright 2019 The Flutter team. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file.
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter/services.dart';
|
||
5 years ago
|
import 'package:provider/provider.dart';
|
||
5 years ago
|
|
||
5 years ago
|
/// The entrypoint for the flutter module.
|
||
5 years ago
|
void main() {
|
||
5 years ago
|
// This call ensures the Flutter binding has been set up before creating the
|
||
|
// MethodChannel-based model.
|
||
5 years ago
|
WidgetsFlutterBinding.ensureInitialized();
|
||
|
|
||
|
final model = CounterModel();
|
||
|
|
||
|
runApp(
|
||
|
ChangeNotifierProvider.value(
|
||
|
value: model,
|
||
|
child: MyApp(),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
5 years ago
|
/// A simple model that uses a [MethodChannel] as the source of truth for the
|
||
5 years ago
|
/// state of a counter.
|
||
5 years ago
|
///
|
||
|
/// Rather than storing app state data within the Flutter module itself (where
|
||
|
/// the native portions of the app can't access it), this module passes messages
|
||
|
/// back to the containing app whenever it needs to increment or retrieve the
|
||
|
/// value of the counter.
|
||
5 years ago
|
class CounterModel extends ChangeNotifier {
|
||
|
CounterModel() {
|
||
|
_channel.setMethodCallHandler(_handleMessage);
|
||
|
_channel.invokeMethod('requestCounter');
|
||
|
}
|
||
|
|
||
|
final _channel = MethodChannel('dev.flutter.example/counter');
|
||
|
|
||
|
int _count = 0;
|
||
|
|
||
|
int get count => _count;
|
||
|
|
||
|
void increment() {
|
||
|
_channel.invokeMethod('incrementCounter');
|
||
|
}
|
||
|
|
||
|
Future<dynamic> _handleMessage(MethodCall call) async {
|
||
|
if (call.method == 'reportCounter') {
|
||
|
_count = call.arguments as int;
|
||
|
notifyListeners();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
/// The "app" displayed by this module.
|
||
|
///
|
||
|
/// It offers two routes, one suitable for displaying as a full screen and
|
||
|
/// another designed to be part of a larger UI.class MyApp extends StatelessWidget {
|
||
5 years ago
|
class MyApp extends StatelessWidget {
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return MaterialApp(
|
||
|
title: 'Flutter Module Title',
|
||
|
routes: {
|
||
|
'/': (context) => FullScreenView(),
|
||
5 years ago
|
'/mini': (context) => Contents(),
|
||
5 years ago
|
},
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
/// Wraps [Contents] in a Material [Scaffold] so it looks correct when displayed
|
||
|
/// full-screen.
|
||
5 years ago
|
class FullScreenView extends StatelessWidget {
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
5 years ago
|
title: Text('Full-screen Flutter'),
|
||
5 years ago
|
),
|
||
5 years ago
|
body: const Contents(showExit: true),
|
||
5 years ago
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
/// The actual content displayed by the module.
|
||
|
///
|
||
|
/// This widget displays info about the state of a counter and how much room (in
|
||
|
/// logical pixels) it's been given. It also offers buttons to increment the
|
||
|
/// counter and (optionally) close the Flutter view.
|
||
5 years ago
|
class Contents extends StatelessWidget {
|
||
5 years ago
|
final bool showExit;
|
||
|
|
||
5 years ago
|
const Contents({this.showExit = false});
|
||
5 years ago
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final mediaInfo = MediaQuery.of(context);
|
||
|
|
||
|
return SizedBox.expand(
|
||
|
child: Stack(
|
||
|
children: [
|
||
|
Positioned.fill(
|
||
|
child: DecoratedBox(
|
||
|
decoration: BoxDecoration(
|
||
|
color: Theme.of(context).scaffoldBackgroundColor,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
Positioned.fill(
|
||
|
child: Opacity(
|
||
|
opacity: .25,
|
||
|
child: FittedBox(
|
||
|
fit: BoxFit.cover,
|
||
|
child: FlutterLogo(),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
Center(
|
||
|
child: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: [
|
||
|
Text(
|
||
5 years ago
|
'Window is ${mediaInfo.size.width.toStringAsFixed(1)} x '
|
||
5 years ago
|
'${mediaInfo.size.height.toStringAsFixed(1)}',
|
||
4 years ago
|
style: Theme.of(context).textTheme.headline5,
|
||
5 years ago
|
),
|
||
|
SizedBox(height: 16),
|
||
5 years ago
|
Consumer<CounterModel>(
|
||
|
builder: (context, model, child) {
|
||
|
return Text(
|
||
|
'Taps: ${model.count}',
|
||
4 years ago
|
style: Theme.of(context).textTheme.headline5,
|
||
5 years ago
|
);
|
||
|
},
|
||
5 years ago
|
),
|
||
|
SizedBox(height: 16),
|
||
5 years ago
|
Consumer<CounterModel>(
|
||
|
builder: (context, model, child) {
|
||
4 years ago
|
return ElevatedButton(
|
||
5 years ago
|
onPressed: () => model.increment(),
|
||
|
child: Text('Tap me!'),
|
||
|
);
|
||
|
},
|
||
5 years ago
|
),
|
||
5 years ago
|
if (showExit) ...[
|
||
5 years ago
|
SizedBox(height: 16),
|
||
4 years ago
|
ElevatedButton(
|
||
4 years ago
|
onPressed: () => SystemNavigator.pop(animated: true),
|
||
5 years ago
|
child: Text('Exit this screen'),
|
||
|
),
|
||
|
],
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|