Skip to content

Commit 68bd1bb

Browse files
committed
add MethodChannel and Assets.
1 parent 49e8e47 commit 68bd1bb

File tree

10 files changed

+265
-8
lines changed

10 files changed

+265
-8
lines changed

android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
2626
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2727

2828
android {
29-
compileSdkVersion 27
29+
compileSdkVersion 28
3030

3131
sourceSets {
3232
main.java.srcDirs += 'src/main/kotlin'
@@ -40,7 +40,7 @@ android {
4040
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4141
applicationId "com.nb.flutterwidgets"
4242
minSdkVersion 16
43-
targetSdkVersion 27
43+
targetSdkVersion 28
4444
versionCode flutterVersionCode.toInteger()
4545
versionName flutterVersionName
4646
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.nb.flutterwidgets
22

33
import android.os.Bundle
4+
import android.widget.Toast
45

56
import io.flutter.app.FlutterActivity
7+
import io.flutter.plugin.common.MethodChannel
68
import io.flutter.plugins.GeneratedPluginRegistrant
79

8-
class MainActivity: FlutterActivity() {
9-
override fun onCreate(savedInstanceState: Bundle?) {
10-
super.onCreate(savedInstanceState)
11-
GeneratedPluginRegistrant.registerWith(this)
12-
}
10+
class MainActivity : FlutterActivity() {
11+
private val CHANNEL = "com.nb.hello/dev"
12+
private var count: Int = 0
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
16+
GeneratedPluginRegistrant.registerWith(this)
17+
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
18+
result.success("nie_bin_${count++}")
19+
Toast.makeText(this, "Hello: ${call.arguments}", Toast.LENGTH_SHORT).show()
20+
}
21+
}
1322
}

lib/const/page_item_const.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,14 @@ const PAGE_ITEMS = [
172172
"img": PageImage.FLUTTER_OPEN,
173173
"click": PageName.LAYOUT,
174174
},
175+
{
176+
"title": PageName.METHOD_CHANNEL,
177+
"img": PageImage.FLUTTER_OPEN,
178+
"click": PageName.METHOD_CHANNEL,
179+
},
180+
{
181+
"title": PageName.ASSET_PAGE,
182+
"img": PageImage.FLUTTER_OPEN,
183+
"click": PageName.ASSET_PAGE,
184+
},
175185
];

lib/const/page_name_const.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ class PageName {
3939
static const EXPANDED = "Expanded";
4040
static const FLOW = "Flow";
4141
static const LAYOUT = "Layout";
42-
42+
static const METHOD_CHANNEL = "MethodChannel";
43+
static const ASSET_PAGE = "AssetsPage";
4344
}

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class FlutterOpenApp extends StatelessWidget {
5353
PageName.EXPANDED: (context) => ExpandPage(),
5454
PageName.FLOW: (context) => FlowPage(),
5555
PageName.LAYOUT: (context) => LayoutPage(),
56+
PageName.METHOD_CHANNEL: (context) => MethodChannelPage(),
57+
PageName.ASSET_PAGE: (context) => AssetsPage(),
5658
},
5759
);
5860
}

lib/page/_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export 'checkbox/CheckBoxPage.dart';
3434
export 'info/_info.dart';
3535
export 'layoutsingle/_layout_single.dart';
3636
export 'muti/_muti.dart';
37+
export 'assets/_assets.dart';

lib/page/assets/AssetsPage.dart

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
///
2+
/// Created by NieBin on 2019/5/26
3+
/// Github: https://github.com/nb312
4+
5+
///
6+
import "package:flutter/material.dart";
7+
import 'package:flutter_widgets/const/_const.dart';
8+
9+
class AssetsPage extends StatefulWidget {
10+
@override
11+
_AssetsState createState() => _AssetsState();
12+
}
13+
14+
class _AssetsState extends State<AssetsPage> {
15+
final _formKey = GlobalKey<FormState>();
16+
final _fieldKey = GlobalKey<FormFieldState>();
17+
var _value = "Hello world";
18+
var _count = 0;
19+
20+
// this.onWillPop,
21+
// this.onChanged,
22+
Future<bool> _willPop() async {
23+
return _formKey.currentState.validate();
24+
}
25+
26+
Widget _form() => Form(
27+
key: _formKey,
28+
autovalidate: true,
29+
onWillPop: _willPop,
30+
onChanged: () {
31+
print("onChange.value= ${_fieldKey.currentState.value}");
32+
},
33+
child: Column(
34+
crossAxisAlignment: CrossAxisAlignment.start,
35+
children: <Widget>[
36+
TextFormField(
37+
key: _fieldKey,
38+
validator: (value) {
39+
if (value.isEmpty) {
40+
return 'Please enter some text';
41+
}
42+
if (value == "hello world") {
43+
return 'This is is hello world';
44+
}
45+
return null;
46+
},
47+
),
48+
Padding(
49+
padding: const EdgeInsets.symmetric(vertical: 16.0),
50+
child: RaisedButton(
51+
onPressed: () {
52+
// Validate will return true if the form is valid, or false if
53+
// the form is invalid.
54+
if (_formKey.currentState.validate()) {
55+
// Process data.
56+
print("print you.");
57+
}
58+
},
59+
child: Text('Submit'),
60+
),
61+
),
62+
],
63+
),
64+
);
65+
66+
// @required this.text,
67+
// this.textAlign = TextAlign.start,
68+
// this.textDirection,
69+
// this.softWrap = true,
70+
// this.overflow = TextOverflow.clip,
71+
// this.textScaleFactor = 1.0,
72+
// this.maxLines,
73+
// this.locale,
74+
// this.strutStyle,
75+
// this.textWidthBasis = TextWidthBasis.parent,
76+
//
77+
List<TextSpan> _spans() => [
78+
TextSpan(
79+
style: TextStyle(color: RED_LIGHT),
80+
text: "Hello",
81+
),
82+
TextSpan(
83+
style: TextStyle(color: BLUE_DEEP, fontSize: 20),
84+
text: "World",
85+
),
86+
];
87+
88+
Widget _rich() => Column(
89+
crossAxisAlignment: CrossAxisAlignment.start,
90+
children: <Widget>[
91+
RichText(
92+
text: TextSpan(
93+
style: TextStyle(),
94+
// text: "Hello",
95+
children: _spans()),
96+
overflow: TextOverflow.ellipsis,
97+
),
98+
],
99+
);
100+
101+
// @required this.builder,
102+
// this.onSaved,
103+
// this.validator,
104+
// this.initialValue,
105+
// this.autovalidate = false,
106+
// this.enabled = true,
107+
// this.enabled = false,
108+
Widget _formField() => FormField(
109+
builder: (FormFieldState s) {
110+
return FloatingActionButton(
111+
child: Text("Button $_value"),
112+
onPressed: () {
113+
_count++;
114+
s.setValue("${s.value}$_count");
115+
s.save();
116+
},
117+
);
118+
},
119+
onSaved: (String v) {
120+
print("value: $v");
121+
setState(() {
122+
_value = v;
123+
});
124+
},
125+
initialValue: _value,
126+
validator: (String v) {
127+
if (v.isEmpty) {
128+
return "Do not allow you to empty.";
129+
}
130+
},
131+
autovalidate: true,
132+
enabled: true,
133+
);
134+
135+
Widget _defaultStyle() => DefaultTextStyle(
136+
style: TextStyle(color: BLUE),
137+
child: Column(
138+
crossAxisAlignment: CrossAxisAlignment.start,
139+
children: <Widget>[
140+
Text("Hello\ngive me some shine."),
141+
Text("World"),
142+
],
143+
),
144+
);
145+
146+
//TODO no done.
147+
Widget _rawImg(BuildContext context) => RawImage();
148+
149+
Widget _assetBundle() {
150+
DefaultAssetBundle.of(context).loadString("");
151+
AssetImage(
152+
"",
153+
bundle: DefaultAssetBundle.of(context),
154+
);
155+
}
156+
157+
@override
158+
Widget build(BuildContext context) {
159+
return Scaffold(
160+
appBar: AppBar(
161+
title: Text(PageName.ASSET_PAGE),
162+
),
163+
body: SingleChildScrollView(
164+
child: Column(
165+
crossAxisAlignment: CrossAxisAlignment.start,
166+
children: <Widget>[
167+
_form(),
168+
_rich(),
169+
_formField(),
170+
_defaultStyle(),
171+
],
172+
),
173+
),
174+
);
175+
}
176+
}

lib/page/assets/_assets.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
///
2+
/// Created by NieBin on 2019/5/26
3+
/// Github: https://github.com/nb312
4+
5+
///
6+
export "AssetsPage.dart";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
///
2+
/// Created by NieBin on 2019/5/26
3+
/// Github: https://github.com/nb312
4+
5+
/// this page is for calling the android native code.
6+
import "package:flutter/material.dart";
7+
import 'package:flutter_widgets/const/_const.dart';
8+
import 'package:flutter/services.dart';
9+
10+
class MethodChannelPage extends StatefulWidget {
11+
@override
12+
_MethodChannelState createState() => _MethodChannelState();
13+
}
14+
15+
class _MethodChannelState extends State<MethodChannelPage> {
16+
static const platform = MethodChannel("com.nb.hello/dev");
17+
String _name = "";
18+
19+
Future<void> _click() async {
20+
String name;
21+
try {
22+
name = await platform.invokeMethod("get");
23+
} on PlatformException catch (e) {
24+
name = "Exception";
25+
}
26+
setState(() {
27+
_name = name;
28+
});
29+
}
30+
31+
@override
32+
Widget build(BuildContext context) {
33+
return Scaffold(
34+
appBar: AppBar(
35+
title: Text(PageName.METHOD_CHANNEL),
36+
),
37+
body: Center(
38+
child: Column(
39+
children: <Widget>[
40+
Text("Hello world! $_name"),
41+
Divider(height: 10),
42+
FloatingActionButton(
43+
child: Text("Click"),
44+
onPressed: _click,
45+
)
46+
],
47+
),
48+
),
49+
);
50+
}
51+
}

lib/page/muti/_muti.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export "IndexStackPage.dart";
77
export "ExpandPage.dart";
88
export 'FlowPage.dart';
99
export 'LayoutPage.dart';
10+
export 'MethodChannelPage.dart';

0 commit comments

Comments
 (0)