thindek_ui 0.4.1 thindek_ui: ^0.4.1 copied to clipboard
Flutter UI toolkit for developing thindek's mobile applications
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
import 'package:thindek_ui/thindek_ui.dart';
import 'image_preview.dart';
void main() {
runApp(DemoApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TDKImage(
url: TEST_URL,
width: 150,
height: 150,
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
const String TEST_URL = 'https://sdkfiledl.jiguang.cn/community/30784/image/FhT5v3dXel8uo5OhG7g6Y5kFiVgT.png';
class DemoApp extends StatefulWidget {
@override
DemoAppState createState() => DemoAppState();
}
class DemoAppState extends State<DemoApp> {
String text = '';
String subject = '';
List<String> imagePaths = [];
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Share Plugin Demo',
home: Scaffold(
appBar: AppBar(
title: const Text('Share Plugin Demo'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(
decoration: const InputDecoration(
labelText: 'Share text:',
hintText: 'Enter some text and/or link to share',
),
maxLines: 2,
onChanged: (String value) => setState(() {
text = value;
}),
),
TextField(
decoration: const InputDecoration(
labelText: 'Share subject:',
hintText: 'Enter subject to share (optional)',
),
maxLines: 2,
onChanged: (String value) => setState(() {
subject = value;
}),
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
ImagePreviews(imagePaths, onDelete: _onDeleteImage),
ListTile(
leading: Icon(Icons.add),
title: Text("Add image"),
onTap: () async {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
);
if (pickedFile != null) {
setState(() {
imagePaths.add(pickedFile.path);
});
}
},
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
child: const Text('Share'),
onPressed: text.isEmpty && imagePaths.isEmpty ? null : () => _onShare(context),
);
},
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
child: const Text('Share With Empty Origin'),
onPressed: () => _onShareWithEmptyOrigin(context),
);
},
),
],
),
),
)),
);
}
_onDeleteImage(int position) {
setState(() {
imagePaths.removeAt(position);
});
}
_onShare(BuildContext context) async {
// A builder is used to retrieve the context immediately
// surrounding the ElevatedButton.
//
// The context's `findRenderObject` returns the first
// RenderObject in its descendent tree when it's not
// a RenderObjectWidget. The ElevatedButton's RenderObject
// has its position and size after it's built.
final RenderBox box = context.findRenderObject() as RenderBox;
if (imagePaths.isNotEmpty) {
await Share.shareFiles(imagePaths,
text: text, subject: subject, sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
} else {
await Share.share(text, subject: subject, sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
}
_onShareWithEmptyOrigin(BuildContext context) async {
await Share.share("text");
}
}