-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
131 lines (121 loc) · 4.07 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'package:flutter/material.dart';
import 'package:flutter_config/flutter_config.dart';
import 'package:tenor_flutter/tenor_flutter.dart';
import 'package:tenor_flutter_example/examples/dark_theme.dart';
import 'package:tenor_flutter_example/examples/localization.dart';
void main() async {
// only used to load api key from .env file, not required
WidgetsFlutterBinding.ensureInitialized();
await FlutterConfig.loadEnvVariables();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tenor Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// replace apiKey with an api key provided by Tenor > https://developers.google.com/tenor/guides/quickstart
var tenor = Tenor(apiKey: FlutterConfig.get('TENOR_API_KEY'));
// define a result that we can display later
TenorResult? selectedResult;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Tenor Flutter Demo'),
),
body: _exampleBody(),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
// A default implementation of tenor flutter. Displays the gif picker
// as a bottom sheet and then updates the selectedResult in state.
FloatingActionButton(
onPressed: () async {
final result = await tenor.showAsBottomSheet(context: context);
setState(() {
selectedResult = result;
});
},
tooltip: 'Default',
child: const Icon(Icons.add),
),
],
),
);
}
// Additional examples, see: https://github.com/Flyclops/tenor_flutter/tree/main/example/lib/examples
Widget _exampleBody() {
final selectedGif = selectedResult?.media.tinyGif ??
selectedResult?.media.tinyGifTransparent;
return Center(
child: Stack(
alignment: Alignment.center,
children: [
Align(
alignment: Alignment.topCenter,
child: Column(
children: [
const SizedBox(height: 8),
const Text('Additional Examples'),
const SizedBox(height: 8),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: [
// https://github.com/Flyclops/tenor_flutter/tree/main/example/lib/examples/dark_theme.dart
ElevatedButton(
onPressed: () => push(const DarkTheme()),
child: const Text('Dark Theme'),
),
// https://github.com/Flyclops/tenor_flutter/tree/main/example/lib/examples/localization.dart
ElevatedButton(
onPressed: () => push(const Localization()),
child: const Text('Localization'),
),
],
),
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
selectedResult != null && selectedGif != null
? Image.network(
selectedGif.url,
width: selectedGif.dimensions.width,
height: selectedGif.dimensions.height,
)
: const Text('No GIF selected'),
],
),
],
),
);
}
void push(Widget page) {
Navigator.of(context).push(
MaterialPageRoute<String>(
builder: (BuildContext context) {
return page;
},
),
);
}
}